From 163cafae8ad7afedf724ccca1062b97b2697abec Mon Sep 17 00:00:00 2001 From: Krist Wongsuphasawat Date: Wed, 23 Jan 2019 10:45:34 -0800 Subject: [PATCH] Convert translation to typescript (#74) * Convert translation to typescript * update input types * update travis * update node version --- .../superset-ui/.travis.yml | 4 +-- .../superset-ui/package.json | 4 +-- .../src/{Translator.js => Translator.ts} | 28 +++++++++---------- ...torSingleton.js => TranslatorSingleton.ts} | 14 +++++----- .../superset-ui-translation/src/index.js | 1 - .../superset-ui-translation/src/index.ts | 2 ++ ...{Translator.test.js => Translator.test.ts} | 21 +++----------- ...on.test.js => TranslatorSingleton.test.ts} | 2 +- .../test/{index.test.js => index.test.ts} | 0 .../test/languagePacks/en.json | 14 ---------- .../test/languagePacks/en.ts | 18 ++++++++++++ .../test/languagePacks/zh.json | 14 ---------- .../test/languagePacks/zh.ts | 18 ++++++++++++ .../types/external.d.ts | 22 +++++++++++++++ 14 files changed, 89 insertions(+), 73 deletions(-) rename superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/{Translator.js => Translator.ts} (52%) rename superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/{TranslatorSingleton.js => TranslatorSingleton.ts} (51%) delete mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.js create mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.ts rename superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/{Translator.test.js => Translator.test.ts} (70%) rename superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/{TranslatorSingleton.test.js => TranslatorSingleton.test.ts} (96%) rename superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/{index.test.js => index.test.ts} (100%) delete mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.json create mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.ts delete mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.json create mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.ts create mode 100644 superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/types/external.d.ts diff --git a/superset-frontend/temporary_superset_ui/superset-ui/.travis.yml b/superset-frontend/temporary_superset_ui/superset-ui/.travis.yml index 879ae9cc21b85..f410e3d418572 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/.travis.yml +++ b/superset-frontend/temporary_superset_ui/superset-ui/.travis.yml @@ -1,7 +1,7 @@ language: node_js node_js: - - 10.7 + - '10.10' cache: - npm: true @@ -15,7 +15,7 @@ install: - yarn install before_script: - - yarn run build + - yarn build script: - yarn test diff --git a/superset-frontend/temporary_superset_ui/superset-ui/package.json b/superset-frontend/temporary_superset_ui/superset-ui/package.json index ee3d00254efd7..18d93c97ce43c 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/package.json +++ b/superset-frontend/temporary_superset_ui/superset-ui/package.json @@ -8,8 +8,8 @@ "build": "yarn run build:cjs && yarn run build:esm && yarn run type:dts", "build:cjs": "NODE_ENV=production beemo babel --extensions=\".js,.jsx,.ts,.tsx\" ./src --out-dir lib/ --minify --workspaces=\"@superset-ui/!(demo|generator-superset)\"", "build:esm": "NODE_ENV=production beemo babel --extensions=\".js,.jsx,.ts,.tsx\" ./src --out-dir esm/ --esm --minify --workspaces=\"@superset-ui/!(demo|generator-superset)\"", - "type": "NODE_ENV=production beemo typescript --workspaces=\"@superset-ui/(connection|core|color|chart|number-format)\" --noEmit", - "type:dts": "NODE_ENV=production beemo typescript --workspaces=\"@superset-ui/(connection|core|color|chart|number-format)\" --emitDeclarationOnly", + "type": "NODE_ENV=production beemo typescript --workspaces=\"@superset-ui/(connection|core|color|chart|number-format|translation)\" --noEmit", + "type:dts": "NODE_ENV=production beemo typescript --workspaces=\"@superset-ui/(connection|core|color|chart|number-format|translation)\" --emitDeclarationOnly", "lint": "beemo create-config prettier && beemo eslint \"./packages/*/{src,test,storybook}/**/*.{js,jsx,ts,tsx}\"", "lint:fix": "beemo create-config prettier && beemo eslint --fix \"./packages/*/{src,test,storybook}/**/*.{js,jsx,ts,tsx}\"", "jest": "beemo jest --color --coverage --react", diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.ts similarity index 52% rename from superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.js rename to superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.ts index b0944f4a8cdb4..9bc495f3608bd 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/Translator.ts @@ -1,4 +1,4 @@ -import Jed from 'jed'; +import Jed, { LanguagePack } from 'jed'; const DEFAULT_LANGUAGE_PACK = { domain: 'superset', @@ -13,27 +13,25 @@ const DEFAULT_LANGUAGE_PACK = { }, }; +export { LanguagePack }; + +export interface TranslatorConfig { + languagePack?: LanguagePack; +} + export default class Translator { - constructor({ languagePack = DEFAULT_LANGUAGE_PACK } = {}) { + i18n: Jed; + + constructor(config: TranslatorConfig = {}) { + const { languagePack = DEFAULT_LANGUAGE_PACK } = config; this.i18n = new Jed(languagePack); } - translate(input, ...args) { - if (input === null || input === undefined) { - return input; - } - + translate(input: string, ...args: any[]): string { return this.i18n.translate(input).fetch(...args); } - translateWithNumber(singular, plural, num = 0, ...args) { - if (singular === null || singular === undefined) { - return singular; - } - if (plural === null || plural === undefined) { - return plural; - } - + translateWithNumber(singular: string, plural: string, num: number = 0, ...args: any[]): string { return this.i18n .translate(singular) .ifPlural(num, plural) diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.ts similarity index 51% rename from superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.js rename to superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.ts index 90f8e11991bd9..da855d592c80e 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/TranslatorSingleton.ts @@ -1,10 +1,10 @@ /* eslint no-console: 0 */ -import Translator from './Translator'; +import Translator, { TranslatorConfig } from './Translator'; -let singleton; +let singleton: Translator; let isConfigured = false; -function configure(config) { +function configure(config?: TranslatorConfig) { singleton = new Translator(config); isConfigured = true; @@ -22,12 +22,12 @@ function getInstance() { return singleton; } -function t(...args) { - return getInstance().translate(...args); +function t(input: string, ...args: any[]) { + return getInstance().translate(input, ...args); } -function tn(...args) { - return getInstance().translateWithNumber(...args); +function tn(singular: string, plural: string, ...args: any[]) { + return getInstance().translateWithNumber(singular, plural, ...args); } export { configure, t, tn }; diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.js deleted file mode 100644 index 5ed37f1bbd1b6..0000000000000 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.js +++ /dev/null @@ -1 +0,0 @@ -export { configure, t, tn } from './TranslatorSingleton'; diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.ts b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.ts new file mode 100644 index 0000000000000..492599e2da6fa --- /dev/null +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/src/index.ts @@ -0,0 +1,2 @@ +export { configure, t, tn } from './TranslatorSingleton'; +export { TranslatorConfig, LanguagePack } from './Translator'; diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.ts similarity index 70% rename from superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.js rename to superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.ts index 906fabe50239c..2a5be0975d57f 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/Translator.test.ts @@ -1,5 +1,5 @@ import Translator from '../src/Translator'; -import languagePackZh from './languagePacks/zh.json'; +import languagePackZh from './languagePacks/zh'; describe('Translator', () => { it('exists', () => { @@ -24,12 +24,6 @@ describe('Translator', () => { const translator = new Translator({ languagePack: languagePackZh, }); - it('returns null for null input', () => { - expect(translator.translate(null)).toBeNull(); - }); - it('returns undefined for undefined input', () => { - expect(translator.translate(undefined)).toBeUndefined(); - }); it('returns original text for unknown text', () => { expect(translator.translate('abc')).toEqual('abc'); }); @@ -47,19 +41,12 @@ describe('Translator', () => { const translator = new Translator({ languagePack: languagePackZh, }); - it('returns null if singular or plural is null', () => { - expect(translator.translateWithNumber(null, 'plural')).toBeNull(); - expect(translator.translateWithNumber('singular', null)).toBeNull(); - expect(translator.translateWithNumber(null, null)).toBeNull(); - }); - it('returns undefined if singular or plural is undefined', () => { - expect(translator.translateWithNumber(undefined, 'plural')).toBeUndefined(); - expect(translator.translateWithNumber('singular', undefined)).toBeUndefined(); - expect(translator.translateWithNumber(undefined, undefined)).toBeUndefined(); - }); it('returns original text for unknown text', () => { expect(translator.translateWithNumber('fish', 'fishes', 1)).toEqual('fish'); }); + it('uses 0 as default value', () => { + expect(translator.translateWithNumber('box', 'boxes')).toEqual('boxes'); + }); it('translates simple text', () => { expect(translator.translateWithNumber('second', 'seconds', 1)).toEqual('秒'); }); diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.ts similarity index 96% rename from superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.js rename to superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.ts index 1df9058181dfc..a1df3a159614b 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/TranslatorSingleton.test.ts @@ -2,7 +2,7 @@ import mockConsole from 'jest-mock-console'; import Translator from '../src/Translator'; import { configure, t, tn } from '../src/TranslatorSingleton'; -import languagePackZh from './languagePacks/zh.json'; +import languagePackZh from './languagePacks/zh'; describe('TranslatorSingleton', () => { describe('before configure()', () => { diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/index.test.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/index.test.ts similarity index 100% rename from superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/index.test.js rename to superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/index.test.ts diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.json b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.json deleted file mode 100644 index 07ed948c0817f..0000000000000 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "domain": "superset", - "locale_data": { - "superset": { - "": { - "domain": "superset", - "plural_forms": "nplurals=2; plural=(n != 1)", - "lang": "en" - }, - "second": [""], - "Copy of %s": [""] - } - } -} diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.ts b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.ts new file mode 100644 index 0000000000000..c3113a85f0414 --- /dev/null +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/en.ts @@ -0,0 +1,18 @@ +import { LanguagePack } from 'jed'; + +const languagePack: LanguagePack = { + domain: 'superset', + locale_data: { + superset: { + '': { + domain: 'superset', + plural_forms: 'nplurals=2; plural=(n != 1)', + lang: 'en', + }, + second: [''], + 'Copy of %s': [''], + }, + }, +}; + +export default languagePack; diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.json b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.json deleted file mode 100644 index d93afdbe11f12..0000000000000 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "domain": "superset", - "locale_data": { - "superset": { - "": { - "domain": "superset", - "plural_forms": "nplurals=2; plural=(n != 1)", - "lang": "zh" - }, - "second": ["秒"], - "Copy of %s": ["%s 的副本", "%s 的副本本本"] - } - } -} diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.ts b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.ts new file mode 100644 index 0000000000000..9b368c313c341 --- /dev/null +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/test/languagePacks/zh.ts @@ -0,0 +1,18 @@ +import { LanguagePack } from 'jed'; + +const languagePack: LanguagePack = { + domain: 'superset', + locale_data: { + superset: { + '': { + domain: 'superset', + plural_forms: 'nplurals=2; plural=(n != 1)', + lang: 'zh', + }, + second: ['秒'], + 'Copy of %s': ['%s 的副本', '%s 的副本本本'], + }, + }, +}; + +export default languagePack; diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/types/external.d.ts b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/types/external.d.ts new file mode 100644 index 0000000000000..4ce1f22161a07 --- /dev/null +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-translation/types/external.d.ts @@ -0,0 +1,22 @@ +declare module 'jed' { + export interface LanguagePack { + domain: string; + locale_data: { + superset: { + [key: string]: string[] | { + domain: string; + plural_forms: string; + lang: string; + }; + }; + } + } + + export default class Jed { + constructor(languagepack: LanguagePack); + + translate(input: string): Jed; + ifPlural(value: number, plural: string): Jed; + fetch(...args: any[]): string; + } +}