@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
@@ -0,0 +1,4 @@
|
||||
# @ohif/i18n
|
||||
|
||||
More information available at
|
||||
[OHIF Docs](https://docs.ohif.org/essentials/translating.html).
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ohif/i18n",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.6",
|
||||
"description": "OHIF extension for internationalization",
|
||||
"author": "OHIF",
|
||||
"license": "MIT",
|
||||
@@ -48,7 +48,6 @@
|
||||
"rollup": "^1.1.2",
|
||||
"rollup-plugin-babel": "^4.2.0",
|
||||
"rollup-plugin-commonjs": "^9.2.0",
|
||||
"rollup-plugin-copy": "^2.0.1",
|
||||
"rollup-plugin-node-builtins": "^2.1.2",
|
||||
"rollup-plugin-node-resolve": "^4.0.0",
|
||||
"rollup-plugin-peer-deps-external": "^2.2.0",
|
||||
|
||||
@@ -7,7 +7,6 @@ import url from 'rollup-plugin-url';
|
||||
import pkg from './package.json';
|
||||
// Deal with https://github.com/rollup/rollup-plugin-commonjs/issues/297
|
||||
import builtins from 'rollup-plugin-node-builtins';
|
||||
import copy from 'rollup-plugin-copy';
|
||||
import json from 'rollup-plugin-json';
|
||||
|
||||
const globals = {
|
||||
@@ -16,9 +15,9 @@ const globals = {
|
||||
'react-redux': 'ReactRedux',
|
||||
'react-resize-detector': 'ReactResizeDetector',
|
||||
'prop-types': 'PropTypes',
|
||||
'i18next': 'i18next',
|
||||
i18next: 'i18next',
|
||||
'react-i18next': 'react-i18next',
|
||||
'i18next-browser-languagedetector': 'LngDetector'
|
||||
'i18next-browser-languagedetector': 'LngDetector',
|
||||
};
|
||||
|
||||
export default {
|
||||
@@ -46,16 +45,12 @@ export default {
|
||||
postcss({
|
||||
modules: false,
|
||||
}),
|
||||
copy({
|
||||
targets: ['src/locales'],
|
||||
outputFolder: 'dist',
|
||||
}),
|
||||
json({
|
||||
// ignores indent and generates the smallest code
|
||||
compact: true, // Default: false
|
||||
|
||||
// generate a named export for every property of the JSON object
|
||||
namedExports: true // Default: true
|
||||
namedExports: true, // Default: true
|
||||
}),
|
||||
url(),
|
||||
babel({
|
||||
@@ -68,5 +63,5 @@ export default {
|
||||
include: ['node_modules/**', '.yalc/**'],
|
||||
}),
|
||||
],
|
||||
external: Object.keys(pkg.peerDependencies || {})
|
||||
external: Object.keys(pkg.peerDependencies || {}),
|
||||
};
|
||||
@@ -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