i18n - extension extraction
This commit is contained in:
1 parent
376b2c518a
commit
543d8d33c1
17 files changed
+4236
-2
No files matched your search
Executable
+70
@@ -0,0 +1,70 @@
|
||||
import i18n from 'i18next';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
|
||||
const currentLanguage = process.env.APP_LANG || 'en-US';
|
||||
|
||||
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 getCleanKeyForDefaults(key) {
|
||||
const regexKeyCleaner = new RegExp(
|
||||
`^(\\.\\/${getDefaultLanguage()}\\/)(.*.)(\\.json)$`
|
||||
);
|
||||
const cleanedKey = key.match(regexKeyCleaner);
|
||||
return cleanedKey !== null ? cleanedKey[2] : null;
|
||||
}
|
||||
|
||||
function getLocales() {
|
||||
const currentLangList = {};
|
||||
const defaultLangList = {};
|
||||
const locales = {};
|
||||
const context = require.context(`./locales`, true, /\.json$/);
|
||||
|
||||
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[currentLanguage] = { ...currentLangList };
|
||||
locales[getDefaultLanguage()] = { ...defaultLangList };
|
||||
return locales;
|
||||
}
|
||||
|
||||
// console.log(getLocales(), currentLanguage, getDefaultLanguage());
|
||||
|
||||
i18n.use(initReactI18next).init({
|
||||
resources: getLocales(),
|
||||
fallbackLng: getDefaultLanguage(),
|
||||
lng: currentLanguage,
|
||||
debug: true,
|
||||
|
||||
// have a common namespace used around the full app
|
||||
keySeparator: false, // uses content as keys
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
|
||||
react: {
|
||||
wait: true,
|
||||
},
|
||||
});
|
||||
|
||||
export default i18n;
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from './i18n';
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"header_test": "Joao Grandao"
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"common": {
|
||||
"Welcome to React": "Welcome to React and react-i18next",
|
||||
"welcomeMsg": "welcome"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"header_test": "$t(common:test)"
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"tests___": "test common"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"header_test": "$t(common:test)"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"Welcome to React": "Welcome to React and react-i18next",
|
||||
"welcomeMsg": "welcome",
|
||||
"test": "Lunch time"
|
||||
}
|
||||
Reference in new issue
Block a user