fix: frontend translation framework crashes on string errors (#34118)

This commit is contained in:
Maxime Beauchemin
2025-07-14 10:23:18 -07:00
committed by GitHub
parent 68b84acd93
commit 160917eae8
2 changed files with 25 additions and 10 deletions

View File

@@ -86,20 +86,33 @@ export default class Translator {
}
translate(input: string, ...args: unknown[]): string {
return this.i18n.translate(input).fetch(...args);
try {
return this.i18n.translate(input).fetch(...args);
} catch (err) {
logging.warn(`Translation failed for key "${input}" with args:`, args);
return input;
}
}
translateWithNumber(key: string, ...args: unknown[]): string {
const [plural, num, ...rest] = args;
if (typeof plural === 'number') {
try {
const [plural, num, ...rest] = args;
if (typeof plural === 'number') {
return this.i18n
.translate(key)
.ifPlural(plural, key)
.fetch(plural, num, ...rest);
}
return this.i18n
.translate(key)
.ifPlural(plural, key)
.fetch(plural, num, ...args);
.ifPlural(num as number, plural as string)
.fetch(...rest);
} catch (err) {
logging.warn(
`Plural translation failed for key "${key}" with args:`,
args,
);
}
return this.i18n
.translate(key)
.ifPlural(num as number, plural as string)
.fetch(...rest);
return key;
}
}

View File

@@ -22,6 +22,8 @@
set -e
export NODE_NO_WARNINGS=1
for file in $( find ../superset/translations/** -name '*.po' );
do
extension=${file##*.}
@@ -29,7 +31,7 @@ do
if [ $extension == "po" ]
then
echo "po2json --domain superset --format jed1.x $file $filename.json"
po2json --domain superset --format jed1.x $file $filename.json
po2json --domain superset --format jed1.x --fuzzy $file $filename.json
prettier --write $filename.json
fi
done