|
1 | 1 | import type { App } from 'vue' |
| 2 | +import type { Locale } from 'vue-i18n' |
2 | 3 | import { createI18n } from 'vue-i18n' |
3 | 4 | import { useLocalStorage } from '@vueuse/core' |
4 | 5 |
|
5 | | -const localPathPrefix = '../locales/' |
6 | | - |
7 | 6 | const storage = useLocalStorage('otg-options', {}) as any |
8 | 7 | const locale = storage.value?.apperance?.language |
9 | 8 |
|
10 | 9 | // import i18n resources |
11 | | -// https://vitejs.dev/guide/features.html#glob-import |
12 | | -const messages = Object.fromEntries( |
13 | | - Object.entries(import.meta.globEager('../locales/*.y(a)?ml')).map( |
14 | | - ([key, value]) => { |
15 | | - const yaml = key.endsWith('.yaml') |
16 | | - return [ |
17 | | - key.slice(localPathPrefix.length, yaml ? -5 : -4), |
18 | | - value?.default! |
19 | | - ] |
| 10 | +const localesMap = Object.fromEntries( |
| 11 | + Object.entries(import.meta.glob('../locales/*.yml')).map( |
| 12 | + ([path, loadLocale]) => { |
| 13 | + console.log(path) |
| 14 | + return [path.match(/([\w-]*)\.yml$/)?.[1], loadLocale] |
20 | 15 | } |
21 | 16 | ) |
22 | 17 | ) |
23 | 18 |
|
24 | | -const install = (app: App) => { |
25 | | - const i18n = createI18n({ |
26 | | - legacy: false, |
27 | | - locale, |
28 | | - globalInjection: true, |
29 | | - messages |
30 | | - }) |
| 19 | +export const availableLocales = Object.keys(localesMap) |
| 20 | + |
| 21 | +const i18n = createI18n({ |
| 22 | + legacy: false, |
| 23 | + locale, |
| 24 | + globalInjection: true, |
| 25 | + messages: {} |
| 26 | +}) |
31 | 27 |
|
| 28 | +export async function loadLanguageAsync(lang: string): Promise<Locale> { |
| 29 | + const messages = await localesMap[lang]() |
| 30 | + i18n.global.setLocaleMessage(lang, messages.default) |
| 31 | + return lang |
| 32 | +} |
| 33 | + |
| 34 | +const install = (app: App) => { |
32 | 35 | app.use(i18n) |
| 36 | + loadLanguageAsync('en-US') |
33 | 37 | } |
34 | 38 |
|
35 | 39 | export default install |
0 commit comments