简体   繁体   English

如何将布尔值转换为本地化字符串

[英]How to convert boolean to localized string

There is any way to convert a boolean value to a localized string. 有任何方法可以将布尔值转换为本地化字符串。 I have tried: 我努力了:

var x = true;

var culture = new CultureInfo("en-US")
x.ToString(culture) // returns True

culture = new CultureInfo("pt-BR")
x.ToString(culture) // returns True, expected Verdadeiro

Or should I start typing the switch now to end before 2020? 或者我应该在2020年之前开始输入开关吗?

Well, start typing because it's documented behaviour :) 好吧,开始打字,因为它记录了行为:)

Boolean.ToString(IFormatProvider) Boolean.ToString(的IFormatProvider)

Remarks 备注

The provider parameter is reserved. provider参数是保留的。 It does not participate in the execution of this method. 它不参与此方法的执行。 This means that the Boolean.ToString(IFormatProvider) method, unlike most methods with a provider parameter, does not reflect culture-specific settings. 这意味着与大多数带有provider参数的方法不同,Boolean.ToString(IFormatProvider)方法不反映特定于文化的设置。

As @Michal pointed out, this is the documented behavior. 正如@Michal指出的那样,这是记录在案的行为。

If your system supports many languages, you must have some sort of i18 support. 如果您的系统支持多种语言,则必须具有某种i18支持。 Use that to convert a boolean value to string. 使用它将布尔值转换为字符串。 You can add an extension method like so: 您可以像这样添加扩展方法:

public string ToLocalizedString(this bool b)
{
    return ...i18n version of true or false...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM