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