Skip to content

Commit 57d1a48

Browse files
petebacondarwinmhevery
authored andcommitted
fix(localize): include the last placeholder in parsed translation text (#38452)
When creating a `ParsedTranslation` from a set of message parts and placeholder names a textual representation of the message is computed. Previously the last placeholder and text segment were missing from this computed message string. PR Close #38452
1 parent d662a64 commit 57d1a48

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function parseTranslation(messageString: TargetMessage): ParsedTranslatio
113113
export function makeParsedTranslation(
114114
messageParts: string[], placeholderNames: string[] = []): ParsedTranslation {
115115
let messageString = messageParts[0];
116-
for (let i = 0; i < placeholderNames.length - 1; i++) {
116+
for (let i = 0; i < placeholderNames.length; i++) {
117117
messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;
118118
}
119119
return {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {computeMsgId, makeTemplateObject, ParsedTranslation, parseTranslation, TargetMessage, translate} from '..';
8+
import {computeMsgId, makeParsedTranslation, makeTemplateObject, ParsedTranslation, parseTranslation, TargetMessage, translate} from '..';
99

1010
describe('utils', () => {
1111
describe('makeTemplateObject', () => {
@@ -22,6 +22,24 @@ describe('utils', () => {
2222
});
2323
});
2424

25+
describe('makeParsedTranslation()', () => {
26+
it('should compute a template object from the parts', () => {
27+
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).messageParts)
28+
.toEqual(makeTemplateObject(['a', 'b', 'c'], ['a', 'b', 'c']));
29+
});
30+
31+
it('should include the placeholder names', () => {
32+
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).placeholderNames).toEqual([
33+
'ph1', 'ph2'
34+
]);
35+
});
36+
37+
it('should compute the message string from the parts and placeholder names', () => {
38+
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).text)
39+
.toEqual('a{$ph1}b{$ph2}c');
40+
});
41+
});
42+
2543
describe('parseTranslation', () => {
2644
it('should extract the messageParts as a TemplateStringsArray', () => {
2745
const translation = parseTranslation('a{$one}b{$two}c');

0 commit comments

Comments
 (0)