From 8cd4099db93742d17264eebcc3ba1999b9f3db7c Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sun, 16 Aug 2020 14:17:56 +0100 Subject: [PATCH] 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 --- .../localize/src/utils/src/translations.ts | 2 +- .../src/utils/test/translations_spec.ts | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/localize/src/utils/src/translations.ts b/packages/localize/src/utils/src/translations.ts index 8e5094b71f..fb0e7aeaae 100644 --- a/packages/localize/src/utils/src/translations.ts +++ b/packages/localize/src/utils/src/translations.ts @@ -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 { diff --git a/packages/localize/src/utils/test/translations_spec.ts b/packages/localize/src/utils/test/translations_spec.ts index 8ccc10cb04..3ec4a5da01 100644 --- a/packages/localize/src/utils/test/translations_spec.ts +++ b/packages/localize/src/utils/test/translations_spec.ts @@ -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');