简体   繁体   English

打字稿中的Angular 2和i18n

[英]Angular 2 and i18n in typescript

I saw that angular2 implements i18n for its components and that by using i18n (and all its related attributes like i18n-title, plurals etc ...) you can internationalize your html templates. 我看到angular2为其组件实现了i18n,并且通过使用i18n (及其所有相关属性,如i18n-title,复数等...),您可以将您的html模板国际化。

But i was wondering how can you internationalize strings that are provided by your typescript code. 但我想知道如何将打字稿代码提供的字符串国际化。 For example i have a popup where depending on the error that is throw by my backend i print in it a different message. 例如,我有一个弹出窗口,根据我的后端抛出的错误,我在其中打印一个不同的消息。 That message is provided by a variable binding to my template and i did not found a way with angular2 internationalization to translate that text. 该消息由绑定到我的模板的变量提供,我没有找到使用angular2国际化来翻译该文本的方法。

Quick example for better undestanding: 更好的未完成的快速示例:

 typescript:
 errorMessage: string = '';
 private errorMapping: Map<number,String>;

 onError(error): void {
   this.errorMessage = errorMapping.get(error.code);
 }

 template:
 <pop-up>{{errorMessage}}</pop-up>

Is there a way to do it ? 有办法吗?

If not am I the one implementing it in a bad way ? 如果不是我是一个以糟糕的方式实施它的人? Should i do it differently ? 我应该采用不同的方式吗?

Thank you very much for your time and answers. 非常感谢你的时间和答案。

This is an old question, but Angular 5 still does not provide support for this, and like the original questioner I am also trying to avoid using 3rd party libraries, my workaround was as follows... 这是一个老问题,但Angular 5仍然没有为此提供支持,并且像原始提问者一样,我也试图避免使用第三方库,我的解决方法如下......

  1. Define a service class called TranslationsService which has a public method to accept a key-value Map of translations, and another public method to retrieve a translation for a given key. 定义一个名为TranslationsService的服务类,它具有一个公共方法来接受一个键值的翻译映射,另一个公共方法来检索给定键的翻译。
  2. In your entry page html, ie app.component.html , add a <span> for each translation that you may wish to access programatically anywhere in your application, ie... 在您的输入页面html(即app.component.html ,为您希望在应用程序的任何位置以编程方式访问的每个翻译添加<span> ,即...

    <span class="translation" #error_1200 i18n="@@error_1200">A Message already exists with this Name</span>

  3. In your app.component.css , add the following so that your static list of translations as defined above is not actually shown in the browser on entry app.component.css ,添加以下内容,以便在输入时实际显示上面定义的静态翻译列表

    .translation {display: none;}

  4. Use Angular's native i18n support to define the required translations for the spans you defined above in the normal way that you would do for any translatable text in any html file in your application. 使用Angular的本机i18n支持为您在上面定义的跨度定义所需的翻译,以正常方式为应用程序中任何html文件中的任何可翻译文本进行定义。

  5. In your app.component.ts you can bind to the translations that you defined in the html, get the text from them and build up a Map of translations that can be passed to your Translations service. app.component.ts您可以绑定到您在html中定义的翻译,从中获取文本并构建可以传递给Translations服务的翻译地图。 Since this is done in the entry component, it will be then be available from any components in your application that need to access one of these translations. 由于这是在入口组件中完成的,因此可以从应用程序中需要访问其中一个转换的任何组件中获取它。 Code as below: 代码如下:

     @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { @ViewChild('error_1200') error1200 : ElementRef; constructor(private translationsService: TranslationsService) { } ngOnInit() { let translations = new Map<string, string>(); translations.set('error_1200', this.error1200.nativeElement.textContent); this.translationsService.setTranslations(translations); } } 
  6. Whichever components in your app require one of these translations, just inject your Translations service and use it to retrieve what you need using the appropriate key 您的应用中的哪个组件需要其中一个翻译,只需注入您的翻译服务并使用它来使用适当的密钥检索您需要的内容

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

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