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 86b6cf5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 88 deletions.
6 changes: 3 additions & 3 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(i18n);
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 Down
40 changes: 11 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
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> = {
static LANGS: Record<'ru' | 'en', 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;
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,18 +49,14 @@ 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.data[lang || this.lang];

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

i18n(keysetName: string, key: string, params?: Params): string {
const lang = this.lang || I18N.defaultLang;
const lang = this.lang;
let languageData: KeysetData | undefined;
if (lang) {
languageData = this.data[lang];
Expand All @@ -81,7 +66,6 @@ export class I18N {
throw new Error(`Language '${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 +74,6 @@ export class I18N {

const keyset = languageData[keysetName];

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

// если в кейсете нет переводов
if (Object.keys(keyset).length === 0) {
this.warn(
'Keyset is empty.',
Expand Down
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 86b6cf5

Please sign in to comment.