From 53f059ee8f55be99c927a5feede2f5e5b043fe0c Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 20 Feb 2020 21:40:56 +0100 Subject: [PATCH] fix(localize): improve placeholder mismatch error message (#35593) The original error message was confusing since often it is the translation that is at fault not the message. PR Close #35593 --- packages/localize/src/utils/src/translations.ts | 3 ++- packages/localize/src/utils/test/translations_spec.ts | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/localize/src/utils/src/translations.ts b/packages/localize/src/utils/src/translations.ts index 2ad83cd9a0..4b5c51e346 100644 --- a/packages/localize/src/utils/src/translations.ts +++ b/packages/localize/src/utils/src/translations.ts @@ -68,7 +68,8 @@ export function translate( return message.substitutions[placeholder]; } else { throw new Error( - `No placeholder found with name ${placeholder} in message ${describeMessage(message)}.`); + `There is a placeholder name mismatch with the translation provided for the message ${describeMessage(message)}.\n` + + `The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`); } }) ]; diff --git a/packages/localize/src/utils/test/translations_spec.ts b/packages/localize/src/utils/test/translations_spec.ts index a4f7899659..267320534d 100644 --- a/packages/localize/src/utils/test/translations_spec.ts +++ b/packages/localize/src/utils/test/translations_spec.ts @@ -88,9 +88,12 @@ describe('utils', () => { it('should throw an error if the translation contains placeholders that are not in the message', () => { - expect(() => doTranslate({'abc': 'a{$PH}bc'}, parts `abc`)) + expect( + () => doTranslate( + {'abc{$INTERPOLATION}def': 'a{$PH}bc'}, parts `abc${1 + 2}:INTERPOLATION:def`)) .toThrowError( - 'No placeholder found with name PH in message "2674653928643152084" ("abc").'); + `There is a placeholder name mismatch with the translation provided for the message "8986527425650846693" ("abc{$INTERPOLATION}def").\n` + + `The translation contains a placeholder with name PH, which does not exist in the message.`); }); it('(with identity translations) should render template literals as-is', () => {