Skip to content

Commit ba43408

Browse files
dgp1130devversion
authored andcommitted
fix(compiler): ignore placeholder-only i18n messages (#58154)
Message which only contain a single placeholder cannot be translated, there is no static text to be translated. Therefore these messages can be skipped and shouldn't be extracted at all. Ideally, Angular would throw an error if a message is only a placeholder, since it should not contain an `i18n` attribute at all. However this would be a breaking change and require a migration which isn't in scope right now. We can explore converting this to a hard error sometime in the future. PR Close #58154
1 parent 37e3b03 commit ba43408

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎packages/compiler/src/i18n/extractor_merger.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as html from '../ml_parser/ast';
1010
import {DEFAULT_CONTAINER_BLOCKS, InterpolationConfig} from '../ml_parser/defaults';
1111
import {ParseTreeResult} from '../ml_parser/parser';
12+
import {TokenType} from '../ml_parser/tokens';
1213

1314
import * as i18n from './i18n_ast';
1415
import {createI18nMessageFactory, I18nMessageFactory} from './i18n_parser';
@@ -380,7 +381,9 @@ class _Visitor implements html.Visitor {
380381
private _addMessage(ast: html.Node[], msgMeta?: string): i18n.Message | null {
381382
if (
382383
ast.length == 0 ||
383-
(ast.length == 1 && ast[0] instanceof html.Attribute && !(<html.Attribute>ast[0]).value)
384+
this._isEmptyAttributeValue(ast) ||
385+
this._isPlaceholderOnlyAttributeValue(ast) ||
386+
this._isPlaceholderOnlyMessage(ast)
384387
) {
385388
// Do not create empty messages
386389
return null;
@@ -392,6 +395,48 @@ class _Visitor implements html.Visitor {
392395
return message;
393396
}
394397

398+
// Check for cases like `<div i18n-title title="">`.
399+
private _isEmptyAttributeValue(ast: html.Node[]): boolean {
400+
if (!isAttrNode(ast)) return false;
401+
const node = ast[0];
402+
403+
return node.value.trim() === '';
404+
}
405+
406+
// Check for cases like `<div i18n-title title="{{ name }}">`.
407+
private _isPlaceholderOnlyAttributeValue(ast: html.Node[]): boolean {
408+
if (!isAttrNode(ast)) return false;
409+
const tokens = ast[0].valueTokens ?? [];
410+
411+
const interpolations = tokens.filter(
412+
(token) => token.type === TokenType.ATTR_VALUE_INTERPOLATION,
413+
);
414+
const plainText = tokens
415+
.filter((token) => token.type === TokenType.ATTR_VALUE_TEXT)
416+
// `AttributeValueTextToken` always has exactly one part per its type.
417+
.map((token) => token.parts[0].trim())
418+
.join('');
419+
420+
// Check if there is a single interpolation and all text around it is empty.
421+
return interpolations.length === 1 && plainText === '';
422+
}
423+
424+
// Check for cases like `<div i18n>{{ name }}</div>`.
425+
private _isPlaceholderOnlyMessage(ast: html.Node[]): boolean {
426+
if (!isTextNode(ast)) return false;
427+
const tokens = ast[0].tokens;
428+
429+
const interpolations = tokens.filter((token) => token.type === TokenType.INTERPOLATION);
430+
const plainText = tokens
431+
.filter((token) => token.type === TokenType.TEXT)
432+
// `TextToken` always has exactly one part per its type.
433+
.map((token) => token.parts[0].trim())
434+
.join('');
435+
436+
// Check if there is a single interpolation and all text around it is empty.
437+
return interpolations.length === 1 && plainText === '';
438+
}
439+
395440
// Translates the given message given the `TranslationBundle`
396441
// This is used for translating elements / blocks - see `_translateAttributes` for attributes
397442
// no-op when called in extraction mode (returns [])
@@ -593,3 +638,11 @@ function _parseMessageMeta(i18n?: string): {meaning: string; description: string
593638

594639
return {meaning, description, id: id.trim()};
595640
}
641+
642+
function isTextNode(ast: html.Node[]): ast is [html.Text] {
643+
return ast.length === 1 && ast[0] instanceof html.Text;
644+
}
645+
646+
function isAttrNode(ast: html.Node[]): ast is [html.Attribute] {
647+
return ast.length === 1 && ast[0] instanceof html.Attribute;
648+
}

‎packages/compiler/test/i18n/extractor_merger_spec.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ describe('Extractor', () => {
100100
expect(extract('<div i18n="m|d"></div>')).toEqual([]);
101101
});
102102

103+
it('should not create a message for placeholder-only elements', () => {
104+
expect(extract('<div i18n="m|d">{{ foo }}</div>')).toEqual([]);
105+
});
106+
103107
it('should ignore implicit elements in translatable elements', () => {
104108
expect(extract('<div i18n="m|d"><p></p></div>', ['p'])).toEqual([
105109
[['<ph tag name="START_PARAGRAPH"></ph name="CLOSE_PARAGRAPH">'], 'm', 'd', ''],
@@ -406,6 +410,10 @@ describe('Extractor', () => {
406410
it('should not create a message for empty attributes', () => {
407411
expect(extract('<div i18n-title="m|d" title></div>')).toEqual([]);
408412
});
413+
414+
it('should not create a message for placeholder-only attributes', () => {
415+
expect(extract('<div i18n-title="m|d" title="{{ foo }}"></div>')).toEqual([]);
416+
});
409417
});
410418

411419
describe('implicit elements', () => {

‎packages/compiler/test/i18n/whitespace_sensitivity_spec.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('i18nPreserveWhitespaceForLegacyExtraction', () => {
1818
const initial = extractMessages(
1919
`
2020
<div i18n>Hello, World!</div>
21-
<div i18n>{{ abc }}</div>
21+
<div i18n>Hello {{ abc }}</div>
2222
<div i18n>Start {{ abc }} End</div>
2323
<div i18n>{{ first }} middle {{ end }}</div>
2424
<div i18n><a href="/foo">First Second</a></div>
@@ -41,7 +41,7 @@ Test case is disabled by omitting the i18n attribute.
4141
Hello, World!
4242
</div>
4343
<div i18n>
44-
{{ abc }}
44+
Hello {{ abc }}
4545
</div>
4646
<div i18n>
4747
Start {{ abc }} End
@@ -111,7 +111,7 @@ Test case is disabled by omitting the i18n attribute.
111111
Hello, World!
112112
</div>
113113
<div i18n>
114-
{{ abc }}
114+
Hello {{ abc }}
115115
</div>
116116
<div i18n>
117117
Start {{ abc }} End
@@ -171,7 +171,7 @@ Test case is disabled by omitting the i18n attribute.
171171
Hello, World!
172172
</div>
173173
<div i18n>
174-
{{ abc }}
174+
Hello {{ abc }}
175175
</div>
176176
<div i18n>
177177
Start {{ abc }} End
@@ -237,7 +237,7 @@ Test case is disabled by omitting the i18n attribute.
237237
exceeds line length.
238238
</div>
239239
<div i18n>
240-
{{ veryLongExpressionWhichMaybeExceedsLineLength | async }}
240+
Hello {{ veryLongExpressionWhichMaybeExceedsLineLength | async }}
241241
</div>
242242
<div i18n>
243243
This is a long {{ abc }} which maybe
@@ -280,7 +280,7 @@ Test case is disabled by omitting the i18n attribute.
280280
maybe exceeds line length.
281281
</div>
282282
<div i18n>
283-
{{
283+
Hello {{
284284
veryLongExpressionWhichMaybeExceedsLineLength
285285
| async
286286
}}
@@ -346,7 +346,7 @@ Test case is disabled by omitting the i18n attribute.
346346
const initial = extractMessages(
347347
`
348348
<div i18n> Hello, World! </div>
349-
<div i18n> {{ abc }} </div>
349+
<div i18n> Hello {{ abc }} </div>
350350
<div i18n> Start {{ abc }} End </div>
351351
<div i18n> {{ first }} middle {{ end }} </div>
352352
<div i18n> <a href="/foo">Foo</a> </div>
@@ -357,7 +357,7 @@ Test case is disabled by omitting the i18n attribute.
357357
358358
i18nPreserveWhitespaceForLegacyExtraction does not support trimming ICU case text.
359359
Test case is disabled by omitting the i18n attribute.
360-
<div>{
360+
<div>Hello {
361361
apples, plural,
362362
=1 { One apple. }
363363
=other { Many apples. }
@@ -369,7 +369,7 @@ Test case is disabled by omitting the i18n attribute.
369369
const trimmed = extractMessages(
370370
`
371371
<div i18n>Hello, World!</div>
372-
<div i18n>{{ abc }}</div>
372+
<div i18n>Hello {{ abc }}</div>
373373
<div i18n>Start {{ abc }} End</div>
374374
<div i18n>{{ first }} middle {{ end }}</div>
375375
<div i18n><a href="/foo">Foo</a></div>
@@ -380,7 +380,7 @@ Test case is disabled by omitting the i18n attribute.
380380
381381
i18nPreserveWhitespaceForLegacyExtraction does not support trimming ICU case text.
382382
Test case is disabled by omitting the i18n attribute.
383-
<div>{
383+
<div>Hello {
384384
apples, plural,
385385
=1 {One apple.}
386386
=other {Many apples.}

0 commit comments

Comments
 (0)