From 6b9ba73c04a339c4d0e1d49566699d6b948c8285 Mon Sep 17 00:00:00 2001 From: Shane Melton Date: Fri, 8 Mar 2024 16:38:50 -0800 Subject: [PATCH] Fix improper tag count comparison --- libs/components/src/i18n/i18n.component.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/libs/components/src/i18n/i18n.component.ts b/libs/components/src/i18n/i18n.component.ts index ae8998654c3..b48843e35d9 100644 --- a/libs/components/src/i18n/i18n.component.ts +++ b/libs/components/src/i18n/i18n.component.ts @@ -81,12 +81,12 @@ export class I18nComponent implements AfterContentInit { this.args[1], this.args[2], ); + const [translationParts, tagCount] = this.parseTranslatedString(translatedText); + this.translationParts = translationParts; - this.translationParts = this.parseTranslatedString(translatedText); - - if (this.translationParts.length !== this.templateTags.length) { + if (tagCount !== this.templateTags.length) { this.logService.warning( - `The translation for "${this.translationKey}" has ${this.translationParts.length} template tags(s), but ${this.templateTags.length} bit-i18n-part directive(s) were found.`, + `The translation for "${this.translationKey}" has ${tagCount} template tags(s), but ${this.templateTags.length} bit-i18n-part directive(s) were found.`, ); } @@ -103,25 +103,28 @@ export class I18nComponent implements AfterContentInit { /** * Parses a translated string into an array of parts separated by tag identifiers. * Tag identifiers must be numbers surrounded by angle brackets. + * Includes the number of tags found in the string. * @example * parseTranslatedString("Hello <0>World!") - * // returns [{ text: "Hello " }, { text: "World", tagId: 0 }, { text: "!" }] + * // returns [[{ text: "Hello " }, { text: "World", tagId: 0 }, { text: "!" }], 1] * @param inputString * @private */ - private parseTranslatedString(inputString: string): I18nStringPart[] { + private parseTranslatedString(inputString: string): [I18nStringPart[], number] { const regex = /<(\d+)>(.*?)<\/\1>|([^<]+)/g; const parts: I18nStringPart[] = []; let match: RegExpMatchArray; + let tagCount = 0; while ((match = regex.exec(inputString)) !== null) { if (match[1]) { parts.push({ text: match[2], tagId: parseInt(match[1]) }); + tagCount++; } else { parts.push({ text: match[3] }); } } - return parts; + return [parts, tagCount]; } }