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?
Well, I'd use custom IDs only in case you need to have more (different) translations of one string. The documentation recommends using them in case you desire more human legibility and some translation context. I'd not worry about maintenance too much, if you used a tool to manage your translations (xliffmerge for example).
The xi18n
tool already does string equality matching, so if it finds more equal strings, it stuffs them under a single <trans-unit>
. You than provide a (target) translated string for this unit and it will be used in place of all the source strings. The IDs should not change between multiple runs of the localization tool since they are based on the content of the strings themselves.
So my suggestion is not to worry about the IDs and re-use too much. If you write just the strings, they will be matched together and the translation will remain the same for all of them. If you use custom IDs, you have to remember to use them and to maintain their translation manually.
The case of a change in a source string is (of course) something you have to take care of.
Let's have a simple case-study for the sake of completeness:
Let's say you have two identical strings in your app that have to have the same translation. You run the xi18n
, generate your messages.xlf
, merge it into an already translated file (e.g. messages.cs.xlf
), translate the <trans-unit>
and build and deploy your app.
Now, somebody comes and wants you to change one of these strings. Two cases can occur here - the source string has to be changed (and translated anew), or only the translation has to be changed (the source string remains the same).
In the first case you go to your code, update the source string and run the localization process again. A new <trans-unit>
will have been created - you provide a new translation, build the app and you are done.
In the second case, you go to the code and slap a custom ID (description and meaning are also recommended) onto the string that has to have a new translation. You run the localization process - a new <trans-unit>
(with the custom ID) will have been generated. You translate it as you are used to, build and deploy.