Visitar URL original
i18n ignore leading and trailing spaces · Issue #13114 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20220127020619/https://github.com/angular/angular/issues/13114
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

i18n ignore leading and trailing spaces #13114

Open
zh99998 opened this issue Nov 28, 2016 · 50 comments
Open

i18n ignore leading and trailing spaces #13114

zh99998 opened this issue Nov 28, 2016 · 50 comments

Comments

@zh99998
Copy link

@zh99998 zh99998 commented Nov 28, 2016 •

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

now ng-xi18n will generate xliff including spaces, for example there is an element

    <p i18n>
        复制
    </p>

it will generate

        <source>
                    复制
                </source>

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

@panuruj
Copy link

@panuruj panuruj commented Mar 6, 2017

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

@ocombe
Copy link
Contributor

@ocombe ocombe commented Mar 6, 2017 •

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).
What do you think @vicb ?

@ocombe
Copy link
Contributor

@ocombe ocombe commented Mar 20, 2017

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.

@vicb
Copy link
Contributor

@vicb vicb commented Mar 21, 2017

Something we should work on for the next major release

@Goodwine
Copy link
Contributor

@Goodwine Goodwine commented Mar 21, 2017

Another case that should be handled is that changing indentation affects the generated message id. I believe the message extractor should replace(/\s+/, ' ') (or something like that).

In the examples below, a span tag is updated to be surrounded by a div tag, which doesn't change the visible contents and therefore should preserve the same id, but it doesn't.

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 <pre> tag or css white-space:pre*, which I don't know what's the best way to solve without adding additional attributes to the tag that has i18n. e.g. <span i18n="desc" i18n-keepws>

@ocombe ocombe changed the title i18n ignore leading and tailing spaces i18n ignore leading and trailing spaces Apr 2, 2017
@ocombe ocombe mentioned this issue May 2, 2017
20 tasks
@ocombe
Copy link
Contributor

@ocombe ocombe commented Sep 19, 2017

@pkozlowski-opensource do you think that we could make this work with your changes for whitespaces removal?

@Jacksunwei
Copy link
Contributor

@Jacksunwei Jacksunwei commented Oct 27, 2017

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.

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@pmccloghrylaing
Copy link
Contributor

@pmccloghrylaing pmccloghrylaing commented Mar 20, 2018

@Jacksunwei would the existing ngPreserveWhitespaces attribute be sufficient?

pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
pmccloghrylaing added a commit to pmccloghrylaing/angular that referenced this issue Mar 22, 2018
Reduces duplicate messages being generated with different IDs.

Closes angular#13114
@EdwinMeijne
Copy link

@EdwinMeijne EdwinMeijne commented Apr 20, 2018

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?

@pmccloghrylaing
Copy link
Contributor

@pmccloghrylaing pmccloghrylaing commented Apr 20, 2018

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.

@ocombe
Copy link
Contributor

@ocombe ocombe commented Apr 22, 2018

That is true, this option will be used for runtime i18n with ivy

@maplion
Copy link

@maplion maplion commented Jul 11, 2018 •

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/

@tbunique
Copy link

@tbunique tbunique commented Apr 24, 2019

I'm using a XML beautifier for the moment after the extract.

@lazarljubenovic
Copy link
Contributor

@lazarljubenovic lazarljubenovic commented Aug 26, 2019

I'm using a XML beautifier for the moment after the extract.

Are there known issues with this workaround?

@crtl
Copy link

@crtl crtl commented Oct 8, 2019

The issue remains in 2019.

@anionDev
Copy link

@anionDev anionDev commented Sep 2, 2020

Are there any news for that issue? This would really be a very nice feature.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Sep 3, 2020

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.

@minijus
Copy link

@minijus minijus commented Feb 4, 2021

@petebacondarwin is there any update on this one?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Feb 8, 2021

I haven't had time to work on these yet.

@minijus
Copy link

@minijus minijus commented Feb 8, 2021

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.

  1. Additional extract-i18n option trimWhitespace (with false as default) - yes/no?
  2. Trimming all whitespace or collapsing it to single space char?
  3. Should any whitespace manipulation inside (not in the start or end) extracted string be considered?
  4. Should translation id generator receive original source value or trimmed one?
  5. Any references to code where you see changes should be done?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Feb 8, 2021

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.

  1. Additional extract-i18n option trimWhitespace (with false as default) - yes/no?
    I would imagine a new option (either on extract-i18n or perhaps in tsconfig.json?) We need to discuss this with the appropriate teams.
  2. Trimming all whitespace or collapsing it to single space char?
    Assuming we are trimming only at the start and end, then I would expect to remove all whitespace and not collapse it to a single space.
  3. Should any whitespace manipulation inside (not in the start or end) extracted string be considered?
    I think that this is covered by the current preservesWhitespace option in tsconfig?
  4. Should translation id generator receive original source value or trimmed one?
    There is a function called parseMessage() which should probably receive the untrimmed value, but then do the trimming before passing to the computeMsgId() function.
  5. Any references to code where you see changes should be done?
    The message digest computation is done at
    const messageId = metadata.customId || computeMsgId(messageString, metadata.meaning || '');
    .

