Skip to content

Commit

Permalink
fix: don't backtrack on translation key if nested value was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Jan 22, 2025
1 parent dfa265e commit 99d268d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ function translate<TTrKey extends string = string>(language: string, key: TTrKey
const keyParts = key.split(".");
let value: string | TrObject | undefined = trObj;
for(const part of keyParts) {
if(typeof value !== "object" || value === null)
if(typeof value !== "object" || value === null) {
value = undefined;
break;
}
value = value?.[part];
}

if(typeof value === "string")
return transformTrVal(key, value);

Expand All @@ -140,7 +143,9 @@ function translate<TTrKey extends string = string>(language: string, key: TTrKey
return transformTrVal(key, value);

// default to fallbackLang or translation key
return fallbackLang ? translate(fallbackLang, key, ...trArgs) : key;
return fallbackLang
? translate(fallbackLang, key, ...trArgs)
: key;
}

//#region tr funcs
Expand Down

0 comments on commit 99d268d

Please sign in to comment.