🏗️ (lang) update automatically lang supported
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="<%= VUE_APP_I18N_LOCALE %>">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
|
||||
21
src/i18n.ts
21
src/i18n.ts
@@ -3,28 +3,37 @@ import VueI18n, { LocaleMessages } from 'vue-i18n'
|
||||
|
||||
Vue.use(VueI18n)
|
||||
|
||||
const lang = navigator.language
|
||||
?.split('-')
|
||||
.shift()
|
||||
?.toLowerCase()
|
||||
|
||||
const isLanguageSupported = (files: string[]) =>
|
||||
lang && files.find((file) => file.includes(lang))
|
||||
|
||||
function loadLocaleMessages(): LocaleMessages {
|
||||
const locales = require.context(
|
||||
'./locales',
|
||||
true,
|
||||
/[A-Za-z0-9-_,\s]+\.json$/i
|
||||
)
|
||||
const languages = locales.keys()
|
||||
const messages: LocaleMessages = {}
|
||||
locales.keys().forEach((key) => {
|
||||
languages.forEach((key) => {
|
||||
const matched = key.match(/([A-Za-z0-9-_]+)\./i)
|
||||
if (matched && matched.length > 1) {
|
||||
const locale = matched[1]
|
||||
messages[locale] = locales(key)
|
||||
}
|
||||
})
|
||||
|
||||
if (lang && isLanguageSupported(languages)) {
|
||||
const html = document.querySelector('html')
|
||||
html?.setAttribute('lang', lang)
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
const lang = navigator.language
|
||||
?.split('-')
|
||||
.shift()
|
||||
?.toLowerCase()
|
||||
|
||||
export default new VueI18n({
|
||||
locale: lang || 'en',
|
||||
fallbackLocale: 'en',
|
||||
|
||||
Reference in New Issue
Block a user