36 lines
890 B
TypeScript
36 lines
890 B
TypeScript
import { createI18n } from 'vue-i18n'
|
|
|
|
const lang = navigator.language
|
|
?.split('-')
|
|
.shift()
|
|
?.toLowerCase()
|
|
|
|
const isLanguageSupported = (files: string[]) =>
|
|
lang && files.find((file) => file.includes(lang))
|
|
|
|
function loadLocaleMessages() {
|
|
const locales = import.meta.glob('./locales/*.json', { eager: true })
|
|
const messages: Record<string, any> = {}
|
|
|
|
for (const path in locales) {
|
|
const matched = path.match(/([A-Za-z0-9-_]+)\.json$/i)
|
|
if (matched && matched.length > 1) {
|
|
const locale = matched[1]
|
|
messages[locale] = (locales[path] as any).default
|
|
}
|
|
}
|
|
|
|
if (lang && Object.keys(messages).includes(lang)) {
|
|
const html = document.querySelector('html')
|
|
html?.setAttribute('lang', lang)
|
|
}
|
|
return messages
|
|
}
|
|
|
|
export default createI18n({
|
|
locale: lang || 'en',
|
|
fallbackLocale: 'en',
|
|
messages: loadLocaleMessages(),
|
|
legacy: false
|
|
})
|