Question

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?

Était-ce utile?

La solution

Well, start typing because it's documented behaviour :)

Boolean.ToString(IFormatProvider)

Remarks

The provider parameter is reserved. 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.

Autres conseils

As @Michal pointed out, this is the documented behavior.

If your system supports many languages, you must have some sort of i18 support. 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...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top