本文介绍了Angular 6-如何从打字稿中提取翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 6,并且根据此处找到的文档对翻译进行了设置处理: https://angular.io/guide/i18n .

I am using Angular 6 and I have setup handling of translations according to the documentation found here: https://angular.io/guide/i18n.

问题在于文档没有说明如何使用打字稿进行翻译.

The problem is that the documentation does not explain how to get translations using typescript.

这里有一个类似的问题:

There is a similar question here: Can I use Angular i18n to translate string literals in typescript code

但是我不能使用该答案,因为它依赖于ngx-translate,一旦Angular赶上了它将不推荐使用,请参见 https://github.com/ngx-translate/core/issues/495 .

But i cannot use that answer since it relies on ngx-translate which will be deprecated once Angular catches up, see https://github.com/ngx-translate/core/issues/495.

那么使用Angular 6 i18n-我将如何使用基于例如id的打字稿获得翻译后的文本?

So using the Angular 6 i18n - how would i get the translated text using typescript based on for example an id?

推荐答案

@Diemauerdk,该文档未解释如何使用打字稿获取翻译,因为这是不可能的.一个变通办法可以是在几个.ts中进行翻译.那是

@Diemauerdk, the documentation not explain how get translations using typescript because this is not possible. A work-around can be has in several .ts the translations. That's

//file locale-us.ts
export const translation:any={
   greeting:"Hello World!",
   mainTitle:"The best Appliacion of the world"
}
//file locale-es.ts
export const translation:any={
   greeting:"¡Hola Mundo!",
   mainTitle:"la mejor aplicación del mundo"
}

在您的.ts中,您可以拥有一个管道

In your .ts you can have a pipe

import { Pipe, PipeTransform } from '@angular/core';
import { translation } from '../../i18n/locale-us';


@Pipe({ name: 'translation' })
export class TranslationPipe implements PipeTransform {
    constructor() {}
    transform(value: string): string {
        return translation[value] || null;
    }
}

您可以在将构造函数注入管道的组件中使用

And you can use in a component where you inject the pipe in constructor some like

constructor(private translate: TranslationPipe){}
//And use like

alert(this.translate.transform('greeting'));

然后,您可以在angular.json中使用分开的"fileReplacements".不显示所有angular.json,否则您必须在其中添加fileReplacement.你有一个如果下载文档 https://angular.io/guide/i18n#internationalization-i18n

Then you can use the apart "fileReplacements" in angular.json. not show all the angular.json else where you must add the fileReplacement. You has a fe.g. of this if you download the i18n example of the documentation https://angular.io/guide/i18n#internationalization-i18n

  "configurations": {

   "production": {
        ...
    },
    "production-es": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }, //add this two lines
        {
          "replace": "src/i18n/locale-us.ts",
          "with": "src/i18n/locale-es.ts"
        }
      ],
      ...
      "outputPath": "dist/my-project-es/",
      "i18nFile": "src/locale/messages.es.xlf",
      ...
    },
    "es": {
      //And add this too
      "fileReplacements": [
        {
          "replace": "src/i18n/locale-us.ts",
          "with": "src/i18n/locale-es.ts"
        }
      ],
      ...

     "outputPath": "dist/my-project-es/",
      "i18nFile": "src/locale/messages.es.xlf",
      ...
    }
  }

这篇关于Angular 6-如何从打字稿中提取翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:14