Commit c5c112b2 authored by Rejo Zenger's avatar Rejo Zenger

Merge branch 'feature/language-variants' into 'master'

Support language variants correctly

Closes #42

See merge request !104
parents 93b754d2 4f8da9b1
......@@ -3,17 +3,48 @@ import * as LanguageDetector from 'i18next-browser-languagedetector';
import * as moment from 'moment-timezone';
import { reactI18nextModule } from 'react-i18next';
const AVAILABLE_LOCALES = {};
const AVAILABLE_LOCALES: {
[key: string]: { translations: any; moment?: string };
} = {};
// Attempt to load the moment locale, but fall back to the main non-variant language
// if the specified variant is not available
const loadMomentLocale = (iso: string): string | undefined => {
try {
require(`moment/locale/${iso}.js`);
return iso;
} catch (error) {
const isoParts = iso.split('-');
if (isoParts.length > 1) {
return loadMomentLocale(isoParts[0]);
}
}
return undefined;
};
// Change the currently active moment locale to the locale we were able to load when
// setting up the translations
const setMomentLocale = (iso: string) => {
const momentLocale = AVAILABLE_LOCALES[iso].moment;
if (momentLocale) {
moment.locale(momentLocale);
}
};
// Load all files available in the `locales` directory and populate the translations object
const localeFiles = require.context('locales/', true, /\.json$/);
localeFiles.keys().forEach(filename => {
const codeMatch = filename.match(/\.\/([^\.]+)/);
if (codeMatch) {
AVAILABLE_LOCALES[codeMatch[1]] = { translations: localeFiles(filename) };
const iso = codeMatch[1];
if (codeMatch[1] !== 'en') {
require(`moment/locale/${codeMatch[1]}.js`);
}
AVAILABLE_LOCALES[iso] = {
translations: localeFiles(filename),
moment: loadMomentLocale(iso.toLowerCase())
};
}
});
......@@ -34,6 +65,7 @@ export default function createI18N(region: string) {
order: ['querystring', 'cookie', 'localStorage', 'navigator']
},
fallbackLng: regionSettings.locale,
lowerCaseLng: true,
initImmediate: true,
interpolation: {
format: (value: any, format?: string, locale?: string): string => {
......@@ -58,7 +90,7 @@ export default function createI18N(region: string) {
nsMode: 'default',
wait: true
} as any,
resources: AVAILABLE_LOCALES,
resources: AVAILABLE_LOCALES as any,
whitelist: Object.keys(AVAILABLE_LOCALES)
},
() => {
......@@ -66,10 +98,8 @@ export default function createI18N(region: string) {
}
);
moment.locale(i18nInstance.language);
i18nInstance.on('languageChanged', language => {
moment.locale(language);
});
setMomentLocale(i18nInstance.language);
i18nInstance.on('languageChanged', setMomentLocale);
return i18nInstance;
}
......
{
"name": "Belgium",
"locale": "be",
"locale": "fr-be",
"support_email": "info@privacysalon.org",
"partners": [
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment