Skip to content

i18n Support in Angular Library #29536

Closed
Closed

Description

@rahulrsingh09

Is this Possible with the current approach of angular libraries , can we have i18n support for Libraries so that i can build the lib using locale specific data .

Currently in the Angular Docs both these topics are under different Leaf Nodes and not related . But we feel it's a valid requirement of having Libraries work with i18n

Activity

  1. rmedaer commented on Mar 27, 2019

    @rmedaer

    It's (not yet) possible to do i18n in libraries (as far as I know). However, as explained by @ocombe it will be included with ivy.

    I recommend the following talk for more details : https://www.youtube.com/watch?v=miG-ghJhFPc

  2. rmedaer commented on Mar 27, 2019

    @rmedaer

    If you are using ng-packagr as builder/packager for your Angular libraries, is here your answer for current version (a.k.a without ivy): ng-packagr/ng-packagr#695

  3. rahulrsingh09 commented on Mar 27, 2019

    @rahulrsingh09
    Author

    @rmedaer Thanks for the links, So untill and unless Ivy comes to the rescue we have no other options? Also if we plan on usong ng-packager will be it possible to have i18n support for Libs as the link mentioned is still and open issue ? can you please shed some more light on the same .

  4. rmedaer commented on Mar 27, 2019

    @rmedaer

    Actually to answer I also have few questions:

    • Would you like to package your library with translations ?
    • (or) would you like to provide translations from your application ?
    • Are you compiling in AOT or JIT ?
    • Did you listen to ngx-translate ? Even if ivy will deprecate ngx-translate it might be an alternative for now.

    Also if we plan on usong ng-packager will be it possible to have i18n support for Libs as the link mentioned is still and open issue ?

    I guess ng-packagr guys will not work-hard on previous Angular renderer because ivy is planned for Q1/Q2 2019.
    About next angular release (8.0.0) I cannot answer this since new i18n on ivy is not yet finished. Again as far as I know (as external observer) @ocombe just started to implement translation loaders. Meaning that "provides translation to/from libraries" will depends on services in top of i18n compilation/configuration...

    I guess the best to advice you is @ocombe and actually he already did a lot here.

  5. added
    area: i18n
    on Mar 27, 2019
  6. added this to the needsTriage milestone on Mar 27, 2019
  7. rahulrsingh09 commented on Mar 29, 2019

    @rahulrsingh09
    Author

    @rmedaer
    w.r.t your questions these are things i am looking at

    Would you like to package your library with translations ? Yes
    (or) would you like to provide translations from your application ?( Maybe both if possible else No )
    Are you compiling in AOT or JIT ? AOT (Lib only complies in AOT if i am not wrong)
    Did you listen to ngx-translate ? Even if ivy will deprecate ngx-translate it might be an alternative for now.(Presently we are looking at ngx-translate Is it the only way as of now ..)

  8. fjoalland commented on Jul 31, 2019

    @fjoalland

    Any more news about this issue?

  9. SantiagoIlli commented on Sep 19, 2019

    @SantiagoIlli
  10. vgb1993 commented on Jan 29, 2020

    @vgb1993

    I also need my libs to provide their own translations.

  11. JanEggers commented on Apr 28, 2020

    @JanEggers

    @ocombe any news?

  12. ocombe commented on May 1, 2020

    @ocombe
    Contributor
  13. petebacondarwin commented on May 1, 2020

    @petebacondarwin
    Contributor

    So two PRs that are in flight to help support translations in libraries are:

    I am working a full tilt on this stuff, except for the odd ngcc fix.

  14. ozkoidi commented on May 13, 2020

    @ozkoidi

    Any news or estimated release date for this?

  15. 21 remaining items

  16. wasdJens commented on Jan 29, 2021

    @wasdJens

    For angular components from a separate library it works fine when you have the component tagged with i18n - When running ng xi18n inside your application that consumes the library you get the tags inside the .xlf for these components.

    But what about the usage of $localize inside a service for example? When running ng xi18n inside the library itself the translations are created but not when running it inside an application.
    Do we need to merge these two translation files or is the extraction of the $localize not supported and we can only have translations in components?

    Sorry to dig out such an old issue but I could only find information about localization with applications and not much about libraries.

  17. petebacondarwin commented on Jan 29, 2021

    @petebacondarwin
    Contributor

    @wasdJens - could you provide an example of this not working? I thought it should, since the $localize calls would make there way into your final application build, where the i18n extractor would grab them...

  18. wasdJens commented on Jan 29, 2021

    @wasdJens

    @petebacondarwin Gonna try to provide a sample library / app configuration since the working project is closed code.

  19. wasdJens commented on Feb 1, 2021

    @wasdJens

    Hey, sorry for late response from my side.

    I have tried to pinpoint my issue to maybe a misconfigured project / library.
    I have also created a sample repo with three angular projects inside:

    When running ng extract-i18n in the v11 app $localize seems to work and is extracted from the library.

    <?xml version="1.0" encoding="UTF-8" ?>
    <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
      <file source-language="en-US" datatype="plaintext" original="ng2.template">
        <body>
          <trans-unit id="6480943972743237078" datatype="html">
            <source>You have 10 users</source>
            <context-group purpose="location">
              <context context-type="sourcefile">../angular-localize-issue/dist/projects/test-lib/src/lib/sample-component/sample-component.component.ts</context>
              <context context-type="linenumber">9</context>
            </context-group>
          </trans-unit>
          <trans-unit id="8def8481e91291a52f9baa31cbdb313e6a6ca02b" datatype="html">
            <source>Hello i18n!</source>
            <context-group purpose="location">
              <context context-type="sourcefile">../angular-localize-issue/dist/projects/test-lib/src/lib/sample-component/sample-component.component.ts</context>
              <context context-type="linenumber">15</context>
            </context-group>
            <note priority="1" from="description">An introduction header for this sample</note>
          </trans-unit>
        </body>
      </file>
    </xliff>

    When installing the library in the other angular application (Running 10.2.4) and running ng xi18n (Since ng extract-i18n is not yet supported there according to the docs) I get the following response (Missing the $localize)

    <?xml version="1.0" encoding="UTF-8" ?>
    <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
      <file source-language="en-US" datatype="plaintext" original="ng2.template">
        <body>
          <trans-unit id="8def8481e91291a52f9baa31cbdb313e6a6ca02b" datatype="html">
            <source>Hello i18n!</source>
            <context-group purpose="location">
              <context context-type="sourcefile">node_modules/test-lib/lib/sample-component/sample-component.component.d.ts</context>
              <context context-type="linenumber">2</context>
            </context-group>
            <note priority="1" from="description">An introduction header for this sample</note>
          </trans-unit>
        </body>
      </file>
    </xliff>

    I can reproduce the same behavior in our real source code (Also running under angular 10.2.4). I am not quite sure if this is a problem with angular itself or the configuration I am using is incorrect. Our solution is most likely to bump to Angular 11 since we are not using any advanced features that are no longer supported or other issues.

  20. petebacondarwin commented on Feb 2, 2021

    @petebacondarwin
    Contributor

    Thanks for looking into this further @wasdJens.

    This sounds like it is working as expected in v11 and not (as expected) in v10. The Ivy extraction was not available in earlier versions of Angular. So indeed your solution is to update to the latest version.

    I am going to close this as it appears that there is no problem with the latest version of Angular.

  21. michaelhunziker commented on Feb 7, 2021

    @michaelhunziker

    @petebacondarwin: I just wanted to try to extract messages from my Angular library (project generated with the latest CLI, 11.1.4):
    ng extract-i18n test-lib (the documentation states that the last parameter is the name of the Angular library: https://angular.io/cli/extract-i18n)

    Unfortunately I get the following error:

    Project 'test-lib' does not support the 'extract-i18n' target.

    Versions:

    • @angular/localize: 11.1.2
    • @angular/core: 11.1.2

    Repo: https://github.com/michaelhunziker/angular-i18n-library/

    What am I doing wrong?

    Maybe you could also verify my understanding of the current Angular library translation process:

    1. Extract messages from the library using: ng extract-i18n test-lib. This results in a messages.xlf file containing only translations from that library 'test-lib'.
    2. Create language specific XLF translation files for the library.
    3. Publish the library to npm (including these XLF translation files)
    4. Consume the library in another app (e.g. 'some-app') as an npm dependency and use 36792 to merge the library translations files into the translation files of 'some-app'.
  22. destus90 commented on Feb 8, 2021

    @destus90
    Contributor
  23. michaelhunziker commented on Feb 8, 2021

    @michaelhunziker

    @destus90 I just added that target: michaelhunziker/angular-i18n-library@3a4be6e

    When running ng extract-i18n test-lib I now get:

    The "path" argument must be of type string. Received type undefined

    This seems to be a duplicate of angular/angular-cli#17140 (comment)

    Obviously ng extract-i18n is still not working for libraries and we have to use localize-extract according to this answer

  24. petebacondarwin commented on Feb 8, 2021

    @petebacondarwin
    Contributor

    @michaelhunziker - yes that is correct. I am sorry that the docs are a bit behind on this. And also that extracting messages from libraries is not straightforward.

  25. michaelhunziker commented on Feb 8, 2021

    @michaelhunziker
  26. petebacondarwin commented on Feb 8, 2021

    @petebacondarwin
    Contributor

    Glad to hear it.

  27. added a commit that references this issue on Mar 11, 2021
  28. angular-automatic-lock-bot commented on Mar 12, 2021

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  29. locked and limited conversation to collaborators on Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: i18nfeature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions