diff options
author | Hsuan Lee <boczeratul@gmail.com> | 2019-03-06 17:46:50 +0800 |
---|---|---|
committer | Hsuan Lee <boczeratul@gmail.com> | 2019-03-06 17:46:50 +0800 |
commit | 35703539d0f2b4ddb3b11d0de8c9634af59ab71f (patch) | |
tree | ae3731221dbbb3a6fa40060a8d916cfd3f738289 /packages/website/ts/utils/translate.ts | |
parent | 92a1fde5b1ecd81b07cdb5bf0c9c1cd3544799db (diff) | |
download | dexon-0x-contracts-stable.tar.gz dexon-0x-contracts-stable.tar.zst dexon-0x-contracts-stable.zip |
Deploy @dexon-foundation/0x.jsstable
Diffstat (limited to 'packages/website/ts/utils/translate.ts')
-rw-r--r-- | packages/website/ts/utils/translate.ts | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/packages/website/ts/utils/translate.ts b/packages/website/ts/utils/translate.ts deleted file mode 100644 index af5c766a9..000000000 --- a/packages/website/ts/utils/translate.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as _ from 'lodash'; -import { Deco, Key, Language } from 'ts/types'; - -import chinese from '../../translations/chinese.json'; -import english from '../../translations/english.json'; -import korean from '../../translations/korean.json'; -import russian from '../../translations/russian.json'; -import spanish from '../../translations/spanish.json'; - -const languageToTranslations = { - [Language.English]: english, - [Language.Spanish]: spanish, - [Language.Chinese]: chinese, - [Language.Korean]: korean, - [Language.Russian]: russian, -}; - -const languagesWithoutCaps = [Language.Chinese, Language.Korean]; - -interface Translation { - [key: string]: string; -} - -export class Translate { - private _selectedLanguage: Language; - private _translation: Translation; - constructor(desiredLanguage?: Language) { - if (!_.isUndefined(desiredLanguage)) { - this.setLanguage(desiredLanguage); - return; - } - const browserLanguage = (window.navigator as any).userLanguage || window.navigator.language || 'en-US'; - let language = Language.English; - if (_.includes(browserLanguage, 'es-')) { - language = Language.Spanish; - } else if (_.includes(browserLanguage, 'zh-')) { - language = Language.Chinese; - } else if (_.includes(browserLanguage, 'ko-')) { - language = Language.Korean; - } else if (_.includes(browserLanguage, 'ru-')) { - language = Language.Russian; - } - this.setLanguage(language); - } - public getLanguage(): Language { - return this._selectedLanguage; - } - public setLanguage(language: Language): void { - 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): string { - let text = this._translation[key]; - // if a translation does not exist for a certain language, fallback to english - // if it still doesn't exist in english, throw an error - if (_.isUndefined(text)) { - const englishTranslation: Translation = languageToTranslations[Language.English]; - const englishText = englishTranslation[key]; - if (!_.isUndefined(englishText)) { - text = englishText; - } else { - throw new Error( - `Translation key not available in ${this._selectedLanguage} or ${Language.English}: ${key}`, - ); - } - } - if (!_.isUndefined(decoration) && !_.includes(languagesWithoutCaps, this._selectedLanguage)) { - 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: string, i: number) => { - if (w.length === 1) { - return w; - } - return this._capitalize(w); - }); - text = capitalizedWords.join(' '); - break; - - default: - throw new Error(`Unrecognized decoration: ${decoration}`); - } - } - return text; - } - private _capitalize(text: string): string { - return `${text.charAt(0).toUpperCase()}${text.slice(1)}`; - } -} |