MyCrypto/common/translations/index.tsx

89 lines
2.4 KiB
TypeScript
Raw Normal View History

import TranslateMarkdown from 'components/Translate';
Migrate to Typescript (#224) * Refactor babel/types * Refactor entry point * Refactor actions * Refactor api * Full project refactor -- Broad type fixing sweep * - completely fix merge conflicts - handle various type errors * Add tslint to package.json * Dependency cleanup * Fix module resolution * Work on type definitions for untyped libs * progress commit * Add more definition typing * various type additions * Add unit types * Fix sagaiterator + unit types * various types added * additional type additions * Fix typing on Sagas * remove flowfixmes; swap translate for translateRaw * Get rid of contracts - awaiting Henry's contract PR * Remove contracts from routing * Fix most of actions/reducers * refactor actions directory structure * fix reducer action type imports * Fix most of type errors pre-actions refactor * fix action creator imports in containers * Refactor more * Refactor index of actions * fix action imports; use module level index export * package-lock.json updated * Use action types in props * Type up action creators * Fix most of connect errors * Typefixing progress * More types * Fix run-time errors * Caching improvements for webpack * Remove path resolve from webpack * Update non-breaking packages to latest version * Fix token typing * Remove unused color code * Fix wallet decrypt dispatch * Set redux-form related props/functions to ANY, since we're stripping it out later on * Revert BigNumber.js package changes * Extend window to custom object for Perf * Format Navigation * Typecase keystore errors as any (since we shouldnt touch this) * Push wallet context fix * - find/replace value->payload in swap - properly type swap state properties - extract inline reducer into reducer function * - type local storage retrieved items as generic * - bind all RPCClient methods with fat arrow * - reformat * Change to enums for constants * Change state into any * Fix swap errors * ensure that seconds are passed into state as integers * Fix rest of errors * use parseInt explicitly instead of type coercion * Fix derivation-checker, remove flow command, add tslint command, add tslint-react, tell travis to use tslint instead of flow. * Whoops, remove those tests. * Remove unsupported (yet) config option. * Fix precommit to target ts and tsx files. * Fix some errors, ignore some silly rules. * Revert jest to v19, use ts-jest and make all tests typescript. Fixes all but one. * Get rid of saga tests * Fix tslint errors
2017-09-25 02:06:28 +00:00
import React from 'react';
import { State as ConfigState } from 'reducers/config';
import { loadStatePropertyOrEmptyObject } from 'utils/localStorage';
Migrate to Typescript (#224) * Refactor babel/types * Refactor entry point * Refactor actions * Refactor api * Full project refactor -- Broad type fixing sweep * - completely fix merge conflicts - handle various type errors * Add tslint to package.json * Dependency cleanup * Fix module resolution * Work on type definitions for untyped libs * progress commit * Add more definition typing * various type additions * Add unit types * Fix sagaiterator + unit types * various types added * additional type additions * Fix typing on Sagas * remove flowfixmes; swap translate for translateRaw * Get rid of contracts - awaiting Henry's contract PR * Remove contracts from routing * Fix most of actions/reducers * refactor actions directory structure * fix reducer action type imports * Fix most of type errors pre-actions refactor * fix action creator imports in containers * Refactor more * Refactor index of actions * fix action imports; use module level index export * package-lock.json updated * Use action types in props * Type up action creators * Fix most of connect errors * Typefixing progress * More types * Fix run-time errors * Caching improvements for webpack * Remove path resolve from webpack * Update non-breaking packages to latest version * Fix token typing * Remove unused color code * Fix wallet decrypt dispatch * Set redux-form related props/functions to ANY, since we're stripping it out later on * Revert BigNumber.js package changes * Extend window to custom object for Perf * Format Navigation * Typecase keystore errors as any (since we shouldnt touch this) * Push wallet context fix * - find/replace value->payload in swap - properly type swap state properties - extract inline reducer into reducer function * - type local storage retrieved items as generic * - bind all RPCClient methods with fat arrow * - reformat * Change to enums for constants * Change state into any * Fix swap errors * ensure that seconds are passed into state as integers * Fix rest of errors * use parseInt explicitly instead of type coercion * Fix derivation-checker, remove flow command, add tslint command, add tslint-react, tell travis to use tslint instead of flow. * Whoops, remove those tests. * Remove unsupported (yet) config option. * Fix precommit to target ts and tsx files. * Fix some errors, ignore some silly rules. * Revert jest to v19, use ts-jest and make all tests typescript. Fixes all but one. * Get rid of saga tests * Fix tslint errors
2017-09-25 02:06:28 +00:00
const fallbackLanguage = 'en';
const repository: {
[language: string]: {
[translationName: string]: string;
};
} = {};
2017-05-25 00:06:30 +00:00
interface ILanguage {
code: string;
data: {
[translationName: string]: string;
};
}
const languages: ILanguage[] = [
require('./lang/en.json'),
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 => {
const translated = translateRaw(x);
2017-07-04 03:21:19 +00:00
return !!translated;
});
2017-06-01 20:45:00 +00:00
}
export type TranslatedText = React.ReactElement<any> | string;
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;
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;
}
return translatedString;
}
export function translate(
key: string,
variables?: { [name: string]: string }
): React.ReactElement<any> {
return <TranslateMarkdown source={translateRaw(key, variables)} />;
}
export default translate;