From bd1ee48a4f398495cb67ff3d2595a25fb34536c9 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Mon, 21 Oct 2019 13:09:52 +0300 Subject: [PATCH 1/2] - client: set i18n language only from available languages --- client/src/helpers/constants.js | 8 ++++---- client/src/i18n.js | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/client/src/helpers/constants.js b/client/src/helpers/constants.js index d4a415a8..fb82c1c9 100644 --- a/client/src/helpers/constants.js +++ b/client/src/helpers/constants.js @@ -30,10 +30,6 @@ export const REPOSITORY = { export const PRIVACY_POLICY_LINK = 'https://adguard.com/privacy/home.html'; export const LANGUAGES = [ - { - key: 'en', - name: 'English', - }, { key: 'da', name: 'Dansk', @@ -46,6 +42,10 @@ export const LANGUAGES = [ key: 'nl', name: 'Dutch', }, + { + key: 'en', + name: 'English', + }, { key: 'es', name: 'EspaƱol', diff --git a/client/src/i18n.js b/client/src/i18n.js index 6ed46d7d..9b5a5f9a 100644 --- a/client/src/i18n.js +++ b/client/src/i18n.js @@ -3,6 +3,8 @@ import { reactI18nextModule } from 'react-i18next'; import { initReactI18n } from 'react-i18next/hooks'; import langDetect from 'i18next-browser-languagedetector'; +import { DEFAULT_LANGUAGE } from './helpers/constants'; + import vi from './__locales/vi.json'; import en from './__locales/en.json'; import ru from './__locales/ru.json'; @@ -99,22 +101,28 @@ const resources = { }, }; +const availableLanguages = Object.keys(resources); + i18n .use(langDetect) .use(initReactI18n) - .use(reactI18nextModule) // passes i18n down to react-i18next + .use(reactI18nextModule) .init({ resources, - fallbackLng: 'en', - keySeparator: false, // we use content as keys - nsSeparator: false, // Fix character in content - returnEmptyString: false, // count empty value as invalid + fallbackLng: DEFAULT_LANGUAGE, + keySeparator: false, + nsSeparator: false, + returnEmptyString: false, interpolation: { - escapeValue: false, // not needed for react!! + escapeValue: false, }, react: { wait: true, }, }); +if (!i18n.language.includes(availableLanguages)) { + i18n.changeLanguage(DEFAULT_LANGUAGE); +} + export default i18n; From 3269766ea7e2340a658cc13343f9d3cc6565b7dc Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Mon, 21 Oct 2019 16:03:17 +0300 Subject: [PATCH 2/2] - client: use lowercase lang codes --- client/src/i18n.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/client/src/i18n.js b/client/src/i18n.js index 9b5a5f9a..5b144110 100644 --- a/client/src/i18n.js +++ b/client/src/i18n.js @@ -51,16 +51,16 @@ const resources = { sv: { translation: sv, }, - 'pt-BR': { + 'pt-br': { translation: ptBR, }, - 'zh-TW': { + 'zh-tw': { translation: zhTW, }, bg: { translation: bg, }, - 'zh-CN': { + 'zh-cn': { translation: zhCN, }, cs: { @@ -87,7 +87,7 @@ const resources = { pl: { translation: pl, }, - 'pt-PT': { + 'pt-pt': { translation: ptPT, }, sk: { @@ -109,6 +109,7 @@ i18n .use(reactI18nextModule) .init({ resources, + lowerCaseLng: true, fallbackLng: DEFAULT_LANGUAGE, keySeparator: false, nsSeparator: false, @@ -119,10 +120,10 @@ i18n react: { wait: true, }, + }, () => { + if (!availableLanguages.includes(i18n.language)) { + i18n.changeLanguage(DEFAULT_LANGUAGE); + } }); -if (!i18n.language.includes(availableLanguages)) { - i18n.changeLanguage(DEFAULT_LANGUAGE); -} - export default i18n;