Visitar URL original
fix(core): strip reserved character from translations · angular/angular@a312520 · GitHub
Skip to content

Commit a312520

Browse files
crisbetoNothingEverHappens
authored andcommitted
fix(core): strip reserved character from translations
We use a special character as a delimiter in translations. If that character appears in the translation, it can throw us off so these changes strip it. (cherry picked from commit 17f689b)
1 parent 0474258 commit a312520

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎packages/core/test/acceptance/i18n_spec.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ describe('runtime i18n', () => {
229229
}-->!</div>`);
230230
});
231231

232+
it('should strip replacement characters in translations', () => {
233+
loadTranslations({
234+
[computeMsgId('Hello {$START_TAG_SPAN}world{$CLOSE_TAG_SPAN}!', '')]:
235+
'Bonjour \uFFFD#100\uFFFD \uFFFD0\uFFFD {$START_TAG_SPAN}monde{$CLOSE_TAG_SPAN}!',
236+
});
237+
const fixture = initWithTemplate(AppComp, `<div i18n>Hello <span>world</span>!</div>`);
238+
expect(fixture.nativeElement.innerHTML).toEqual(
239+
`<div>Bonjour #100 0 <span>monde</span>!</div>`,
240+
);
241+
});
242+
232243
it('should support multiple i18n blocks', () => {
233244
loadTranslations({
234245
[computeMsgId('trad {$INTERPOLATION}')]: 'traduction {$INTERPOLATION}',

‎packages/localize/src/utils/src/translations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function translate(
9595
* @param message the message to be parsed.
9696
*/
9797
export function parseTranslation(messageString: TargetMessage): ParsedTranslation {
98+
messageString = messageString.replace(/\uFFFD/g, '');
9899
const parts = messageString.split(/{\$([^}]*)}/);
99100
const messageParts = [parts[0]];
100101
const placeholderNames: string[] = [];
@@ -122,6 +123,7 @@ export function makeParsedTranslation(
122123
messageParts: string[],
123124
placeholderNames: string[] = [],
124125
): ParsedTranslation {
126+
messageParts = messageParts.map((part) => part.replace(/\uFFFD/g, ''));
125127
let messageString = messageParts[0];
126128
for (let i = 0; i < placeholderNames.length; i++) {
127129
messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;

‎packages/localize/src/utils/test/translations_spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ describe('utils', () => {
5353
'a{$ph1}b{$ph2}c',
5454
);
5555
});
56+
57+
it('should strip replacement characters from message parts', () => {
58+
const translation = makeParsedTranslation(['a\uFFFD#1', 'b\uFFFD*1:1'], ['ph1']);
59+
expect(translation.messageParts).toEqual(
60+
makeTemplateObject(['a#1', 'b*1:1'], ['a#1', 'b*1:1']),
61+
);
62+
expect(translation.text).toEqual('a#1{$ph1}b*1:1');
63+
});
5664
});
5765

5866
describe('parseTranslation', () => {
@@ -108,6 +116,13 @@ describe('utils', () => {
108116
expect(translation.messageParts.raw).toEqual(['', '', '']);
109117
expect(translation.placeholderNames).toEqual(['one', 'two']);
110118
});
119+
120+
it('should strip replacement characters from message parts', () => {
121+
const translation = parseTranslation('a\uFFFD#1{$one}\uFFFD:marker:b\uFFFD');
122+
expect(translation.messageParts).toEqual(['a#1', ':marker:b']);
123+
expect(translation.messageParts.raw).toEqual(['a#1', '\\:marker:b']);
124+
expect(translation.text).toEqual('a#1{$one}:marker:b');
125+
});
111126
});
112127

113128
describe('translate', () => {

0 commit comments

Comments
 (0)