diff options
author | Fabio Berger <me@fabioberger.com> | 2018-02-21 06:11:19 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-02-21 06:11:19 +0800 |
commit | c4f65681a160c4e62d947ba22b81246ac0b2c6a4 (patch) | |
tree | 3fa21d23e8b839a3d5bd2a7d1e20483dff3743ed /packages/website/ts/utils | |
parent | f600226aa931561a2f5fa7a921046348c2fb8f01 (diff) | |
download | dexon-sol-tools-c4f65681a160c4e62d947ba22b81246ac0b2c6a4.tar.gz dexon-sol-tools-c4f65681a160c4e62d947ba22b81246ac0b2c6a4.tar.zst dexon-sol-tools-c4f65681a160c4e62d947ba22b81246ac0b2c6a4.zip |
Add translation infra and replace english text with calls to translate
Diffstat (limited to 'packages/website/ts/utils')
-rw-r--r-- | packages/website/ts/utils/translate.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/website/ts/utils/translate.ts b/packages/website/ts/utils/translate.ts new file mode 100644 index 000000000..cddfef088 --- /dev/null +++ b/packages/website/ts/utils/translate.ts @@ -0,0 +1,54 @@ +import * as _ from 'lodash'; +import { english } from 'ts/translations/english'; +import { Deco, Key, Language } from 'ts/types'; + +const languageToTranslations = { + [Language.English]: english, +}; + +interface Translation { + [key: string]: string; +} + +export class Translate { + private _selectedLanguage: Language; + private _translation: Translation; + constructor() { + this.setLanguage(Language.English); + } + public setLanguage(language: Language) { + const isLanguageSupported = !_.isUndefined(languageToTranslations[language]); + if (!isLanguageSupported) { + throw new Error(`${language} not supported`); + } + this._selectedLanguage = language; + this._translation = languageToTranslations[language]; + } + public get(key: Key, decoration?: Deco) { + let text = this._translation[key]; + if (!_.isUndefined(decoration)) { + switch (decoration) { + case Deco.Cap: + text = this._capitalize(text); + break; + + case Deco.Upper: + text = text.toUpperCase(); + break; + + case Deco.CapWords: + const words = text.split(' '); + const capitalizedWords = _.map(words, w => this._capitalize(w)); + text = capitalizedWords.join(' '); + break; + + default: + throw new Error(`Unrecognized decoration: ${decoration}`); + } + } + return text; + } + private _capitalize(text: string) { + return `${text.charAt(0).toUpperCase()}${text.slice(1)}`; + } +} |