angular-cli: extract-i18n does not respect the i18nDuplicateTranslation option
š Bug report
Command (mark with an x)
- new
- build
- serve
- test
- e2e
- generate
- add
- update
- lint
- extract-i18n
- run
- config
- help
- version
- doc
Is this a regression?
No
Description
Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless of the value of the i18nDuplicateTranslation option in the build target.
š¬ Minimal Reproduction
This repository can be used to quickly reproduce this bug: https://github.com/reduckted/repro-extract-i18n-duplicates
Or using this archive: repro-extract-i18n-duplicates.zip
- Run
npm i - Run
ng extract-i18n
The template app.component.html contains two i18n messages with the same ID but different text. The angular.json file has the i18nDuplicateTranslation option set to "error" in the browser builder.
The output is:
WARNINGS:
- Duplicate messages with id "test":
- "first" : src\app\app.component.html:1
- "second" : src\app\app.component.html:2
š„ Exception or Error
I expect that with i18nDuplicateTranslation set to "error", the duplicate message IDs will be logged as errors and that the process exits with a non-zero exit code.
Instead, the duplicate message IDs are logged as a warning, and the process exits with an exit code of zero.
š Your Environment
> ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ ā³ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 14.1.0
Node: 16.12.0
Package Manager: npm 8.15.0
OS: win32 x64
Angular: 14.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1401.0
@angular-devkit/build-angular 14.1.0
@angular-devkit/core 14.1.0
@angular-devkit/schematics 14.1.0
@schematics/angular 14.1.0
rxjs 7.5.6
typescript 4.7.4
Anything else relevant?
Looking at the code, it seems like this might be a simple fix. š¤
The duplicate message IDs are checked here with a hard-coded value of āwarningā and then always logged as warnings: https://github.com/angular/angular-cli/blob/1b89fd43400455366a3054f6ada10fb0dae5a209/packages/angular_devkit/build_angular/src/builders/extract-i18n/index.ts#L273-L283
Earlier in the same function, the browser target options are fetched here, which should contain the value of the i18nDuplicateTranslation option:
https://github.com/angular/angular-cli/blob/1b89fd43400455366a3054f6ada10fb0dae5a209/packages/angular_devkit/build_angular/src/builders/extract-i18n/index.ts#L138-L141
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 16
- Comments: 24 (3 by maintainers)
Commits related to this issue
- fix(@angular/build): provide `extract-i18n` does not respect `i18nDuplicateTranslation` config value (#29179) This fixes an issue where the config was not passed. Closes #23635 (cherry picked from ... — committed to angular/angular-cli by Algoritm211 2 years ago
- fix(@angular/build): provide `extract-i18n` does not respect `i18nDuplicateTranslation` config value (#29179) This fixes an issue where the config was not passed. Closes #23635 — committed to angular-robot/angular-cli by Algoritm211 2 years ago
- fix(@angular-devkit/build-angular): respect i18nDuplicateTranslation option when duplicates exist Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless o... — committed to igord/angular-cli by deleted user 2 years ago
- fix(@angular-devkit/build-angular): respect i18nDuplicateTranslation option when duplicates exist Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless o... — committed to igord/angular-cli by deleted user 2 years ago
- fix(@angular-devkit/build-angular): respect i18nDuplicateTranslation option when duplicates exist Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless o... — committed to angular/angular-cli by deleted user 2 years ago
- fix(@angular-devkit/build-angular): respect i18nDuplicateTranslation option when duplicates exist Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless o... — committed to angular-robot/angular-cli by deleted user 2 years ago
- fix(@angular-devkit/build-angular): respect i18nDuplicateTranslation option when duplicates exist Running the extract-i18n command will always log duplicate i18n message IDs as warnings, regardless o... — committed to igord/angular-cli by deleted user 2 years ago
Some discussion about this option today. We definitely agree it should be possible to treat duplicate message IDs as an error during extraction.
What confused us somewhat is why this is an option to begin with? In the event of a duplicate translation which is ignored, Iām not sure we can do anything intelligent to pick between them. Any tool which processes the extracted output wonāt know which message to use. And when the CLI merges translations back in, we canāt know which one is right for the duplicated ID. I double checked with @AndrewKushnir, who also couldnāt recall a compelling reason for why a user would want to ignore such duplications.
Iām thinking we should delete the option altogether and just always treat duplicate translation IDs as a hard error, both at extraction and merge times.
I am hoping that since this issue is not closed, it will eventually be fixed. It would be nice to be able to break builds that introduce i18n bugs.
Just confirming my own thoughts here. With these files:
app.component.html:test.component.html:app.component.htmlThere are no warnings from
ng extract-i18nand this is the output:However, if I add some whitespace to one of the values, for example:
Then you get a warning, which is 100% what I would expect (although it should be an error š)
So you can use the same ID in multiple templates. You just have to make sure they have the same text value. If they donāt have the same text value, then you absolutely should be alerted about it via an error.
@reduckted, I donāt think our behavior needs to change there. Just like how no warning is emitted when the text matches, we donāt have to fail that case. My suggestion is to always error in the case where there are two messages with the same ID and different content.
On a separate point, if you have two locations where you want to use the same translations, I recommend using
$localizeinstead at one place in your code and just binding it in two locations. That way you have a single source of truth and you donāt have to hard-code an ID anywhere.We could expose a similar option to
i18nDuplicateTranslation. Let me check with the rest of the team.We are heavily using duplicates and itās pretty annoying in development mode, to have hundreds of āduplicate message idā warnings all the time. We should not rely on having to write precisely the same filler text, at least give us an option to ignore duplicate warnings.
This is still a problem for us in Angular 17.3
We are looking at implementing a redirect of the output from running āng extract-i18nā to a file and then searching for āDuplicate messagesā and aborting our CI build if found.
Not ideal, and brittle if that message text ever changes.
But what happens if I want to reuse a key on purpose? The same translation string? š I wanted to use the ignore function because its on purpose like I said