initial commit. Cloned timetracker repository
This commit is contained in:
47
bo/src/plugins/i18n.ts
Normal file
47
bo/src/plugins/i18n.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { useFetchJson } from '@/composable/useFetchJson'
|
||||
import { langs } from '@/router/langs'
|
||||
import type { Resp } from '@/types'
|
||||
import { getLangs } from '@/utils/fake'
|
||||
import { watch } from 'vue'
|
||||
import { createI18n, type LocaleMessageValue, type PathValue, type VueMessageType } from 'vue-i18n'
|
||||
|
||||
// const x =
|
||||
|
||||
export const i18ninstall = createI18n({
|
||||
legacy: false, // you must set `false`, to use Composition API
|
||||
locale: 'en',
|
||||
lazy: true,
|
||||
messages: {},
|
||||
messageResolver: (obj, path) => {
|
||||
const value = path
|
||||
.split('.')
|
||||
// eslint-disable-next-line
|
||||
.reduce<unknown>((o, key) => (o as any)?.[key], obj as any)
|
||||
|
||||
if (value === '' || value === null || value === undefined) {
|
||||
return null
|
||||
}
|
||||
|
||||
return value as PathValue
|
||||
},
|
||||
})
|
||||
|
||||
export const i18n = i18ninstall.global
|
||||
|
||||
let downloadedLangs = [] as string[]
|
||||
|
||||
watch(
|
||||
i18n.locale,
|
||||
async (l) => {
|
||||
if (!downloadedLangs.includes(l)) {
|
||||
const lang = langs.find((x) => x.iso_code == l)
|
||||
if (!lang) return
|
||||
downloadedLangs.push(l)
|
||||
const res = await useFetchJson<any>(`/api/v1/translations?lang_id=${lang?.id}&scope=backoffice`)
|
||||
// console.log(res.items[lang.id as number]['backoffice'])
|
||||
|
||||
i18n.setLocaleMessage(l, res.items[lang.id]['backoffice'])
|
||||
}
|
||||
},
|
||||
{},
|
||||
)
|
||||
Reference in New Issue
Block a user