How to use angular i18n custom IDs

Viewed 2936

In angular i18n documentation, it's recommended to set unique custom ids. But I have trouble understanding how to use them.

I understand that IDs are useful in order to prevent translation changes when you update the source language. And theses IDs should be unique. When the extractor find duplicated IDs, it only keep the first.
But I have a lot of repetitions in my app. Should I use the same ID for all duplicated sentences? Should I not use an ID for theses? Should I use a different ID for all, and translate each occurrence separately?

I guess that the better solution is to not use an ID for duplicated content, and leave the generated ID. But if I have a unique sentence, and my application change, then this sentence is not unique anymore, I will have to remove this ID and translate it again, right? I will have to be careful of what is unique and what is not. Does it seems ok?

2 Answers

Extract tool maps two instances of the same string into a single resource item. This has both advantages and disadvantages. It lets you to keep the duplicates strings at minimum but at the same time it may also be dangerous to use the same string in two or more contexts. This is why custom IDs let you control the extraction better. Unfortunately custom ID must be unique in order them to work. If you use the same custom ID twice the second string is just ignored even if the string value is different.

Because of these issues we take different approach. We add a meaning value to every i18n attributes. Then we tell our localization tool to combine the location context (also extracted to XLIFF with meaning and id values) with the meaning value to get the unique context. The context does not change even we fix a typo in the string. Also the meaning value needs to be unique only among the file it is used not among all other ids/meaning in the application.

Here is a sample

<h1 i18n="header|">Plural Sample</h1>

When extracted we get

<unit id="1835493080467832141">
  <notes>
    <note category="meaning">header</note>
    <note category="location">app/app.component.ts:2</note>
  </notes>
  <segment>
    <source>Plural Sample</source>
  </segment>
</unit>

It has meaning and location properties. The localization tool we use, Soluling, combines these two to get context:

messages.xlf.app\app.component.ts.header

This is fixed as long as we do not rename the source code file or change the meaning value. We can freely modify the string value. Also the context value is much more describing than the generated id:

1835493080467832141

This is how the sample looks in the localization tool.

enter image description here

Related