i18n - extension extraction

This commit is contained in:
romulo bordezani 2019-06-03 19:56:59 -03:00
parent aff74bcee4
commit d678a617dc
17 changed files with 58 additions and 44 deletions

View File

@ -93,7 +93,7 @@ class Header extends Component {
</button> </button>
<button <button
className="research-use" className="research-use"
onClick={() => this.changeLanguage('en-ES')} onClick={() => this.changeLanguage('es-MX')}
> >
ES ES
</button> </button>

View File

@ -1,59 +1,51 @@
import i18n from 'i18next'; import i18n from 'i18next';
import { initReactI18next } from 'react-i18next'; import { initReactI18next } from 'react-i18next';
const currentLanguage = process.env.APP_LANG || 'en-US'; const currentLanguage = process.env.APP_LANG || 'es-MX';
function getDefaultLanguage() { function getDefaultLanguage() {
const mainLanguage = currentLanguage.match(/(.*.)(-)/); const mainLanguage = currentLanguage.match(/(.*.)(-)/);
return mainLanguage !== null ? mainLanguage[1] : null; return mainLanguage !== null ? mainLanguage[1] : null;
} }
function getCleanKeyForNameSpaces(key) { function getNameSpaceString(key) {
const regexKeyCleaner = new RegExp( const nameSpaceMatcher = key.match(/[^/]+$/g);
`^(\\.\\/${currentLanguage}\\/)(.*.)(\\.json)$` let finalNameSpace;
);
const cleanedKey = key.match(regexKeyCleaner); if (nameSpaceMatcher !== null) {
return cleanedKey !== null ? cleanedKey[2] : null; finalNameSpace = nameSpaceMatcher[0].replace('.json', '');
}
return finalNameSpace;
} }
function getCleanKeyForDefaults(key) { function getCleanKeyForNameSpaces(key) {
const regexKeyCleaner = new RegExp( const cleanedKey = key.match(/[/\\].+(?=[/\\])/);
`^(\\.\\/${getDefaultLanguage()}\\/)(.*.)(\\.json)$` let finalKey;
);
const cleanedKey = key.match(regexKeyCleaner); if (cleanedKey !== null) {
return cleanedKey !== null ? cleanedKey[2] : null; finalKey = cleanedKey[0].replace(/[/\\]/, '');
finalKey = finalKey.replace(/[/\\]/, '-');
}
return finalKey;
} }
function getLocales() { function getLocales() {
const currentLangList = {};
const defaultLangList = {};
const locales = {};
const context = require.context(`./locales`, true, /\.json$/); const context = require.context(`./locales`, true, /\.json$/);
const locales = {};
context.keys().forEach(key => { context.keys().forEach(key => {
const isTheSameOfCurrentLang = key.indexOf(currentLanguage) >= 0; locales[getCleanKeyForNameSpaces(key)] = {
const isADefaultLang = key.indexOf(`${getDefaultLanguage()}/`) >= 0; ...locales[getCleanKeyForNameSpaces(key)],
[getNameSpaceString(key)]: context(key),
if (isTheSameOfCurrentLang) { };
currentLangList[getCleanKeyForNameSpaces(key)] = context(key);
}
if (isADefaultLang) {
defaultLangList[getCleanKeyForDefaults(key)] = context(key);
}
}); });
locales[currentLanguage] = { ...currentLangList };
locales[getDefaultLanguage()] = { ...defaultLangList };
return locales; return locales;
} }
console.log(getLocales(), ' > ', getDefaultLanguage());
// console.log(getLocales(), currentLanguage, getDefaultLanguage());
i18n.use(initReactI18next).init({ i18n.use(initReactI18next).init({
// resources: getLocales(),
resources: getLocales(), resources: getLocales(),
fallbackLng: getDefaultLanguage(), fallbackLng: getDefaultLanguage(),
lng: currentLanguage, lng: currentLanguage,
@ -65,6 +57,9 @@ i18n.use(initReactI18next).init({
escapeValue: false, escapeValue: false,
}, },
fallbackNS: ['common'],
defaultNS: 'common',
react: { react: {
wait: true, wait: true,
}, },

View File

@ -1,6 +0,0 @@
{
"common": {
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome"
}
}

View File

@ -0,0 +1,3 @@
{
"fps": "ingles fps"
}

4
src/locales/en/UK/common.json Executable file
View File

@ -0,0 +1,4 @@
{
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome"
}

View File

@ -0,0 +1,3 @@
{
"fps": "ES RAIZ cps"
}

View File

@ -0,0 +1,3 @@
{
"fps": "MX cps"
}

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"
}

5
src/locales/es/common.json Executable file
View File

@ -0,0 +1,5 @@
{
"Welcome to React": "Welcome to React and react-i18next",
"welcomeMsg": "welcome",
"test": "Lunch time"
}

View File

@ -11782,13 +11782,12 @@ react-transition-group@^2.0.0, react-transition-group@^2.2.0:
prop-types "^15.6.2" prop-types "^15.6.2"
react-lifecycles-compat "^3.0.4" react-lifecycles-compat "^3.0.4"
react-viewerbase@0.4.2: "react-viewerbase@file:.yalc/react-viewerbase":
version "0.4.2" version "0.2.17-1a8178ff"
resolved "https://registry.yarnpkg.com/react-viewerbase/-/react-viewerbase-0.4.2.tgz#a890db450c0f6d1ec495dc2ceb85dbe38fcb7fda"
integrity sha512-1rqdXZ3gQIuRtuxOTKwg9D4EJ1NJ8ug9mQWOAFxx+7PSFhfbjXn55O4Fko92qXrlKqd+Rba1jqBvzTXPr0h0fQ==
dependencies: dependencies:
"@babel/runtime" "7.2.0" "@babel/runtime" "7.2.0"
classnames "2.2.6" classnames "2.2.6"
i18next "^15.1.3"
lodash.isequal "4.5.0" lodash.isequal "4.5.0"
moment "2.24.0" moment "2.24.0"
prop-types "15.6.2" prop-types "15.6.2"
@ -11796,6 +11795,7 @@ react-viewerbase@0.4.2:
react-dates "18.4.1" react-dates "18.4.1"
react-dnd "7.0.2" react-dnd "7.0.2"
react-dnd-html5-backend "7.0.2" react-dnd-html5-backend "7.0.2"
react-i18next "^10.11.0"
react-with-direction "1.3.0" react-with-direction "1.3.0"
react-vtkjs-viewport@^0.0.7: react-vtkjs-viewport@^0.0.7: