i18n - Multiple languages overrides

This commit is contained in:
romulo bordezani committed 2019-06-04 18:16:07 -03:00
1 parent 1874f93bcc
commit 236a115112
33 files changed
+1401 -695

No files matched your search

+78 -1
View File
@@ -1 +1,78 @@
export { default } from './i18n';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
const currentLanguage = process.env.APP_LANG || 'en-US';
const debugMode = process.env.I18N_DEBUG || false;
function getDefaultLanguage() {
const mainLanguage = currentLanguage.match(/(.*.)(-)/);
return mainLanguage !== null ? mainLanguage[1] : null;
}
function getNameSpaceString(key) {
const nameSpaceMatcher = key.match(/[^/]+$/g);
let finalNameSpace;
if (nameSpaceMatcher !== null) {
finalNameSpace = nameSpaceMatcher[0].replace('.json', '');
}
return finalNameSpace;
}
function getCleanKeyForNameSpaces(key) {
const cleanedKey = key.match(/[/\\].+(?=[/\\])/);
let finalKey;
if (cleanedKey !== null) {
finalKey = cleanedKey[0].replace(/[/\\]/, '');
finalKey = finalKey.replace(/[/\\]/, '-');
}
return finalKey;
}
function getLocales() {
const context = require.context(`./locales`, true, /\.json$/);
const locales = {};
context.keys().forEach(key => {
locales[getCleanKeyForNameSpaces(key)] = {
...locales[getCleanKeyForNameSpaces(key)],
[getNameSpaceString(key)]: context(key),
};
});
return locales;
}
let translate;
i18n
.use(initReactI18next)
.init({
resources: getLocales(),
fallbackLng: getDefaultLanguage(),
lng: currentLanguage,
debug: debugMode,
// have a common namespace used around the full app
keySeparator: false, // uses content as keys
interpolation: {
escapeValue: false,
},
fallbackNS: ['common'],
defaultNS: 'common',
react: {
wait: true,
},
})
.then(function(t) {
translate = t;
});
export { translate as t };
export default i18n;