Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 613
Marcelo de Aguiar Avatar asked Dec 16 '13 20:12

Marcelo de Aguiar


2 Answers

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.

like image 168
Michal Levý Avatar answered Nov 16 '22 21:11

Michal Levý


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...
}
like image 1
zmbq Avatar answered Nov 16 '22 23:11

zmbq