如何格式化一般一个布尔到是/否字符串? [英] How to generically format a boolean to a Yes/No string?
问题描述
我想显示是/否不同语言根据一些布尔变量。结果
是有根据传递给它的区域它来格式化一个通用的方法?结果
如果没有,什么是格式化除了 boolVar一个布尔值的标准方法? Resources.Yes:Resources.No
结果
我猜 boolVar.ToString(的IFormatProvider)
涉及<。 BR>
是我的假设是正确的?
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?
推荐答案
框架本身并没有为你提供这个(据我知道)。翻译真/假
到是/否
不罢工我比其他潜在的翻译(如<较为常见code>开/关,选中/取消
,只读/读写$ C $ 。C>或其他)
The framework itself does not provide this for you (as far as I know). Translating true/false
into yes/no
does not strike me as more common than other potential translations (such as on/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:
public static class BooleanExtensions
{
public static string ToYesNoString(this bool value)
{
return value ? Resources.Yes : Resources.No;
}
}
用法:
Usage:
bool someValue = GetSomeValue();
Console.WriteLine(someValue.ToYesNoString());
这篇关于如何格式化一般一个布尔到是/否字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!