From cd62e12069bbb7a70e9d9b980be68cb30956e450 Mon Sep 17 00:00:00 2001 From: Evgeny Alaev Date: Fri, 26 Jan 2024 14:38:47 +0300 Subject: [PATCH] fix: review fixes --- src/index.ts | 22 ++++++++++++++-------- src/replace-params.ts | 2 +- src/translation-helpers.ts | 4 ++-- tsconfig.json | 3 ++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index bb0f827..b38d141 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,7 +132,7 @@ export class I18N { has(keysetName: string, key: string, lang?: string) { const languageData = this.getLanguageData(lang); - return Boolean(languageData && languageData[keysetName] && languageData[keysetName][key]); + return Boolean(languageData && languageData[keysetName] && languageData[keysetName]?.[key]); } i18n(keysetName: string, key: string, params?: Params): string { @@ -150,12 +150,12 @@ export class I18N { const message = mapErrorCodeToMessage({ code: details.code, lang: this.lang, - fallbackLang: this.fallbackLang, + fallbackLang: this.fallbackLang === this.lang ? undefined : this.fallbackLang, }); this.warn(message, details.keysetName, details.key); } - if (this.fallbackLang) { + if (text === undefined && this.fallbackLang && this.fallbackLang !== this.lang) { ({text, fallbackText, details} = this.getTranslationData({ keysetName, key, @@ -166,14 +166,14 @@ export class I18N { if (details && details.code !== ErrorCode.NoLanguageData) { const message = mapErrorCodeToMessage({ code: details.code, - lang: this.lang, - fallbackLang: this.fallbackLang, + lang: this.fallbackLang, }); this.warn(message, details.keysetName, details.key); } } - if (!text && !fallbackText) { + const result = text ?? fallbackText; + if (result === undefined) { const message = mapErrorCodeToMessage({ code: details?.code, lang: this.lang, @@ -182,7 +182,7 @@ export class I18N { throw new Error(message); } - return (text || fallbackText) as string; + return result; } keyset(keysetName: string) { @@ -265,7 +265,7 @@ export class I18N { const keyValue = keyset && keyset[key]; const result: TranslationData = {}; - if (typeof keyValue === 'undefined') { + if (keyValue === undefined) { return { fallbackText: key, details: {code: ErrorCode.MissingKey, keysetName, key}, @@ -291,6 +291,12 @@ export class I18N { const pluralizer = this.getLanguagePluralizer(lang); result.text = keyValue[pluralizer(count, PluralForm)] || keyValue[PluralForm.Many]; + if (result.text === undefined) { + return { + fallbackText: key, + details: {code: ErrorCode.MissingKeyPlurals, keysetName, key}, + }; + } if (keyValue[PluralForm.None] === undefined) { result.details = { diff --git a/src/replace-params.ts b/src/replace-params.ts index d2106e7..f8e79a9 100644 --- a/src/replace-params.ts +++ b/src/replace-params.ts @@ -14,7 +14,7 @@ export function replaceParams(keyValue: string, params: Params): string { lastIndex = PARAM_REGEXP.lastIndex; const [all, key] = match; - if (Object.prototype.hasOwnProperty.call(params, key)) { + if (key && Object.prototype.hasOwnProperty.call(params, key)) { result += params[key]; } else { result += all; diff --git a/src/translation-helpers.ts b/src/translation-helpers.ts index 0d0a1e0..239ff33 100644 --- a/src/translation-helpers.ts +++ b/src/translation-helpers.ts @@ -42,7 +42,7 @@ export function mapErrorCodeToMessage(args: {lang?: string; fallbackLang?: strin break; } case ErrorCode.MissingKeyPlurals: { - message = 'Missing required plurals'; + message = 'Missing required plurals.'; break; } case ErrorCode.NoLanguageData: { @@ -51,7 +51,7 @@ export function mapErrorCodeToMessage(args: {lang?: string; fallbackLang?: strin } if (fallbackLang) { - message += ` Trying to use default language "${fallbackLang}"...`; + message += ` Trying to use fallback language "${fallbackLang}"...`; } return message; diff --git a/tsconfig.json b/tsconfig.json index 1c06f60..940e607 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,8 @@ "module": "esnext", "baseUrl": ".", "importHelpers": true, - "declaration": true + "declaration": true, + "noUncheckedIndexedAccess": true }, "include": ["src/*.ts"], "exclude": ["**/*.spec.ts"]