Angular Boolean Pipe是/否本地化

发布于 2025-02-06 14:54:11 字数 578 浏览 2 评论 0原文

我需要一种方便的方式来使我的Yes-no管道本地化,例如日期管道 {{my.date |日期:'short':'utc':'de-de-de'}}

类似

    {{ my.boolean | yesNo : "de-DE" }}

,而不是返回value? “是”:“否”;我需要一些魔术:D

    @Pipe({
      name: 'yesNo'
    })
    export class YesNoPipe implements PipeTransform {
    
      transform(value: boolean, ...args: unknown[]): string {
        // need some magic
        return value ? localizedYes : localizedNo;
      }
    
    }

i need a handy way to get my yes-no pipe localized like the date pipe
{{ my.date | date: 'short' : 'UTC' : 'de-DE' }}

something like

    {{ my.boolean | yesNo : "de-DE" }}

and instead of returning value ? "Yes" : "No"; I need some magic :D

    @Pipe({
      name: 'yesNo'
    })
    export class YesNoPipe implements PipeTransform {
    
      transform(value: boolean, ...args: unknown[]): string {
        // need some magic
        return value ? localizedYes : localizedNo;
      }
    
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹎☆浅夏丿初晴 2025-02-13 14:54:11

我建议在Angular的内置国际化中使用“选择”功能来翻译布尔值:

步骤1-遵循角文档中概述的步骤,在您的应用程序中设置国际化。这里有一个很好的教学视频: https://angular.io/guide/i18n-overview

步骤2-准备组件进行翻译。

只需说您的组件看起来像这样:

<span>{{myBooleanValue}}</span>

您可以通过添加I18N标签和选择功能来准备翻译以进行翻译:

<span i18n>{ myBooleanValue, select, true {true} false {false}}</span>

步骤3-通过运行 ng提取-I18N -OUTPUT -PATH src/locale

步骤4提取源文本,以进行翻译 ng extract -i18n - 如果您在步骤1中遵循视频,则应该有一个消息。xlf文件借助您的源文本,并使用目标语言文本的副本。将从源XLF的transunit复制到目标XLF,并在trans单元中的源文本下方添加以下目标行:

<source>{VAR_SELECT, select, true {true} false {false}}</source>
<target>{VAR_SELECT, select, true {verdadero} false {falso}}</target>

这些显示了西班牙语翻译的真/错误。您可以用目标语言添加是/否。

I'd recommend using the 'select' feature in Angular's built in internationalization for translating Booleans:

Step 1 - set up internationalization in your app following the steps outlined in the Angular documentation. There's a good instructional video here: https://angular.io/guide/i18n-overview

Step 2 - Prepare your component for translation.

Just say you have a component that looks like this:

<span>{{myBooleanValue}}</span>

You can prepare it for translation by adding the i18n tag and the select feature:

<span i18n>{ myBooleanValue, select, true {true} false {false}}</span>

Step 3 - extract your source text for translation by running ng extract-i18n --output-path src/locale

Step 4 - If you followed the video in Step 1, you should have an messages.xlf file with your source text, and a copy of it with your target language text. Copy the transunit from your source xlf to your target xlf and add the following target line below the source text in your trans unit:

<source>{VAR_SELECT, select, true {true} false {false}}</source>
<target>{VAR_SELECT, select, true {verdadero} false {falso}}</target>

These show spanish translation for true/false. You could add yes/no instead in your target language.

微暖i 2025-02-13 14:54:11

不幸的是,与Date不同,可以轻松地使用JS本地化,必须翻译一个简单的“是”或“否”字符串才能实现这一目标。以下是我要实现这一目标的一些步骤:

  1. issal ngx-translate https ://www.npmjs.com/package/@ngx-translate/core 并遵循其设置指南,并为您想要翻译的所有语言创建翻译资源文件。如果您不喜欢他们的文档,这是一篇很棒的文章,易于遵循指南 https://www.codeandweb.com/babeledit/tutorials/how-to-to-to-to-to-to-treanslate-your-angular-angular-angular-angular-angular-angular-angular-ang--with-ngx-ngx-translate
  2. 只是将翻译服务与您的管道一起使用,
    @Pipe({
      name: 'yesNo'
    })
    export class YesNoPipe implements PipeTransform {
    
      transform(value: boolean, ...args: unknown[]): string {
        return value ? 'KEY.YES' : 'KEY.NO';
      }
    
    }
    ...
    {{ my.boolean | yesNo | translate }}

这有点痛苦,但是此设置将无限地扩展到需要本地化/全球化的任何未来工作。

Unfortunately, unlike Date which can be easily localize using JS, a simple "Yes" or "No" string have to be translated in order to achieve this. Here are some of the steps that I would go to achieve this:

  1. Instal ngx-translate https://www.npmjs.com/package/@ngx-translate/core and follow their guide for setup as well as create the translation resource files for all the languages that you want to be translated. If you don't like their documentation, here's a great article with an easy to follow guide https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular-app-with-ngx-translate
  2. The rest is just using the translate service together with your pipe
    @Pipe({
      name: 'yesNo'
    })
    export class YesNoPipe implements PipeTransform {
    
      transform(value: boolean, ...args: unknown[]): string {
        return value ? 'KEY.YES' : 'KEY.NO';
      }
    
    }
    ...
    {{ my.boolean | yesNo | translate }}

It's a bit of a pain but this setup will scale infinitely for any future work that require localization/globalization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文