用于占位符文本的 Angular2 i18n

Angular2 i18n for placeholder text

有没有办法使用 Angular 2 的 i18n 翻译输入文本字段的占位符文本?

<input placeholder="hello world" i18n>

我在文档中没有看到任何关于此的信息: https://angular.io/docs/ts/latest/cookbook/i18n.html

已经有例子了,不过我好像找不到了。

您应该可以使用 i18n-attributename。例如:

<input type="number" placeholder="From" i18n-placeholder="From placeholder"/>

需要这样的条目:

<trans-unit id="generatedId" datatype="html">
  <source>From</source>
  <target state="translated">Van</target>
  <note priority="1" from="description">From placeholder</note>
</trans-unit>

在您的 messages.xlf 文件中。如果没有翻译,我无法让它工作。所以你需要添加 state=translated 和一个值。

即使我们不在 i18n-placeholder 中赋值也可以。 只是喜欢:

<input type="number" placeholder="From" i18n-placeholder/>

它将正常工作。

要添加到@evandongen 的答案,这里是 Angular 文档中记录的位置:

已更新link:

https://angular.io/guide/i18n#translate-attributes

Add i18n translation attributes


To mark an attribute for translation, add an attribute in the form of i18n-x, where x is the name of the attribute to translate. The following example shows how to mark the title attribute for translation by adding the i18n-title attribute on the img tag:

<img [src]="logo" i18n-title title="Angular logo" />

This technique works for any attribute of any element.

You also can assign a meaning, description, and id with the i18n-x="<meaning>|<description>@@<id>" syntax.

我使用了这个属性

 <input type="submit" value="{{ 'LOGIN.LOG' | translate }}">