Getting a feel for desired multi-language approach #424

This commit is contained in:
James Barnsley
2020-07-04 21:39:30 +12:00
parent 4e44e31136
commit 78ec0eff26
17 changed files with 2491 additions and 272 deletions

54
src/js/locale/index.js Executable file
View File

@ -0,0 +1,54 @@
import React, { Fragment } from 'react';
import { useSelector } from 'react-redux';
import get from 'lodash/get';
import dictionaries from './dictionaries';
import { titleCase } from '../util/helpers';
const PARAMS_REG_EXP = '%{(.*?)}';
const paramsRegExp = new RegExp(PARAMS_REG_EXP, 'g');
const content = (path, params = {}, transform, dictionary) => {
let value = get((dictionary || dictionaries.en), path, '');
value = value.replace(
paramsRegExp,
(replaceText, key) => params.hasOwnProperty(key) ? params[key] : '',
);
switch (transform) {
case 'upper':
value = value.toUpperCase();
break;
case 'lower':
value = value.toLowerCase();
break;
case 'title':
value = titleCase(value);
break;
default:
break;
}
return value;
};
const Content = ({ path, params = {}, transform, children }) => {
const language = useSelector((state) => state.ui.language);
const dictionary = dictionaries[language];
return (
<Fragment>
{content(path, params, transform, dictionary)}
{children}
</Fragment>
);
}
export default {
Content,
content,
};
export {
Content,
content,
};