简体   繁体   English

ngx-translate:如何为 html 和 ts 文件中的翻译提供上下文?

[英]ngx-translate : How to provide context for translations in html and ts files?

I am using ngx-translate to support I18N in my angular 5 application.我在 angular 5 应用程序中使用ngx-translate来支持 I18N。

I wanted to know how i can specify the context for the word /sentence getting translated?我想知道如何指定要翻译的单词 /sentence 的上下文?

Example code from HTML:来自 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:来自 TS 文件的示例代码:

fTitle = TRANSLATE("Completion rate");

"TRANSLATE" is a service which extracts the text from translation. “TRANSLATE”是一项从翻译中提取文本的服务。 Below is the code:下面是代码:

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

I am using " ngx-translate-extract " to extract strings for translation.我正在使用“ ngx-translate-extract ”来提取字符串进行翻译。 Below is the command from package.json:下面是来自 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.提前致谢。

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 https://www.npmjs.com/package/@shavenzov/ngx-translate-extract

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM