diff --git a/README.md b/README.md index 3adfba7..d7b0edc 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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 @@ -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') ); // -> "Нет доступа к чарту" @@ -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}}'" } ``` diff --git a/src/index.spec.ts b/src/index.spec.ts index f6b855f..15d5b62 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,4 +1,4 @@ -import {I18N} from "./index"; +import {I18N} from './index'; let i18n: I18N; @@ -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'); }); diff --git a/src/index.ts b/src/index.ts index 95bad7b..83a3985 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,42 +9,16 @@ type KeysetData = Record; export * from './types'; export class I18N { - static LANGS: Record = { - 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; - + data: Record = {}; + 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 = {}) { @@ -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.'); @@ -90,7 +55,6 @@ export class I18N { const keyset = languageData[keysetName]; - // если нет кейсета if (!keyset) { this.warn( 'Keyset not found.', @@ -100,7 +64,6 @@ export class I18N { return key; } - // если в кейсете нет переводов if (Object.keys(keyset).length === 0) { this.warn( 'Keyset is empty.', @@ -189,4 +152,9 @@ export class I18N { } }); } + + protected getLanguageData(lang?: string): KeysetData | undefined { + const langCode = lang || this.lang; + return langCode ? this.data[langCode] : undefined; + } } 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); - } -};