@ohif-i18n - Replace use of context to import locales (#575)
This commit is contained in:
1 parent
6548dffb42
commit
d04bb323f7
25 files changed
+1625
-1755
No files matched your search
@@ -4,64 +4,25 @@ import LngDetector from 'i18next-browser-languagedetector';
|
||||
import customDebug from './debugger';
|
||||
import pkg from '../package.json';
|
||||
import { debugMode, detectionOptions } from './config';
|
||||
import locales from './locales';
|
||||
|
||||
let translate;
|
||||
|
||||
function getNameSpaceString(key) {
|
||||
const nameSpaceMatcher = key.match(/[^/]+$/g);
|
||||
let finalNameSpace;
|
||||
function addLocales(newLocales) {
|
||||
customDebug(`Adding locales ${newLocales}`, 'info');
|
||||
|
||||
if (nameSpaceMatcher !== null) {
|
||||
finalNameSpace = nameSpaceMatcher[0].replace('.json', '');
|
||||
}
|
||||
let resourceBundle = [];
|
||||
|
||||
return finalNameSpace;
|
||||
}
|
||||
|
||||
function getKeyForNameSpaces(key) {
|
||||
const cleanedKey = key.match(/[/\\].+(?=[/\\])/);
|
||||
let finalKey;
|
||||
|
||||
if (cleanedKey !== null) {
|
||||
finalKey = cleanedKey[0].replace(/[/\\]/, '');
|
||||
finalKey = finalKey.replace(/[/\\]/, '-');
|
||||
}
|
||||
|
||||
return finalKey;
|
||||
}
|
||||
|
||||
function getLocales() {
|
||||
var isTestEnvironment = process.env.NODE_ENV === 'test';
|
||||
|
||||
// require.context is exclusive from webpack. This conditional is needed to escape while running tests
|
||||
if (isTestEnvironment) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const context = require.context(`./locales`, true, /\.json$/);
|
||||
const locales = {};
|
||||
|
||||
context.keys().forEach(key => {
|
||||
locales[getKeyForNameSpaces(key)] = {
|
||||
...locales[getKeyForNameSpaces(key)],
|
||||
[getNameSpaceString(key)]: context(key),
|
||||
};
|
||||
Object.keys(newLocales).map(key => {
|
||||
Object.keys(newLocales[key]).map(namespace => {
|
||||
const locale = newLocales[key][namespace];
|
||||
resourceBundle.push({ key, namespace, locale });
|
||||
i18n.addResourceBundle(key, namespace, locale, true, true);
|
||||
});
|
||||
});
|
||||
|
||||
return locales;
|
||||
}
|
||||
|
||||
function addLocales(context) {
|
||||
context.keys().forEach(key => {
|
||||
i18n.addResourceBundle(
|
||||
getKeyForNameSpaces(key),
|
||||
getNameSpaceString(key),
|
||||
context(key),
|
||||
true,
|
||||
true
|
||||
);
|
||||
});
|
||||
customDebug(`Locales added successfully`, 'info');
|
||||
customDebug(resourceBundle, 'info');
|
||||
}
|
||||
|
||||
function initI18n(detection = detectionOptions) {
|
||||
@@ -69,7 +30,7 @@ function initI18n(detection = detectionOptions) {
|
||||
.use(LngDetector)
|
||||
.use(initReactI18next)
|
||||
.init({
|
||||
resources: getLocales(),
|
||||
resources: locales,
|
||||
debug: debugMode,
|
||||
keySeparator: false,
|
||||
interpolation: {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import Header from './Header.json';
|
||||
|
||||
export default {
|
||||
'en-UK': {
|
||||
Header,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import Header from './Header.json';
|
||||
|
||||
export default {
|
||||
'en-US': {
|
||||
Header,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
import Buttons from './Buttons.json';
|
||||
import CineDialog from './CineDialog.json';
|
||||
import Common from './Common.json';
|
||||
import Header from './Header.json';
|
||||
import MeasurementTable from './MeasurementTable.json';
|
||||
import UserPreferencesModal from './UserPreferencesModal.json';
|
||||
|
||||
/** Sublanguages */
|
||||
import en_US from './US';
|
||||
import en_UK from './UK';
|
||||
|
||||
export default {
|
||||
en: {
|
||||
Buttons,
|
||||
CineDialog,
|
||||
Common,
|
||||
Header,
|
||||
MeasurementTable,
|
||||
UserPreferencesModal,
|
||||
},
|
||||
...en_US,
|
||||
...en_UK,
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import Header from './Header.json';
|
||||
|
||||
export default {
|
||||
'es-AR': {
|
||||
Header,
|
||||
},
|
||||
};
|
||||
@@ -38,5 +38,7 @@
|
||||
"Manual": "Manual",
|
||||
"Save": "Guardar",
|
||||
"Reset to Defaults": "$t(Common:reset) por defectos",
|
||||
"Cancel": "Cancelar"
|
||||
"Cancel": "Cancelar",
|
||||
"2D MPR": "2D MPR",
|
||||
"WWWC": "WWWC"
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"reset": "Reiniciar",
|
||||
"Reset": "Reiniciar",
|
||||
"Previous": "Anterior",
|
||||
"Next": "Próximo",
|
||||
"Play": "Play",
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import Header from './Header.json';
|
||||
|
||||
export default {
|
||||
'es-MX': {
|
||||
Header,
|
||||
},
|
||||
};
|
||||
@@ -7,5 +7,6 @@
|
||||
"NonTargets": "NonObjetivos",
|
||||
"MAX": "máximo",
|
||||
"Chest Wall Posterior": "Pared pectoral posterior",
|
||||
"Bone Extremity": "Extremidad ósea"
|
||||
"Bone Extremity": "Extremidad ósea",
|
||||
"Measurements": "Mediciones"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import Buttons from './Buttons.json';
|
||||
import CineDialog from './CineDialog.json';
|
||||
import Common from './Common.json';
|
||||
import Header from './Header.json';
|
||||
import MeasurementTable from './MeasurementTable.json';
|
||||
import UserPreferencesModal from './UserPreferencesModal.json';
|
||||
|
||||
/** Sublanguages */
|
||||
import es_AR from './AR';
|
||||
import es_MX from './MX';
|
||||
|
||||
export default {
|
||||
es: {
|
||||
Buttons,
|
||||
CineDialog,
|
||||
Common,
|
||||
Header,
|
||||
MeasurementTable,
|
||||
UserPreferencesModal,
|
||||
},
|
||||
...es_AR,
|
||||
...es_MX,
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import en from './en';
|
||||
import es from './es';
|
||||
|
||||
export default {
|
||||
...en,
|
||||
...es,
|
||||
};
|
||||
Reference in new issue
Block a user