[Ivy] [i18n] Translated messages are not loaded for library components
See original GitHub issue🐞 bug report
Is this a regression?
Yes, the translated messages are picked up by Angular 6-8 and in Angular 9 with Ivy disabled.
Description
Messages for third-party components are extracted successfully when running ng xi18n, but the compiler fails to locate the translated strings during compilation.
Possibly related: https://github.com/angular/angular/issues/32311, https://github.com/angular/angular/pull/32881
🔬 Minimal Reproduction
- Clone https://github.com/tsvetomir/ivy-i18n-messages
- Run
npm ci - Run
ng serve --configuration=esorng serve --configuration=es,production
Result:
- None of the messages declared in
CalendarModuleare translated. - The message declared in the application (“Hello, World!”) is translated.
- If
enableIvyis set to false intsconfig.jsonmessages are translated normally.
🔥 Exception or Error
No run-time errors.
🌍 Your Environment
Angular Version:
Angular CLI: 9.0.0-rc.3
Node: 12.13.1
OS: darwin x64
Angular: 9.0.0-rc.3
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.0-rc.3
@angular-devkit/build-angular 0.900.0-rc.3
@angular-devkit/build-optimizer 0.900.0-rc.3
@angular-devkit/build-webpack 0.900.0-rc.3
@angular-devkit/core 9.0.0-rc.3
@angular-devkit/schematics 9.0.0-rc.3
@angular/localize 9.0.0-rc.0
@ngtools/webpack 9.0.0-rc.3
@schematics/angular 9.0.0-rc.3
@schematics/update 0.900.0-rc.3
rxjs 6.5.3
typescript 3.6.4
webpack 4.41.2
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Some i18n translations are not displayed after enabling ivy
I recently upgraded Angular 8 project to Angular 9. Some strings in Angular 9 with ivy enabled are not getting translated.
Read more >Angular localization with Ivy
This resulted in awkward workarounds where artificial components were created purely to hold text that would be translated.
Read more >Angular localization with Ivy - Gaikwad Shrutika - Medium
With Ivy, you can compile components more independently of each other. ... Finally, it was not possible to load translations at runtime, ...
Read more >Angular localization with Ivy from Angular Blog - Medium
This resulted in awkward workarounds where artificial components were created purely to hold text that would be translated.
Read more >Angular Localization with Ivy - iFour Technolab
Furthermore, it was not possible to mark text in the translation application, only text in component templates, this resulted in difficult workarounds where ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Some background…
The “legacy message id format” (used for XLIFF 1.2) has message ids that can only be computed from the original template source. By the time we have compiled a component to ivy there is not enough information left to compute these ids.
The code generated by the Angular compiler (which is used by ngcc) looks like:
We can compute “non-legacy” message ids from the information in this generated code, but not legacy message ids. The workaround is that we can tell the Angular compiler that we want to use legacy message ids, and so it will compute them from the template and add them to the generated code. Resulting in code that looks like:
Note the addition of
@@2df64767cd895a8fabe3e18b94b5b6b6f9e2e3f0, which is the legacy message id.For this to happen the Angular compiler needs to know that legacy ids are required, ahead of the actual translation merging, which will happen much later in the build pipeline (at the end, in fact).
Currently ngcc has no way to know that the final application is expecting legacy message ids and so is not telling the Angular compiler to add them.
I think I know what is going on here…
This is what the warnings look like:
Note that the message IDs are not XLIFF 1.2 format, which is the format that the translation file is using.
Normally when compiling we convert the IDs to use the appropriate message ID format for XLIFF 1.2 (aka
i18nLegacyMessageIdFormat) which would mean that the compiled template uses the correct message ids.The problem here is that these templates are being compiled by ngcc, which doesn’t know to use the
i18nLegacyMessageIdFormat, so it is just using the regular message id format for these templates.I think we should be able to get the CLI to tell ngcc what format to use…