diff --git a/scripts/generatePluginList.ts b/scripts/generatePluginList.ts index 83b1379ae..9bf54961d 100644 --- a/scripts/generatePluginList.ts +++ b/scripts/generatePluginList.ts @@ -116,16 +116,10 @@ async function getTranslation(node: Node): Promise { await readFile(`./translations/en/${namespace}.json`, "utf-8") ); - function getByPath(key: string, object: any) { - try { - return key.split(".").reduce((obj, key) => obj[key], object); - } catch { - // errors if the object doesn't contain the key - return undefined; - } - } + const dotProp = (key: string, object: any) => + key.split(".").reduce((obj, key) => obj?.[key], object); - return getByPath(path, bundle); + return dotProp(path, bundle); } async function parseFile(fileName: string) { diff --git a/src/utils/translation.tsx b/src/utils/translation.tsx index 57d900705..4ae8b4690 100644 --- a/src/utils/translation.tsx +++ b/src/utils/translation.tsx @@ -74,20 +74,14 @@ function format(source: string, variables: Record) { } // converts a dot-notation path to an object value -function getByPath(key: string, object: any) { - try { - return key.split(".").reduce((obj, key) => obj[key], object); - } catch { - // errors if the object doesn't contain the key - return undefined; - } -} +const dotProp = (key: string, object: any) => + key.split(".").reduce((obj, key) => obj?.[key], object); type Translation = string | ({ [rule in Intl.LDMLPluralRule]?: string } & { other: string; }); // translation retrieval function function _t(key: string, bundle: any): Translation { - const translation = getByPath(key, bundle); + const translation = dotProp(key, bundle); if (!translation) { if (bundle !== translations.en) {