From 86b6cf56dcb8646dcac767505c30a33806a0ce09 Mon Sep 17 00:00:00 2001 From: Dmitrii Kharin Date: Fri, 20 May 2022 18:33:41 +0300 Subject: [PATCH] feat: remove static methods for prior instance method setLang, remove default rumLogger --- README.md | 6 ++--- src/index.ts | 40 +++++++++----------------------- src/pluralize.ts | 2 +- src/replace-params.ts | 2 +- src/rum-logger.ts | 54 ------------------------------------------- 5 files changed, 16 insertions(+), 88 deletions(-) delete mode 100644 src/rum-logger.ts diff --git a/README.md b/README.md index 3adfba7..aabad49 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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') ); // -> "Нет доступа к чарту" diff --git a/src/index.ts b/src/index.ts index 95bad7b..062d12c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; type KeysetData = Record; @@ -9,32 +9,21 @@ type KeysetData = Record; export * from './types'; export class I18N { - static LANGS: Record = { + 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 = { [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; } @@ -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; } } @@ -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]; @@ -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.'); @@ -90,7 +74,6 @@ export class I18N { const keyset = languageData[keysetName]; - // если нет кейсета if (!keyset) { this.warn( 'Keyset not found.', @@ -100,7 +83,6 @@ export class I18N { return key; } - // если в кейсете нет переводов if (Object.keys(keyset).length === 0) { this.warn( 'Keyset is empty.', diff --git a/src/pluralize.ts b/src/pluralize.ts index f6574a5..0aec377 100644 --- a/src/pluralize.ts +++ b/src/pluralize.ts @@ -1,4 +1,4 @@ -import {Plural} from "./types"; +import {Plural} from './types'; export function pluralize(keyValue: string[], count: number): string { let result: string; diff --git a/src/replace-params.ts b/src/replace-params.ts index c006b67..3e3d95f 100644 --- a/src/replace-params.ts +++ b/src/replace-params.ts @@ -1,4 +1,4 @@ -import {Params} from "./types"; +import {Params} from './types'; export function replaceParams(keyValue: string, params: Params): string { let result = keyValue; diff --git a/src/rum-logger.ts b/src/rum-logger.ts deleted file mode 100644 index 0e3c2ec..0000000 --- a/src/rum-logger.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* eslint camelcase: ['error', {allow: ['__webpack_require__']}] */ - -import {Logger} from "./types"; - -declare let __webpack_require__: unknown; - -declare global { - interface Window { - Ya?: { - Rum?: { - logError: (arg?: any) => void; - ERROR_LEVEL: { - INFO: string; - }; - }; - }; - } -} - -const warnCache = new Set(); - -/** - * @deprecated - */ -export const rumLogger: Logger = { - log(message, {level, logger, extra} = {}) { - if (typeof window === 'undefined' || typeof window.Ya?.Rum?.logError !== 'function') { - return; - } - - if (warnCache.has(logger)) { - return; - } - - if (typeof __webpack_require__ !== 'undefined' && process.env.NODE_ENV === 'development') { - console.warn('@yandex-cloud/i18n: default logger is deprecated, and would be removed in future. Consult docs for alternative.'); - } - - console.warn(`[${extra?.type}][${logger || ''}] ${message}`); - - try { - window.Ya.Rum.logError({ - message, - type: extra?.type, - level: level === 'info' ? window.Ya.Rum.ERROR_LEVEL.INFO : undefined, - block: logger - }); - } catch (err) { - console.error(err); - } - - warnCache.add(logger); - } -};