2018-03-22 03:50:25 +00:00
|
|
|
import TranslateMarkdown from 'components/Translate';
|
2017-09-25 02:06:28 +00:00
|
|
|
import React from 'react';
|
2018-03-22 03:50:25 +00:00
|
|
|
import { State as ConfigState } from 'reducers/config';
|
|
|
|
import { loadStatePropertyOrEmptyObject } from 'utils/localStorage';
|
2017-09-25 02:06:28 +00:00
|
|
|
const fallbackLanguage = 'en';
|
2018-03-07 23:36:05 +00:00
|
|
|
const repository: {
|
|
|
|
[language: string]: {
|
|
|
|
[translationName: string]: string;
|
|
|
|
};
|
|
|
|
} = {};
|
2017-05-25 00:06:30 +00:00
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
interface ILanguage {
|
|
|
|
code: string;
|
|
|
|
data: {
|
|
|
|
[translationName: string]: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const languages: ILanguage[] = [
|
2018-03-22 03:50:25 +00:00
|
|
|
require('./lang/en.json'),
|
2017-10-14 19:02:40 +00:00
|
|
|
require('./lang/de.json'),
|
|
|
|
require('./lang/el.json'),
|
|
|
|
require('./lang/es.json'),
|
|
|
|
require('./lang/fi.json'),
|
|
|
|
require('./lang/fr.json'),
|
|
|
|
require('./lang/ht.json'),
|
|
|
|
require('./lang/hu.json'),
|
|
|
|
require('./lang/id.json'),
|
|
|
|
require('./lang/it.json'),
|
|
|
|
require('./lang/ja.json'),
|
|
|
|
require('./lang/nl.json'),
|
|
|
|
require('./lang/no.json'),
|
|
|
|
require('./lang/pl.json'),
|
|
|
|
require('./lang/pt.json'),
|
|
|
|
require('./lang/ru.json') /*sk, sl, sv */,
|
|
|
|
require('./lang/ko.json'),
|
|
|
|
require('./lang/tr.json'),
|
|
|
|
require('./lang/vi.json'),
|
|
|
|
require('./lang/zhcn.json'),
|
|
|
|
require('./lang/zhtw.json')
|
2017-05-25 00:06:30 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
languages.forEach(l => {
|
2017-07-04 03:21:19 +00:00
|
|
|
repository[l.code] = l.data;
|
2017-05-25 00:06:30 +00:00
|
|
|
});
|
|
|
|
|
2017-06-01 20:45:00 +00:00
|
|
|
export function getTranslators() {
|
2017-07-04 03:21:19 +00:00
|
|
|
return [
|
|
|
|
'TranslatorName_1',
|
|
|
|
'TranslatorName_2',
|
|
|
|
'TranslatorName_3',
|
|
|
|
'TranslatorName_4',
|
|
|
|
'TranslatorName_5'
|
|
|
|
].filter(x => {
|
2018-03-22 03:50:25 +00:00
|
|
|
const translated = translateRaw(x);
|
2017-07-04 03:21:19 +00:00
|
|
|
return !!translated;
|
|
|
|
});
|
2017-06-01 20:45:00 +00:00
|
|
|
}
|
2017-08-28 18:05:38 +00:00
|
|
|
|
2018-03-22 03:50:25 +00:00
|
|
|
export type TranslatedText = React.ReactElement<any> | string;
|
2017-08-28 18:05:38 +00:00
|
|
|
|
2018-03-22 03:50:25 +00:00
|
|
|
export function translateRaw(key: string, variables?: { [name: string]: string }): string {
|
|
|
|
// redux store isn't initialized in time which throws errors, instead we get the language selection from localstorage
|
|
|
|
const lsConfig = loadStatePropertyOrEmptyObject('config');
|
|
|
|
const language = !!lsConfig ? (lsConfig as ConfigState).meta.languageSelection : fallbackLanguage;
|
|
|
|
const translatedString =
|
|
|
|
(repository[language] && repository[language][key]) || repository[fallbackLanguage][key] || key;
|
2017-08-28 18:05:38 +00:00
|
|
|
|
2018-03-22 03:50:25 +00:00
|
|
|
if (!!variables) {
|
|
|
|
// Find each variable and replace it in the translated string
|
|
|
|
let str = translatedString;
|
|
|
|
Object.keys(variables).forEach(v => {
|
|
|
|
str = str.replace(v, variables[v]);
|
|
|
|
});
|
|
|
|
return str;
|
|
|
|
}
|
2018-02-05 23:25:01 +00:00
|
|
|
|
2018-03-22 03:50:25 +00:00
|
|
|
return translatedString;
|
|
|
|
}
|
2017-08-28 18:05:38 +00:00
|
|
|
|
2018-03-22 03:50:25 +00:00
|
|
|
export function translate(
|
|
|
|
key: string,
|
|
|
|
variables?: { [name: string]: string }
|
|
|
|
): React.ReactElement<any> {
|
|
|
|
return <TranslateMarkdown source={translateRaw(key, variables)} />;
|
2017-08-28 18:05:38 +00:00
|
|
|
}
|
2018-03-22 03:50:25 +00:00
|
|
|
|
|
|
|
export default translate;
|