🏗️ (lang) update automatically lang supported

This commit is contained in:
2020-07-18 23:47:09 +02:00
parent 83245b1f12
commit aa68de4934
2 changed files with 16 additions and 7 deletions

View File

@@ -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" />

View File

@@ -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',