i18n - extension extraction

This commit is contained in:
romulo bordezani committed 2019-06-03 19:56:59 -03:00
1 parent aff74bcee4
commit d678a617dc
17 files changed
+58 -44

No files matched your search

+1 -1
View File
@@ -93,7 +93,7 @@ class Header extends Component {
</button>
<button
className="research-use"
onClick={() => this.changeLanguage('en-ES')}
onClick={() => this.changeLanguage('es-MX')}
>
ES
</button>
+28 -33
View File
@@ -1,59 +1,51 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
const currentLanguage = process.env.APP_LANG || 'en-US';
const currentLanguage = process.env.APP_LANG || 'es-MX';
function getDefaultLanguage() {
const mainLanguage = currentLanguage.match(/(.*.)(-)/);
return mainLanguage !== null ? mainLanguage[1] : null;
}
function getCleanKeyForNameSpaces(key) {
const regexKeyCleaner = new RegExp(
`^(\\.\\/${currentLanguage}\\/)(.*.)(\\.json)$`
);
const cleanedKey = key.match(regexKeyCleaner);
return cleanedKey !== null ? cleanedKey[2] : null;
function getNameSpaceString(key) {
const nameSpaceMatcher = key.match(/[^/]+$/g);
let finalNameSpace;
if (nameSpaceMatcher !== null) {
finalNameSpace = nameSpaceMatcher[0].replace('.json', '');
}
return finalNameSpace;
}
function getCleanKeyForDefaults(key) {
const regexKeyCleaner = new RegExp(
`^(\\.\\/${getDefaultLanguage()}\\/)(.*.)(\\.json)$`
);
const cleanedKey = key.match(regexKeyCleaner);
return cleanedKey !== null ? cleanedKey[2] : null;
function getCleanKeyForNameSpaces(key) {
const cleanedKey = key.match(/[/\\].+(?=[/\\])/);
let finalKey;
if (cleanedKey !== null) {
finalKey = cleanedKey[0].replace(/[/\\]/, '');
finalKey = finalKey.replace(/[/\\]/, '-');
}
return finalKey;
}
function getLocales() {
const currentLangList = {};
const defaultLangList = {};
const locales = {};
const context = require.context(`./locales`, true, /\.json$/);
const locales = {};
context.keys().forEach(key => {
const isTheSameOfCurrentLang = key.indexOf(currentLanguage) >= 0;
const isADefaultLang = key.indexOf(`${getDefaultLanguage()}/`) >= 0;
if (isTheSameOfCurrentLang) {
currentLangList[getCleanKeyForNameSpaces(key)] = context(key);
}
if (isADefaultLang) {
defaultLangList[getCleanKeyForDefaults(key)] = context(key);
}
locales[getCleanKeyForNameSpaces(key)] = {
...locales[getCleanKeyForNameSpaces(key)],
[getNameSpaceString(key)]: context(key),
};
});
locales[currentLanguage] = { ...currentLangList };
locales[getDefaultLanguage()] = { ...defaultLangList };
return locales;
}
console.log(getLocales(), ' > ', getDefaultLanguage());
// console.log(getLocales(), currentLanguage, getDefaultLanguage());
i18n.use(initReactI18next).init({
// resources: getLocales(),
resources: getLocales(),
fallbackLng: getDefaultLanguage(),
lng: currentLanguage,
@@ -65,6 +57,9 @@ i18n.use(initReactI18next).init({
escapeValue: false,
},
fallbackNS: ['common'],
defaultNS: 'common',
react: {
wait: true,
},
-6
View File
@@ -1,6 +0,0 @@
{
"common": {
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome"
}
}
+3
View File
@@ -0,0 +1,3 @@
{
"fps": "ingles fps"
}
File renamed without changes.
+4
View File
@@ -0,0 +1,4 @@
{
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
View File
@@ -0,0 +1,3 @@
{
"fps": "ES RAIZ cps"
}
File renamed without changes.
+3
View File
@@ -0,0 +1,3 @@
{
"fps": "MX cps"
}
+7
View File
@@ -0,0 +1,7 @@
{
"header_research_use": "SOLO USO DE INVESTIGACIÓN",
"header_options": "Opciones",
"header_about": "Sobre",
"header_preferences": "Preferencias",
"header_study_list": "Lista de estudio"
}
File renamed without changes.
File renamed without changes.
+5
View File
@@ -0,0 +1,5 @@
{
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome",
"test": "Lunch time"
}