Skip to content

Commit

Permalink
feat: remove static methods for prior instance method setLang, remove…
Browse files Browse the repository at this point in the history
… default rumLogger (#25)
  • Loading branch information
dihar authored May 24, 2022
1 parent 32ea385 commit 9a07c54
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 110 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Utilities in the I18N package are designed for internationalization of Yandex Cloud UI services.

### Breaking changes in 0.6.0

- Removed static method setDefaultLang, you have to use i18n.setLang instead
- Removed default Rum Logger, you have to connect your own logger from application side
- Removed static property LANGS

### Install

`npm install --save @yandex-cloud/i18n`
Expand Down Expand Up @@ -56,12 +62,12 @@ const i18n = new I18N();
i18n.registerKeysets('ru', ru);
i18n.registerKeysets('en', en);

I18N.setDefaultLang('ru');
i18n.setLang('ru');
console.log(
i18n.i18n('wizard', 'label_error-widget-no-access')
); // -> "Нет доступа к чарту"

I18N.setDefaultLang('en');
i18n.setLang('en');
console.log(
i18n.i18n('wizard', 'label_error-widget-no-access')
); // -> "No access to the chart
Expand All @@ -73,7 +79,7 @@ console.log(
); // -> "No access to the chart"


I18N.setDefaultLang('ru');
i18n.setLang('ru');
console.log(
keyset('label_error-widget-no-access')
); // -> "Нет доступа к чарту"
Expand All @@ -92,7 +98,7 @@ The library supports templating. Templated variables are enclosed in double curl

```json
{
"label_template": "No matches found for "{{inputValue}}" in "{{folderName}}"
"label_template": "No matches found for '{{inputValue}}' in '{{folderName}}'"
}
```

Expand Down
3 changes: 2 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {I18N} from "./index";
import {I18N} from './index';

let i18n: I18N;

Expand Down Expand Up @@ -42,6 +42,7 @@ describe('has', () => {
describe('i18n', () => {
it('should return key when translation missing', () => {
i18n.setLang('ru');
i18n.registerKeyset('ru', 'notification', {});
expect(i18n.i18n('notification', 'title')).toBe('title');
});

Expand Down
66 changes: 17 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
import {pluralize} from "./pluralize";
import {replaceParams} from "./replace-params";
import {rumLogger} from "./rum-logger";
import {Logger, Params, Plural} from "./types";
import {pluralize} from './pluralize';
import {replaceParams} from './replace-params';
import {Logger, Params, Plural} from './types';


type KeysData = Record<string, string | string[]>;
type KeysetData = Record<string, KeysData>;

export * from './types';

export class I18N {
static LANGS: Record<string, string> = {
ru: 'ru',
en: 'en',
};

static defaultLang: string | undefined = undefined;

static setDefaultLang(lang: string) {
if (I18N.LANGS[lang]) {
I18N.defaultLang = lang;
} else {
console.warn('Attempted to set unknown lang as default.');
I18N.defaultLang = I18N.LANGS.ru;
}
}

data: Record<string, KeysetData> = {
[I18N.LANGS.ru]: {},
[I18N.LANGS.en]: {},
};

lang: string | undefined = undefined;

data: Record<string, KeysetData> = {};
lang?: string = undefined;
logger: Logger | null = null;

constructor(options: {logger?: Logger} = {logger: rumLogger}) {
constructor(options?: {logger?: Logger}) {
this.logger = options?.logger || null;
}

setLang(lang: string) {
if (I18N.LANGS[lang]) {
this.lang = lang;
} else {
console.warn('Attempted to set unknown lang.');
this.lang = I18N.LANGS.ru;
}
this.lang = lang;
}

registerKeyset(lang: string, keysetName: string, data: KeysData = {}) {
Expand All @@ -60,28 +34,19 @@ export class I18N {
});
}

has(keysetName: string, key: string) {
const lang = this.lang || I18N.defaultLang;
let languageData: KeysetData | undefined;
if (lang) {
languageData = this.data[lang];
}
has(keysetName: string, key: string, lang?: string) {
const languageData = this.getLanguageData(lang);

return Boolean(languageData && languageData[keysetName] && languageData[keysetName][key]);
}

i18n(keysetName: string, key: string, params?: Params): string {
const lang = this.lang || I18N.defaultLang;
let languageData: KeysetData | undefined;
if (lang) {
languageData = this.data[lang];
}
const languageData = this.getLanguageData(this.lang);

if (typeof languageData === 'undefined') {
throw new Error(`Language '${lang}' is not defined, make sure you call setLang for the same language you called registerKeysets for!`);
throw new Error(`Language '${this.lang}' is not defined, make sure you call setLang for the same language you called registerKeysets for!`);
}

// если нет переводов
if (Object.keys(languageData).length === 0) {
this.warn('Language data is empty.');

Expand All @@ -90,7 +55,6 @@ export class I18N {

const keyset = languageData[keysetName];

// если нет кейсета
if (!keyset) {
this.warn(
'Keyset not found.',
Expand All @@ -100,7 +64,6 @@ export class I18N {
return key;
}

// если в кейсете нет переводов
if (Object.keys(keyset).length === 0) {
this.warn(
'Keyset is empty.',
Expand Down Expand Up @@ -189,4 +152,9 @@ export class I18N {
}
});
}

protected getLanguageData(lang?: string): KeysetData | undefined {
const langCode = lang || this.lang;
return langCode ? this.data[langCode] : undefined;
}
}
2 changes: 1 addition & 1 deletion src/pluralize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Plural} from "./types";
import {Plural} from './types';

export function pluralize(keyValue: string[], count: number): string {
let result: string;
Expand Down
2 changes: 1 addition & 1 deletion src/replace-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Params} from "./types";
import {Params} from './types';

export function replaceParams(keyValue: string, params: Params): string {
let result = keyValue;
Expand Down
54 changes: 0 additions & 54 deletions src/rum-logger.ts

This file was deleted.

0 comments on commit 9a07c54

Please sign in to comment.