Angular Boolean Pipe是/否本地化
我需要一种方便的方式来使我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在Angular的内置国际化中使用“选择”功能来翻译布尔值:
步骤1-遵循角文档中概述的步骤,在您的应用程序中设置国际化。这里有一个很好的教学视频: https://angular.io/guide/i18n-overview
步骤2-准备组件进行翻译。
只需说您的组件看起来像这样:
您可以通过添加I18N标签和选择功能来准备翻译以进行翻译:
步骤3-通过运行 ng提取-I18N -OUTPUT -PATH src/locale
步骤4提取源文本,以进行翻译 ng extract -i18n - 如果您在步骤1中遵循视频,则应该有一个消息。xlf文件借助您的源文本,并使用目标语言文本的副本。将从源XLF的transunit复制到目标XLF,并在trans单元中的源文本下方添加以下目标行:
这些显示了西班牙语翻译的真/错误。您可以用目标语言添加是/否。
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:
You can prepare it for translation by adding the i18n tag and the select feature:
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:
These show spanish translation for true/false. You could add yes/no instead in your target language.
不幸的是,与
Date
不同,可以轻松地使用JS本地化,必须翻译一个简单的“是”或“否”字符串才能实现这一目标。以下是我要实现这一目标的一些步骤:这有点痛苦,但是此设置将无限地扩展到需要本地化/全球化的任何未来工作。
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:It's a bit of a pain but this setup will scale infinitely for any future work that require localization/globalization.