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
i18n ignore leading and trailing spaces #13114
Comments
|
The more serious issue is that the ID takes the white space into account, and the ID could change just because of a change in the white space (e.g. indentation). The change in the ID prevents an existing translation from being used. I know that there is a feature to manually override the ID, but it would great if the generated ID can be more stable. CC: @ocombe |
|
We could probably ignore the leading and trailing spaces yes, that would actually decrease the number of duplicated translations when we do the extraction and make the translations easier to do (with some software it's hard to reproduce the line breaks/tabs when you translate). |
|
Another issue due to this: <h4 i18n>
{ids.length, plural,
=0 {You have not selected any country yet.}
=1 {You have selected the following country: }
other {You have selected the following countries: }
}
</h4>will generate 2 messages after extraction: <messagebundle>
<msg id="203167065070819173">
<ph name="ICU"><ex>ICU</ex></ph>
</msg>
<msg id="5290485580948134395">{VAR_PLURAL, plural, =0 {You have not selected any country yet.} =1 {You have selected the following country: } other {You have selected the following countries: } }</msg>
</messagebundle>because there are line breaks before and after the ICU expression. Removing them would fix this. |
|
Something we should work on for the next major release |
|
Another case that should be handled is that changing indentation affects the generated message id. I believe the message extractor should In the examples below, a Example: <!-- initial version -->
<span i18n>
Some text.
Another Text.
</span>
<!-- updated version -->
<div>
<span i18n>
Some text.
Another Text.
</span>
</div>These will generate something like: <!-- initial version -->
<messagebundle>
<msg id="123">
Some text.
Another Text.
</msg>
</messagebundle>
<!-- updated version -->
<messagebundle>
<msg id="456">
Some text.
Another Text.
</msg>
</messagebundle>One would expect that the actual generated message trims whitespaces (as OP suggests), and also transforms multiple whitespaces into a single one: <messagebundle>
<msg id="123">Some text. Another Text.</msg>
</messagebundle>Of course there is this problem then when the text is surrounded by a |
|
@pkozlowski-opensource do you think that we could make this work with your changes for whitespaces removal? |
|
I'd like to echo on this proposal. It seems stripping consecutive spaces is a more common use case. We can use additionally suffix (i18n-keepws) to tell message extractor not to do so if needed. |
|
@Jacksunwei would the existing |
Reduces duplicate messages being generated with different IDs. Closes angular#13114
Reduces duplicate messages being generated with different IDs. Closes angular#13114
Reduces duplicate messages being generated with different IDs. Closes angular#13114
Reduces duplicate messages being generated with different IDs. Closes angular#13114
Reduces duplicate messages being generated with different IDs. Closes angular#13114
Reduces duplicate messages being generated with different IDs. Closes angular#13114
|
I am encountering this issue a lot when working in the HTML - many language entries are incorrectly marked as changed resulting in occasional merge conflicts that cost a lot of time to solve. Will the recent work by @pmccloghrylaing offer a solution for this? |
|
I abandoned the work I started on it. I believe that area of code is being refactored as part of Ivy and in order to support runtime i18n. |
|
That is true, this option will be used for runtime i18n with ivy |
|
For those interested, I built something for myself that works pretty well and made it available: as part of the process, it trims and flattens the source and target tags: https://github.com/maplion/angular6-i18n-helpers/ |
|
I'm using a XML beautifier for the moment after the extract. |
Are there known issues with this workaround? |
|
The issue remains in 2019. |
|
Are there any news for that issue? This would really be a very nice feature. |
|
Hi everyone. This has not been on my radar but it sounds like it might be a good idea to implement it. I think we could put it behind some kind of flag to prevent a breaking change. In the not-too-distant future we also need to migrate away from our legacy message ids, so this could also be part of that work. Since this is not a trivial piece of work, I am not sure right now when it will be implemented. Sorry not to give you any better indication, right now. |
|
@petebacondarwin is there any update on this one? |
|
I haven't had time to work on these yet. |
Thanks for quick response! Could you please share your tech insights on this one? This could help motivate someone contributing with an implementation.
|
Great questions! This is one of the reasons why this has not yet been implemented. There are not necessarily good answers to them all.
|
Absolutely yes, otherwise this feature would lose a lot of its value. And I think this must be independent of the existing Building on #13114 (comment), all of the following examples must have exactly the same ID: <span i18n>Some text. Another Text.</span>
<span i18n>
Some text. Another Text.
</span>
<span i18n>
Some text.
Another Text.
</span>
<div>
<span i18n>
Some text.
Another Text.
</span>
</div>The exact string used to generate the message ID must be exactly |
|
@PowerKiKi what about @petebacondarwin That said I think angular should follow the prettier formatter (which apparently uses the angular-html-parser) when it comes to whitespace and have the option to set
https://prettier.io/blog/2018/11/07/1.15.0.html The current default of prettier is |
|
For <span i18n> Some text. Another Text.</span>the message ID must be exactly If whitespace is intended, then you'll have to specify it with a new attribute like so (or some equivalent): <span i18n-preserve-whitespace> Some text. Another Text.</span> |
|
@PowerKiKi I like the idea of being able to control it with a property like you mentioned. I still think the preserve-whitespace command line argument for |
|
I would go one further and just have it as a global config, (perhaps) with the option to override. You cant really just change the existing something comparable to: "angularCompilerOptions": {
"preserveWhitespaces": false
} |
|
I also encounter this issue frequently, especially when using the same ID multiple times where sometimes the text is on new line and sometimes it is not. Is there any work being done or planned for this? It has been 5 years after all |
|
The new message id format based on the Ivy localization approach is very close to resolving this. But frustratingly still has some sharp edges. For example. Consider the following examples: <p i18n>复制</p>results in: <trans-unit id="7733598902834788488" datatype="html">
<source>复制</source>
</trans-unit><p i18n>
复制
</p>results in <trans-unit id="1300322071046508880" datatype="html">
<source> 复制
</source>
</trans-unit> <p i18n>
复制
</p>results in <trans-unit id="4994019382382194096" datatype="html">
<source> 复制 </source>
</trans-unit> <p i18n>
复制
</p>results in <trans-unit id="4994019382382194096" datatype="html">
<source> 复制 </source>
</trans-unit><p i18n> 复制 </p>results in <trans-unit id="4994019382382194096" datatype="html">
<source> 复制 </source>
</trans-unit><p i18n>
复制</p>results in <trans-unit id="6609365850414455956" datatype="html">
<source> 复制</source>
</trans-unit> |
Is this from angular 13? |
|
The thing to note here is that the new message id is generated from the result of the Angular compiler applying its own whitespace removal strategy, which is not entirely stable to whitespace changes. For example, you can see that once there is significant whitespace either side of the text of a message then that will be collapsed to a single space. E.g. the following: <p i18n> 复制 </p>
<p i18n> 复制 </p>
<p i18n> 复制
</p>
<p i18n>
复制
</p>will all be converted to: <p i18n> 复制 </p>and will have the same message id. |
|
But if there is no whitespace or (and this is probably a bug) only a newline then we get different results. E.g. <p i18n>复制</p>
<p i18n>复制
</p>
<p i18n> 复制</p>will all have different message ids (that are also different to the one above). |
|
The point is that the message id syntax is now directly correlated to the way the component template will be rendered into the DOM at runtime. So to fix these sharp edges we just need to decide how to modify the Angular compiler's whitespace squashing. Be aware that whitespace squashing is very subjective. Different tools squash differently; and also different elements (e.g. inline vs block) get rendered differently - even for the same HTML! |
|
@petebacondarwin so this would still not provide a (on demand) solution to ignore leading and trailing whitespaces altogether if I understand it correctly right? |
|
99% of the time i would want leading and trailing whitespace removed and the rest squashed. why not have i18n and i18n-pre (to preserve white space). in conjunction with my previous suggestion of using a config to NOT preserve white space |
The new i18n message IDs were introduced with Ivy in v9 but is hidden behind a flag. You need to ensure that All new CLI projects have this flag set to If you are switching from the legacy ID by changing this to false you should read https://angular.io/guide/migration-legacy-message-id |
Components have a setting to preserve whitespace within their templates. I feel like adding another one (or an additional tag) would create confusion - why would you want to preserve whitespace when rendering but not when matching message ids? I think it would be better if we could make the whitespace collapsing of Angular in general more reliable, then only need one way to configure this. |
For example if I have a button it's ignoring leading/trailing whitespaces anyways. So they don't get rendered. I still want them in my code because the formatting of the code is more readable. But since they are not rendered and are only "cosmetic" why should they be in the message ids? If I now switch the button to anything else the whitespaces suddenly get rendered because they were in my translation files, even though we only have them to make the code look nicer. |
|
From an i18n perspective, I can't imagine a reasonable and common enough use-case to keep leading and trailing whitespace and to not squash the rest. We should always trim and squash, regardless of whether the specific HTML element is block or inline, or has some custom CSS applied to it (!). In the very rare case where you do want to output a IMHO all of Pete's example should ended up as: <trans-unit id="7733598902834788488" datatype="html">
<source>复制</source>
</trans-unit>IMHO anything else than that would not entirely solve the issue here. |
|
There are two possible pitfalls here:
a<button i18n>b</button>c
a<button i18n> b </button>cWhich (literally) gets rendered as follows: abc If you strip the leading and trailing whitespace off only the i18n message (but not the original text being rendered), then the translation will end up being rendered differently to the original.
<p i18n>a<span> b </span>c</p>This results in the following translation units (simplified for clarity): <source>a<x id="START_TAG_SPAN"/> b <x id="CLOSE_TAG_SPAN"/>c</source>
<source>a<x id="START_TAG_SPAN"/>b<x id="CLOSE_TAG_SPAN"/>c</source>Should the |
|
@petebacondarwin A resounding yes: "b" and " b " should be the same translation. If one explicitly wants spaces, they can add As an aside, I have updated my own tools about 4 months ago for my own use with Angular 12 and you can find them here if you wish to leverage them: https://github.com/maplion/angular-i18n-helpers |
|
My opinion and nitpick is that "if you want spaces you can add them around the span" is not i18n friendly because you are making assumptions about all languages behaving the same The best one can do is generate these IDs according to how the browser renders data, in other words collapsing whitespaces unless explicitly set otherwise My employer requires me to say that this is not a contribution 🤷🏽♂️ |
|
@Goodwine agreed, which is why I prefer the second option: put your tags at the lowest level (no nested html) and manage it that way. The browser often ignores non-explicit spaces anyway. |


I'm submitting a ... (check one with "x")
Current behavior
now ng-xi18n will generate xliff including spaces, for example there is an element
it will generate
it's really weak to rely on spaces since they could changed Unintentionally or even by IDE reformat code.
Expected behavior
ignore leading and tailing spaces
The text was updated successfully, but these errors were encountered: