Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Convert translation to typescript #74

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this shorthand for yarn run build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, yarn xxx = npm run xxx


script:
- yarn test
Expand Down
4 changes: 2 additions & 2 deletions package.json
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 };
1 change: 0 additions & 1 deletion packages/superset-ui-translation/src/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/superset-ui-translation/src/index.ts
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
14 changes: 0 additions & 14 deletions packages/superset-ui-translation/test/languagePacks/en.json

This file was deleted.

18 changes: 18 additions & 0 deletions packages/superset-ui-translation/test/languagePacks/en.ts
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;
14 changes: 0 additions & 14 deletions packages/superset-ui-translation/test/languagePacks/zh.json

This file was deleted.

18 changes: 18 additions & 0 deletions packages/superset-ui-translation/test/languagePacks/zh.ts
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;
22 changes: 22 additions & 0 deletions packages/superset-ui-translation/types/external.d.ts
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;
}
}