-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d8c5b8
commit 2233a33
Showing
2 changed files
with
30 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
import type { App } from 'vue' | ||
import type { Locale } from 'vue-i18n' | ||
import { createI18n } from 'vue-i18n' | ||
import { useLocalStorage } from '@vueuse/core' | ||
|
||
const localPathPrefix = '../locales/' | ||
|
||
const storage = useLocalStorage('otg-options', {}) as any | ||
const locale = storage.value?.apperance?.language | ||
|
||
// import i18n resources | ||
// https://vitejs.dev/guide/features.html#glob-import | ||
const messages = Object.fromEntries( | ||
Object.entries(import.meta.globEager('../locales/*.y(a)?ml')).map( | ||
([key, value]) => { | ||
const yaml = key.endsWith('.yaml') | ||
return [ | ||
key.slice(localPathPrefix.length, yaml ? -5 : -4), | ||
value?.default! | ||
] | ||
const localesMap = Object.fromEntries( | ||
Object.entries(import.meta.glob('../locales/*.yml')).map( | ||
([path, loadLocale]) => { | ||
console.log(path) | ||
return [path.match(/([\w-]*)\.yml$/)?.[1], loadLocale] | ||
} | ||
) | ||
) | ||
|
||
const install = (app: App) => { | ||
const i18n = createI18n({ | ||
legacy: false, | ||
locale, | ||
globalInjection: true, | ||
messages | ||
}) | ||
export const availableLocales = Object.keys(localesMap) | ||
|
||
const i18n = createI18n({ | ||
legacy: false, | ||
locale, | ||
globalInjection: true, | ||
messages: {} | ||
}) | ||
|
||
export async function loadLanguageAsync(lang: string): Promise<Locale> { | ||
const messages = await localesMap[lang]() | ||
i18n.global.setLocaleMessage(lang, messages.default) | ||
return lang | ||
} | ||
|
||
const install = (app: App) => { | ||
app.use(i18n) | ||
loadLanguageAsync('en-US') | ||
} | ||
|
||
export default install |