如何将布尔值一般格式化为是/否字符串?
我想根据一些布尔变量以不同的语言显示是/否。
是否有一种通用方法可以根据传递给它的区域设置对其进行格式化?
如果没有,除了 boolVar 之外,格式化布尔值的标准方法是什么?资源.是 : 资源.否
.
我猜测涉及到 boolVar.ToString(IFormatProvider)
。
我的假设正确吗?
I would like to display Yes/No in different languages according to some boolean variable.
Is there a generic way to format it according to the locale passed to it?
If there isn't, what is the standard way to format a boolean besides boolVar ? Resources.Yes : Resources.No
.
I'm guessing that boolVar.ToString(IFormatProvider)
is involved.
Is my assumption correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
框架本身并没有为你提供这个(据我所知)。在我看来,将
true/false
翻译成yes/no
并不比其他潜在翻译(例如on/off
、选中/未选中
、只读/读写
或其他)。我想封装行为的最简单方法是创建一个扩展方法来包装您在问题中建议的构造:
用法:
The framework itself does not provide this for you (as far as I know). Translating
true/false
intoyes/no
does not strike me as more common than other potential translations (such ason/off
,checked/unchecked
,read-only/read-write
or whatever).I imagine that the easiest way to encapsulate the behavior is to make an extension method that wraps the construct that you suggest yourself in your question:
Usage:
正如其他答案所示,该框架不允许布尔值具有自定义格式化程序。但是,它确实允许数字具有自定义格式。布尔值的 GetHashCode 方法将返回 1(表示 true)和 0(表示 false)。
根据 MSDN 自定义数字格式字符串,当“;”有 3 个部分时指定的格式将应用于“正数;负数;零”。
可以对 bool 值调用 GetHashCode 方法以返回数字,以便您可以使用自定义数字格式字符串返回是/否或开/关或情况需要的任何其他单词集。
这是返回 on/OFF 的示例:
返回:
As the other answers indicate, the framework does not allow boolean values to have custom formatters. However, it does allow for numbers to have custom formats. The GetHashCode method on the boolean will return 1 for true and 0 for false.
According to MSDN Custom Numeric Format Strings, when there are 3 sections of ";" the specified format will be applied to "positive numbers;negative numbers;zero".
The GetHashCode method can be called on the bool value to return a number so you can use the Custom Numeric Format String to return Yes/No or On/Off or any other set of words the situation calls for.
Here is a sample that returns on/OFF:
returns:
不幸的是, Boolean.ToString(IFormatProvider) 在这里没有帮助:
无论如何,布尔值代表True和False,而不是Yes和No。如果你想映射True ->是与错 ->不,您必须自己完成此操作(包括本地化);框架中没有对此的内置支持。您提出的解决方案(Resources.Yes/No)对我来说看起来不错。
Unfortunately, Boolean.ToString(IFormatProvider) does not help here:
In any case, Booleans represent True and False, not Yes and No. If you want to map True -> Yes and False -> No, you will have to do that (including localization) yourself; there's no built-in support in the framework for that. Your propopsed solution (Resources.Yes/No) looks fine to me.
在我的剃刀页面中正在工作:
@(item.Active.GetValueOrDefault() ? "Yes" : "No")
这里很重要,如果属性是必需的(可以为空),如果需要,它应该很好用:
我发现这个在其他帖子上,只是重播,希望对某些人有所帮助1。
in my razor page is working this:
@(item.Active.GetValueOrDefault() ? "Yes" : "No")
here is important if attribute is required (nullable) or not, if is required it should be good with:
I found this here on some other post, just replaying, hope it helps some1.