3

I am using ngx-translate to support I18N in my angular 5 application.

I wanted to know how i can specify the context for the word /sentence getting translated?

Example code from HTML:

<span class="arc-gauge-title">{{title | translate}}</span>
<app-change-pages-dropdown [pageTitle]="pageTitle | translate"></app-change-pages-dropdown>

Example code from TS file:

fTitle = TRANSLATE("Completion rate");

"TRANSLATE" is a service which extracts the text from translation. Below is the code:

    export function TRANSLATE(str: string) {
    return str;
}

I am using "ngx-translate-extract" to extract strings for translation. Below is the command from package.json:

"extract-translations": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json --marker TRANSLATE"

Any help in specifying the context for the string to be translated is appreciated.

Thanks in advance.

user2439903
  • 1,277
  • 2
  • 34
  • 68
  • What do you mean by "context"? It isn't very clear from your explanation. – Tsvetan Ganev Apr 19 '18 at 20:53
  • 1
    @Tsvetan Ganev: somehting similar to what is mentioned in this link: https://angular.io/guide/i18n#help-the-translator-with-a-description-and-meaning (To translate a text message accurately, the translator may need additional information or context.) Can we add a description of the text message as the value of the 'translate' attribute? – user2439903 Apr 19 '18 at 20:57
  • I don't think so. ngx-translate has a simpler key-value translation schema. If you need need more advanced features, you would better use the official Angular i18n. – Tsvetan Ganev Apr 19 '18 at 21:08
  • I don't believe `ngx-translate` supports files that would contain this 'context'. They only support key/value pairs where the key is a `string` and the value is either a `string` or an object of the key/value pairs. Looking at the unit test code for the `ngx-translate-extract` library, I don't see any examples that would consist of this type of 'context' specification so I don't think they support that: https://github.com/biesbjerg/ngx-translate-extract/blob/master/tests/parsers/pipe.parser.spec.ts – Daniel W Strimpel Apr 19 '18 at 21:08
  • 1
    @Tsvetan Ganev: Yes, Angular i18n was the first i evaluated, but it does not have support for translation in TS file and also have to compile app separately for different languages, which I don't want to do. So ngx-translate was the fallback. – user2439903 Apr 19 '18 at 21:09
  • @Tsvetan Ganev / Daniel W Strimpel: Any suggestion on other i18n libraries which supports providing context to translator? – user2439903 Apr 19 '18 at 21:12
  • I'm not aware of any. I have only used the `ngx-translate` library but we use a web service that returns the i18n strings via service not the default way of json files in the assets folder. On the server we have all of those details but we don't return them with the json response. – Daniel W Strimpel Apr 19 '18 at 21:18

1 Answers1

0

You can use forked version where you can specify string context and comment.

You can do it like this:

<div translate-context="US State" translate-comment="Please, translate it as US STATE." translate>
  US State.Georgia  
</div>

<div translate-context="Country" translate-comment="Please, translate it as COUNTRY." translate>
    Country.Georgia
</div>

https://www.npmjs.com/package/@shavenzov/ngx-translate-extract

Tenuj
  • 1