Skip to content

Commit

Permalink
Convert translation to typescript (#74)
Browse files Browse the repository at this point in the history
* Convert translation to typescript

* update input types

* update travis

* update node version
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 17, 2021
1 parent 08898b0 commit dbfdd8c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js

node_js:
- 10.7
- '10.10'

cache:
- npm: true
Expand All @@ -15,7 +15,7 @@ install:
- yarn install

before_script:
- yarn run build
- yarn build

script:
- yarn test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Jed from 'jed';
import Jed, { LanguagePack } from 'jed';

const DEFAULT_LANGUAGE_PACK = {
domain: 'superset',
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 };

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { configure, t, tn } from './TranslatorSingleton';
export { TranslatorConfig, LanguagePack } from './Translator';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Translator from '../src/Translator';
import languagePackZh from './languagePacks/zh.json';
import languagePackZh from './languagePacks/zh';

describe('Translator', () => {
it('exists', () => {
Expand All @@ -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');
});
Expand All @@ -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('秒');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit dbfdd8c

Please sign in to comment.