mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat(i18n): add frontend internationalization setup
This commit is contained in:
1 parent
f1038e40f4
commit
a3c69764c8
6 files changed
+91
-9
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import en from './locales/en.json'
|
||||
import zhTW from './locales/zh-TW.json'
|
||||
|
||||
export const supportedLocales = ['zh-TW', 'en'] as const
|
||||
|
||||
export type SupportedLocale = (typeof supportedLocales)[number]
|
||||
|
||||
function isSupportedLocale(locale: string): locale is SupportedLocale {
|
||||
return supportedLocales.includes(locale as SupportedLocale)
|
||||
}
|
||||
|
||||
function getInitialLocale(): SupportedLocale {
|
||||
const savedLocale = localStorage.getItem('locale')
|
||||
|
||||
if (savedLocale && isSupportedLocale(savedLocale)) {
|
||||
return savedLocale
|
||||
}
|
||||
|
||||
return navigator.language.startsWith('zh') ? 'zh-TW' : 'en'
|
||||
}
|
||||
|
||||
const initialLocale = getInitialLocale()
|
||||
|
||||
document.documentElement.lang = initialLocale
|
||||
|
||||
export const i18n = createI18n({
|
||||
legacy: false,
|
||||
locale: initialLocale,
|
||||
fallbackLocale: 'zh-TW',
|
||||
messages: {
|
||||
'zh-TW': zhTW,
|
||||
en,
|
||||
},
|
||||
})
|
||||
|
||||
export function setLocale(locale: SupportedLocale) {
|
||||
i18n.global.locale.value = locale
|
||||
localStorage.setItem('locale', locale)
|
||||
document.documentElement.lang = locale
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
Reference in new issue
Block a user