fix(core): handle spaces after `select` and `plural` ICU keywords by AndrewKushnir · Pull Request #37866 · angular/angular · GitHub
Skip to content

fix(core): handle spaces after select and plural ICU keywords - #37866

Closed
AndrewKushnir wants to merge 4 commits into
angular:masterfrom
AndrewKushnir:i18n_select_whitespace
Closed

AndrewKushnir wants to merge 4 commits into
angular:masterfrom
AndrewKushnir:i18n_select_whitespace

Conversation

@AndrewKushnir

Copy link
Copy Markdown
Contributor

Currently when the plural or select keywords in an ICU contain trailing spaces (e.g. {count, select , ...}), these spaces are also included into the key names in ICU vars (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be converted into _ symbols while normalizing placeholder names, thus causing mismatches at runtime (i.e. placeholder will not be replaced with the correct value). This commit updates the code to trim these spaces while generating an object with placeholders, to make sure the runtime logic can replace these placeholders with the right values.

Another approach that I was considering is to trim these spaces at the Lexer level, but that would be a big breaking change affecting a portion of apps due to the fact that i18n message ids would be changed in this case.

PR Type

What kind of change does this PR introduce?

  • Bugfix

Does this PR introduce a breaking change?

  • Yes
  • No

Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g. `{count, select , ...}`), these spaces are also included into the key names in ICU vars (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be converted into `_` symbols while normalizing placeholder names, thus causing mismatches at runtime (i.e. placeholder will not be replaced with the correct value). This commit updates the code to trim these spaces while generating an object with placeholders, to make sure the runtime logic can replace these placeholders with the right values.
@AndrewKushnir AndrewKushnir added type: bug/fix action: review The PR is still awaiting reviews from at least one requested reviewer area: i18n Issues related to localization and internationalization target: patch This PR is targeted for the next patch release labels Jul 1, 2020
@ngbot ngbot Bot modified the milestone: needsTriage Jul 1, 2020

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AndrewKushnir - I think we can fix this a bit further up the call stack without breaking the current message ids, but I might be wrong so please check this idea out and see what you think...

diff --git a/packages/compiler/src/render3/r3_template_transform.ts b/packages/compiler/src/render3/r3_template_transform.ts
index e91502af63..137fb7bff8 100644
--- a/packages/compiler/src/render3/r3_template_transform.ts
+++ b/packages/compiler/src/render3/r3_template_transform.ts
@@ -276,7 +276,8 @@ class HtmlAstToIvyAst implements html.Visitor {
         // ICU expression is a plain string, not wrapped into start
         // and end tags, so we wrap it before passing to binding parser
         const wrapped = `${config.start}${value}${config.end}`;
-        vars[key] = this._visitTextWithInterpolation(wrapped, expansion.sourceSpan) as t.BoundText;
+        vars[key.trim()] =
+            this._visitTextWithInterpolation(wrapped, expansion.sourceSpan) as t.BoundText;
       } else {
         placeholders[key] = this._visitTextWithInterpolation(value, expansion.sourceSpan);
       }

This is much simpler and doesn't require touching i18nBindProps and callers at all.

@AndrewKushnir
AndrewKushnir marked this pull request as ready for review July 1, 2020 18:53
@AndrewKushnir

Copy link
Copy Markdown
Contributor Author

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me!

@AndrewKushnir AndrewKushnir removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Jul 1, 2020
@AndrewKushnir

Copy link
Copy Markdown
Contributor Author

Presubmit

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit cla: yes labels Jul 1, 2020
@alxhub alxhub closed this in aed6b13 Jul 6, 2020
alxhub pushed a commit that referenced this pull request Jul 6, 2020
…7866)

Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g. `{count, select , ...}`), these spaces are also included into the key names in ICU vars (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be converted into `_` symbols while normalizing placeholder names, thus causing mismatches at runtime (i.e. placeholder will not be replaced with the correct value). This commit updates the code to trim these spaces while generating an object with placeholders, to make sure the runtime logic can replace these placeholders with the right values.

PR Close #37866
@angular-automatic-lock-bot

Copy link
Copy Markdown

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Aug 6, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…gular#37866)

Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g. `{count, select , ...}`), these spaces are also included into the key names in ICU vars (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be converted into `_` symbols while normalizing placeholder names, thus causing mismatches at runtime (i.e. placeholder will not be replaced with the correct value). This commit updates the code to trim these spaces while generating an object with placeholders, to make sure the runtime logic can replace these placeholders with the right values.

PR Close angular#37866
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: i18n Issues related to localization and internationalization cla: yes target: patch This PR is targeted for the next patch release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants