Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): support i18n interpolated only attribute bindings #43815

Conversation

petebacondarwin
Copy link
Member

While fully dynamic bound properties (and attributes) cannot be marked for localization, properties that only contain interpolation can.

This commit ensure that attribute bindings that only contain interpolation can also be marked for localization.

Closes #43260

@petebacondarwin petebacondarwin added action: review The PR is still awaiting reviews from at least one requested reviewer area: i18n target: patch This PR is targeted for the next patch release area: compiler Issues related to `ngc`, Angular's template compiler labels Oct 12, 2021
@ngbot ngbot bot modified the milestone: Backlog Oct 12, 2021
@google-cla google-cla bot added the cla: yes label Oct 12, 2021
@petebacondarwin petebacondarwin added state: WIP and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 12, 2021
@petebacondarwin petebacondarwin marked this pull request as draft October 12, 2021 21:05
@petebacondarwin petebacondarwin force-pushed the i18n-aria-attrs-issue-43260 branch 2 times, most recently from c2a00d3 to 999e342 Compare October 12, 2021 21:13
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great, thanks Pete 👍

Just left a couple comments + it might be great to add an acceptance test as well (packages/core/test/acceptance/i18n_spec.ts).

@petebacondarwin petebacondarwin added action: presubmit The PR is in need of a google3 presubmit action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Oct 13, 2021
@petebacondarwin petebacondarwin marked this pull request as ready for review October 13, 2021 20:47
@petebacondarwin
Copy link
Member Author

@AndrewKushnir - I added the tests and comments - PTAL - and can you run a presubmit?

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@petebacondarwin FYI I've left a couple comments and started a presubmit (I'd also need to verify extraction mechanisms separately).

While fully dynamic bound properties (and attributes) cannot be marked for localization, properties that only contain interpolation can.

This commit ensure that attribute bindings that only contain interpolation can also be marked for localization.

Closes angular#43260
@petebacondarwin petebacondarwin added area: compiler Issues related to `ngc`, Angular's template compiler and removed action: review The PR is still awaiting reviews from at least one requested reviewer area: compiler Issues related to `ngc`, Angular's template compiler labels Oct 14, 2021
@AndrewKushnir
Copy link
Contributor

@petebacondarwin FYI presubmits went well and extra testing was also successful, so this PR is in a good-to-go state from g3 perspective. Thank you.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Oct 15, 2021
@petebacondarwin petebacondarwin added the action: merge The PR is ready for merge by the caretaker label Oct 15, 2021
@AndrewKushnir
Copy link
Contributor

This PR was merged into the repository by commit bba0a87.

AndrewKushnir pushed a commit that referenced this pull request Oct 18, 2021
)

While fully dynamic bound properties (and attributes) cannot be marked for localization, properties that only contain interpolation can.

This commit ensure that attribute bindings that only contain interpolation can also be marked for localization.

Closes #43260

PR Close #43815
AndrewKushnir pushed a commit that referenced this pull request Oct 18, 2021
)

While fully dynamic bound properties (and attributes) cannot be marked for localization, properties that only contain interpolation can.

This commit ensure that attribute bindings that only contain interpolation can also be marked for localization.

Closes #43260

PR Close #43815
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this pull request Oct 18, 2021
…ngs (angular#43815)"

This reverts commit bba0a87.

The reason for rollback: this change is breaking some targets in Google's codebase when there is no attribute value is displayed (attr.aria-label) when translated.
@AndrewKushnir
Copy link
Contributor

Reopened due to the rollback, see #43882.
We'll need to run a global presubmit for this change before we try to land it next time.

@AndrewKushnir AndrewKushnir reopened this Oct 18, 2021
@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit state: blocked and removed action: merge The PR is ready for merge by the caretaker labels Oct 18, 2021
AndrewKushnir added a commit that referenced this pull request Oct 18, 2021
…ngs (#43815)" (#43882)

This reverts commit bba0a87.

The reason for rollback: this change is breaking some targets in Google's codebase when there is no attribute value is displayed (attr.aria-label) when translated.

PR Close #43882
AndrewKushnir added a commit that referenced this pull request Oct 18, 2021
…ngs (#43815)" (#43882)

This reverts commit bba0a87.

The reason for rollback: this change is breaking some targets in Google's codebase when there is no attribute value is displayed (attr.aria-label) when translated.

PR Close #43882
AndrewKushnir added a commit that referenced this pull request Oct 18, 2021
…ngs (#43815)" (#43882)

This reverts commit bba0a87.

The reason for rollback: this change is breaking some targets in Google's codebase when there is no attribute value is displayed (attr.aria-label) when translated.

PR Close #43882
@AndrewKushnir AndrewKushnir self-assigned this Oct 18, 2021
@AndrewKushnir
Copy link
Contributor

Next steps: @AndrewKushnir to investigate in g3 further and come up with a test case that we can use to avoid regressions.

@AndrewKushnir
Copy link
Contributor

@petebacondarwin I've performed the necessary investigation and found the origin of the problem. The template that can be used for repro:

<div i18n-attr.aria-label attr.aria-label="hello {{name}}"></div>

It's important to have an attribute that doesn't directly translate to a property. The underlying problem is that the i18nAttributes instruction that we generate actually sets a property on an element, not an attribute (taking an advantage that attribute translations were not supported by the compiler), see:

case I18nUpdateOpCode.Attr:
const propName = updateOpCodes[++j] as string;
const sanitizeFn = updateOpCodes[++j] as SanitizerFn | null;
const tNodeOrTagName = tView.data[nodeIndex] as TNode | string;
ngDevMode && assertDefined(tNodeOrTagName, 'Experting TNode or string');
if (typeof tNodeOrTagName === 'string') {
// IF we don't have a `TNode`, then we are an element in ICU (as ICU content does
// not have TNode), in which case we know that there are no directives, and hence
// we use attribute setting.
setElementAttribute(
lView[RENDERER], lView[nodeIndex], null, tNodeOrTagName, propName, value,
sanitizeFn);
} else {
elementPropertyInternal(
tView, tNodeOrTagName, lView, propName, value, lView[RENDERER], sanitizeFn,
false);
}
break;

We end up calling elementPropertyInternal. It worked for the test case that we had, since:

const div = document.createElement('div');
div.title = 'abc';
div.getAttribute('title'); // outputs 'abc'

But for attributes like aria-label, that would not work (we'd just set a property, which would actually be incorrect).

It looks like the mentioned code in the i18n_apply.ts should have a clear indication when we should set an attribute vs a property for an element (in addition to the current if check that we have there). We might consider introducing 2 instructions: i18nAttributes and i18nProperties, which would use different OpCodes, so that the code in the i18n_apply.ts can differentiate between attributes and properties.

@AndrewKushnir
Copy link
Contributor

AndrewKushnir commented Oct 26, 2021

FYI we also have a code that maps attribute names that don't correspond to their element property names:

https://github.com/angular/angular/blob/master/packages/core/src/render3/instructions/shared.ts#L1004

So we can potentially consider expanding the logic in the i18n_apply.ts (or the shared.ts) to have some extra mapping logic, but it would be either too heavy from code size perspective (if we just put 1:1 mapping for ARIA and other attributes) or too slow (if we try to apply some transform to an attribute name).

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Oct 26, 2021
Serginho pushed a commit to TuLotero/angular that referenced this pull request Jan 20, 2022
…ular#43815)

While fully dynamic bound properties (and attributes) cannot be marked for localization, properties that only contain interpolation can.

This commit ensure that attribute bindings that only contain interpolation can also be marked for localization.

Closes angular#43260

PR Close angular#43815
Serginho pushed a commit to TuLotero/angular that referenced this pull request Jan 20, 2022
…ngs (angular#43815)" (angular#43882)

This reverts commit bba0a87.

The reason for rollback: this change is breaking some targets in Google's codebase when there is no attribute value is displayed (attr.aria-label) when translated.

PR Close angular#43882
@amakhrov
Copy link

Should we reopen the original issue then (#43260)?
It currently looks like it's resolved, which is misleading (I actually ended up running through the changelog, and then looking at the PRs again, just to learn the fix has been reverted)

@AndrewKushnir
Copy link
Contributor

Thanks for the comment @amakhrov, I've reopened the mentioned issue.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: compiler Issues related to `ngc`, Angular's template compiler area: i18n cla: yes state: blocked target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

i18n doesn't play nicely with interpolated attr.attr-name
4 participants