Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(localize): improve placeholder mismatch error message #35593

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/localize/src/utils/src/translations.ts
Expand Up @@ -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.`);
}
})
];
Expand Down
7 changes: 5 additions & 2 deletions packages/localize/src/utils/test/translations_spec.ts
Expand Up @@ -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', () => {
Expand Down