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
This commit is contained in:
Pete Bacon Darwin 2020-08-16 14:17:56 +01:00 committed by Misko Hevery
parent 0b54c0c6b4
commit 8cd4099db9
2 changed files with 20 additions and 2 deletions

View File

@ -113,7 +113,7 @@ export function parseTranslation(messageString: TargetMessage): ParsedTranslatio
export function makeParsedTranslation(
messageParts: string[], placeholderNames: string[] = []): ParsedTranslation {
let messageString = messageParts[0];
for (let i = 0; i < placeholderNames.length - 1; i++) {
for (let i = 0; i < placeholderNames.length; i++) {
messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;
}
return {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {computeMsgId, makeTemplateObject, ParsedTranslation, parseTranslation, TargetMessage, translate} from '..';
import {computeMsgId, makeParsedTranslation, makeTemplateObject, ParsedTranslation, parseTranslation, TargetMessage, translate} from '..';
describe('utils', () => {
describe('makeTemplateObject', () => {
@ -22,6 +22,24 @@ describe('utils', () => {
});
});
describe('makeParsedTranslation()', () => {
it('should compute a template object from the parts', () => {
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).messageParts)
.toEqual(makeTemplateObject(['a', 'b', 'c'], ['a', 'b', 'c']));
});
it('should include the placeholder names', () => {
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).placeholderNames).toEqual([
'ph1', 'ph2'
]);
});
it('should compute the message string from the parts and placeholder names', () => {
expect(makeParsedTranslation(['a', 'b', 'c'], ['ph1', 'ph2']).text)
.toEqual('a{$ph1}b{$ph2}c');
});
});
describe('parseTranslation', () => {
it('should extract the messageParts as a TemplateStringsArray', () => {
const translation = parseTranslation('a{$one}b{$two}c');