feat. i18n 설정
This commit is contained in:
54
i18n/locales/fr.ts
Normal file
54
i18n/locales/fr.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
// import { TRANSLATION_ITEMS } from '../i18n.config'
|
||||
// common.json 파일을 직접 import
|
||||
// @ts-ignore
|
||||
import commonData from '../../layers/assets/data/common.json'
|
||||
|
||||
export default defineI18nLocale(async (locale: string) => {
|
||||
const config = useRuntimeConfig()
|
||||
const baseType = config.public.baseType
|
||||
const translationItems = config.public.translationItems
|
||||
const translationItemsArr = translationItems.split(',')
|
||||
const staticUrl = config.public.staticUrl
|
||||
const translationApi = translationItemsArr.map((item: string): string => {
|
||||
return `${staticUrl}/${baseType}/tmp/${item}.json`
|
||||
})
|
||||
|
||||
// API 데이터 가져오기
|
||||
const fetchDataPromises = translationApi.map((apiUrl) => {
|
||||
return useFetch(apiUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json;charset=UTF-8'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
try {
|
||||
const fetchResults = await Promise.all(fetchDataPromises)
|
||||
|
||||
// 각 결과에서 locale에 맞는 데이터를 추출
|
||||
const apiData = fetchResults.map((result) => {
|
||||
return result.data.value?.[locale] || {} // locale에 맞는 데이터가 없으면 빈 객체 반환
|
||||
})
|
||||
|
||||
// apiData를 이용해 자동으로 병합
|
||||
const mergedResult = apiData.reduce((acc, data) => {
|
||||
return { ...acc, ...data }
|
||||
}, {})
|
||||
|
||||
// common.json에서 해당 locale의 데이터를 가져와서 병합
|
||||
const commonLocaleData = commonData[locale] || {}
|
||||
|
||||
// API 데이터와 common.json 데이터를 병합 (common.json이 우선순위)
|
||||
const finalResult = { ...mergedResult, ...commonLocaleData }
|
||||
|
||||
// 병합된 결과 출력
|
||||
// console.log('finalResult:', finalResult)
|
||||
|
||||
return finalResult
|
||||
} catch (error) {
|
||||
console.error('Error fetching translation data:', error)
|
||||
// 에러 발생 시 common.json 데이터라도 반환
|
||||
return commonData[locale] || {}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user