fix(ivy): handle ICUs with placeholders in case other nested ICUs are present (#31516)

Prior to this fix, the logic to set the right placeholder format for ICUs was a bit incorrect: if there was a nested ICU in one of the root ICU cases, that led to a problem where placeholders in subsequent branches used the wrong ({$placeholder}) format instead of {PLACEHOLDER} one. This commit updates the logic to make sure we properly transform all placeholders even if nested ICUs are present.

PR Close #31516
This commit is contained in:
Andrew Kushnir
2019-07-11 11:59:22 -07:00
committed by Matias Niemelä
parent d545bbeee4
commit 63e458dd3a
3 changed files with 85 additions and 8 deletions

View File

@ -16,18 +16,19 @@ import {formatI18nPlaceholderName} from './util';
*/
class SerializerVisitor implements i18n.Visitor {
/**
* Flag that indicates that we are processing elements of an ICU.
* Keeps track of ICU nesting level, allowing to detect that we are processing elements of an ICU.
*
* This flag is needed due to the fact that placeholders in ICUs and in other messages are
* represented differently in Closure:
* This is needed due to the fact that placeholders in ICUs and in other messages are represented
* differently in Closure:
* - {$placeholder} in non-ICU case
* - {PLACEHOLDER} inside ICU
*/
private insideIcu = false;
private icuNestingLevel = 0;
private formatPh(value: string): string {
const formatted = formatI18nPlaceholderName(value, /* useCamelCase */ !this.insideIcu);
return this.insideIcu ? `{${formatted}}` : `{$${formatted}}`;
const isInsideIcu = this.icuNestingLevel > 0;
const formatted = formatI18nPlaceholderName(value, /* useCamelCase */ !isInsideIcu);
return isInsideIcu ? `{${formatted}}` : `{$${formatted}}`;
}
visitText(text: i18n.Text, context: any): any { return text.value; }
@ -37,11 +38,11 @@ class SerializerVisitor implements i18n.Visitor {
}
visitIcu(icu: i18n.Icu, context: any): any {
this.insideIcu = true;
this.icuNestingLevel++;
const strCases =
Object.keys(icu.cases).map((k: string) => `${k} {${icu.cases[k].visit(this)}}`);
const result = `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
this.insideIcu = false;
this.icuNestingLevel--;
return result;
}