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
  • Loading branch information
dihar committed May 24, 2022
1 parent 32ea385 commit 895d0e8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 93 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,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 +73,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 +92,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
48 changes: 15 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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>;
Expand All @@ -14,27 +14,16 @@ export class I18N {
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;
lang: string = I18N.LANGS.en;

logger: Logger | null = null;

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

Expand All @@ -43,7 +32,7 @@ export class I18N {
this.lang = lang;
} else {
console.warn('Attempted to set unknown lang.');
this.lang = I18N.LANGS.ru;
this.lang = this.lang || I18N.LANGS.en;
}
}

Expand All @@ -60,28 +49,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 +70,6 @@ export class I18N {

const keyset = languageData[keysetName];

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

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

protected getLanguageData(lang?: string): KeysetData | undefined {
return this.data[lang || this.lang];
}
}
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 895d0e8

Please sign in to comment.