Skip to content

Commit

Permalink
fix: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
korvin89 committed Jan 26, 2024
1 parent cec4ee9 commit cd62e12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -182,7 +182,7 @@ export class I18N {
throw new Error(message);
}

return (text || fallbackText) as string;
return result;
}

keyset<TKey extends string = string>(keysetName: string) {
Expand Down Expand Up @@ -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},
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/replace-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/translation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"module": "esnext",
"baseUrl": ".",
"importHelpers": true,
"declaration": true
"declaration": true,
"noUncheckedIndexedAccess": true
},
"include": ["src/*.ts"],
"exclude": ["**/*.spec.ts"]
Expand Down

0 comments on commit cd62e12

Please sign in to comment.