@PowerKiKi
Copy link
Contributor

@PowerKiKi PowerKiKi commented Mar 10, 2021

Should any whitespace manipulation inside (not in the start or end) extracted string be considered?

Absolutely yes, otherwise this feature would lose a lot of its value. And I think this must be independent of the existing preservesWhitespace option in tsconfig.

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 Some text. Another Text. in all cases.

@stefan-schweiger
Copy link

@stefan-schweiger stefan-schweiger commented Apr 22, 2021 •

@PowerKiKi what about Text<span i18n> Some text. Another Text. </span>Text? There is the possibility that this whitespace was intended.

@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 --html-whitespace-sensitivity. This has the following options:

  • css - Respect the default value of CSS display property.
  • strict - All whitespace is considered significant.
  • ignore - All whitespace is considered insignificant.

https://prettier.io/blog/2018/11/07/1.15.0.html

The current default of prettier is css which makes it extra annoying to fix whitespaces, because it keeps formatting block-elements to be multiline and introduces whitespaces which you can't really remove while still using prettier.
But for a <span>-tag it is often intended to have whitespaces at the front or end.

@PowerKiKi
Copy link
Contributor

@PowerKiKi PowerKiKi commented Apr 22, 2021

For

<span i18n> Some text. Another Text.</span>

the message ID must be exactly Some text. Another Text, like all other cases I mentioned.

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>

@stefan-schweiger
Copy link

@stefan-schweiger stefan-schweiger commented Apr 22, 2021 •

@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 extract-i18n should have more options besides on/off, but having an attribute would be very useful in any case 👍

@ricardosaracino
Copy link

@ricardosaracino ricardosaracino commented Apr 22, 2021

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 i18n attributes behavior.

something comparable to:

"angularCompilerOptions": { 
  "preserveWhitespaces": false 
} 

@ibrcic
Copy link

@ibrcic ibrcic commented Nov 4, 2021

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 🙂

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021 •

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>

@ricardosaracino
Copy link

@ricardosaracino ricardosaracino commented Nov 13, 2021

close to resolving this. But frustratingly still has some sharp edges. For example. Consider the following examples:

Is this from angular 13?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021 •

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.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021

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).

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021 •

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!

@stefan-schweiger
Copy link

@stefan-schweiger stefan-schweiger commented Nov 13, 2021

@petebacondarwin so this would still not provide a (on demand) solution to ignore leading and trailing whitespaces altogether if I understand it correctly right?

@ricardosaracino
Copy link

@ricardosaracino ricardosaracino commented Nov 13, 2021

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

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021

close to resolving this. But frustratingly still has some sharp edges. For example. Consider the following examples:

Is this from angular 13?

The new i18n message IDs were introduced with Ivy in v9 but is hidden behind a flag. You need to ensure that "enableI18nLegacyMessageIdFormat": false, was set in your tsconfig.json.

All new CLI projects have this flag set to false.

If you are switching from the legacy ID by changing this to false you should read https://angular.io/guide/migration-legacy-message-id

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021

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

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.

@stefan-schweiger
Copy link

@stefan-schweiger stefan-schweiger commented Nov 13, 2021

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

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.

<button i18n longListOfOtherProps>
    My Button
</button>

@PowerKiKi
Copy link
Contributor

@PowerKiKi PowerKiKi commented Nov 13, 2021

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 <pre> text, you can easily specify i18n-pre instead of i18n.

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.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Nov 13, 2021 •

There are two possible pitfalls here:

  1. If you don't know where the message will appear then removing leading and trailing whitespace in the actual translated string could cause rendering differences... consider
a<button i18n>b</button>c
a<button i18n> b </button>c

Which (literally) gets rendered as follows:

abc
a b c

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.

  1. What should happen when there are elements within an i18n message?
<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 b text have the whitespace removed so that these two translations are identical?

@maplion
Copy link

@maplion maplion commented Nov 13, 2021 •

@petebacondarwin A resounding yes: "b" and " b " should be the same translation. If one explicitly wants spaces, they can add &nbsp; after the <span> and put i18n on the span instead. Additionally, one could split up "a", "b" and "c" into separate i18n spans. I find that in general, there are less issues if no html is included in your i18n tag.

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

@Goodwine
Copy link
Contributor

@Goodwine Goodwine commented Nov 13, 2021

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 🤷🏽‍♂️

@maplion
Copy link

@maplion maplion commented Nov 13, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet