Angular i18n - 插值

问题描述 投票:0回答:2

我正在关注 Angular i18n 指南:https://angular.io/guide/i18n

我知道了如何正确插入“字符串+变量”。

<trans-unit id="interpolated-persons" datatype="html">
        <source>Persons: <x id="INTERPOLATION" equiv-text="{{number-of-people}}"/></source>
        <target>Personas: <x id="INTERPOLATION" equiv-text="{{number-of-people}}"/></target>
</trans-unit>

<span i18n="@@interpolated-persons">Persons: {{persons}}</span>

但是,我不知道如何插入“String + Plural”。

<trans-unit id="interpolated-time" datatype="html">
        <source>Time: <x id="ICU" equiv-text="{tempo, plural, other {...}}"/></source>
        <target>Tiempo: {tempo, plural, =60 {una hora} =1440 {un día} =10080 {una semana} other {mucho tiempo}}</target>
</trans-unit>

<span i18n="@@interpolated-time">Time: {minutes, plural, other {{{minutes}} elapsed}}</span><br>

我尝试了几件事。我让它工作的唯一方法是通过 硬编码值或直接在 中改变分钟变量来改变节奏。但是,如果我这样做,我将无法在另一个页面中重复使用相同的翻译。

是否可以插入 字符串 + 复数

angular6 angular-i18n
2个回答
4
投票

Angular 将“字符串 + 复数”表达式拆分为 2 个更简单的表达式。

复数内部表达式获取一个随机标识符,它应该与 messages.xlf 文件中的跨单元“id”匹配才能工作。

https://angular.io/guide/i18n中的确切翻译嵌套表达式示例应该是这样的:

<trans-unit id="interpolated-time" datatype="html"> <source>Updated: <x id="ICU" equiv-text="{minutes, plural, =0 {...} =1 {...} other {...}}"/></source> <target>Actualizado: <x id="ICU" equiv-text="{minutes, plural, =0 {...} =1 {...} other {...}}"/></target> </trans-unit> <trans-unit id="7151c2e67748b726f0864fc443861d45df21d706" datatype="html"> <source>{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago} other {<x id="INTERPOLATION" equiv-text="{{minutes}}"/> minutes ago by {VAR_SELECT, select, male {male} female {female} other {other} }} }</source> <target>{VAR_PLURAL, plural, =0 {justo ahora} =1 {hace 1 minuto} other {hace <x id="INTERPOLATION" equiv-text="{{minutes}}"/> minutos por {VAR_SELECT, select, male {un hombre} female {una dama} other {otro} }} }</target> </trans-unit>

id

7151c2e67748b726f0864fc443861d45df21d706 应从编译输出中获取:

Missing translation for message "7151c2e67748b726f0864fc443861d45df21d706"
    

0
投票
试试这个:只要说你有一个变量“secondsRemaining”并且你想包含在这样的字符串中:

<span>There are 10 seconds left</span> <!-- secondsRemaining = 10 --> <span>There is 1 second left</span> <!-- secondsRemaining = 1 --> <span>Out of time</span> <!-- secondsRemaining = 0 -->

您可以在 html 文件中执行类似的操作:

<span i18n>{secondsRemaining, plural, =0 {Out of time} =1 {There is 1 second left} other {There are {{secondsRemaining}} left}}</span >

然后,提取后,你可以让你的trans-unit看起来像这样:

<trans-unit id="6794418477110295816" datatype="html"> <source>{VAR_PLURAL, plural, =0 {Out of time} =1 {There is 1 second left} other {There are <x id="INTERPOLATION" /> seconds left}} </source> <target>{VAR_PLURAL, plural, =0 {Sin tiempo} =1 {Queda un segundo} other {Queda <x id="INTERPOLATION" /> segundos}} </target> <context-group purpose="location"> <context context-type="sourcefile">src/app/your-folder-path/your-file.component.html</context> <context context-type="linenumber">100</context> </context-group> </trans-unit>

当然,这是使用自动生成的ID。请随意将您自己的 id 添加到范围中。

© www.soinside.com 2019 - 2024. All rights reserved.