From aa68de49340ed731374523d109b060a1ebdcce20 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sat, 18 Jul 2020 23:47:09 +0200 Subject: [PATCH] :building_construction: (lang) update automatically lang supported --- public/index.html | 2 +- src/i18n.ts | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/public/index.html b/public/index.html index a886fbb..21228ea 100644 --- a/public/index.html +++ b/public/index.html @@ -1,5 +1,5 @@ - + diff --git a/src/i18n.ts b/src/i18n.ts index 5d852f5..b4d0e23 100644 --- a/src/i18n.ts +++ b/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',