0

As documented in the Boolean.ToString(IFormatProvider) method doc, the IFormatProvider provider does not impact the constant "True/False" output.

Now, is there a way to however translate the "True" to "Verdadero"?

public static void Main(string[] args)
{
    //Your code goes here
    Console.WriteLine("Hello, world!");
    System.Globalization.CultureInfo ci = new CultureInfo("es-ES");
    Console.WriteLine(true.ToString(ci));
}
// Hello, world!
// True
serge
  • 13,940
  • 35
  • 121
  • 205
  • As the first two answers below suggest - you can either use a conditional statement or an extension method - but rather than use strings as in the answers use localizable string resources. See this answer : http://stackoverflow.com/questions/1142802/how-to-use-localization-in-c-sharp – PaulF Mar 22 '17 at 17:17
  • @Ðаn: without further input from OP, then it is difficult to say - but what would you suggest as an improvement over the answers so far. – PaulF Mar 22 '17 at 17:25
  • 1
    I see you have posted my comment as an answer including the same link. – PaulF Mar 22 '17 at 17:30

3 Answers3

2

There isn't a library-based solution to your question, nor should there be. The reason is that a string representation of System.Boolean is unlikely to be useful for anything but the most trivial of localization. Note that is not the case for floating-point numbers where a culture-specific . or , can be applied when formatting. Dates (System.DateTime) have some localization support from the operating system itself, so .NET is able to build on that; this is not the case for System.Boolean.

Usually, there will be other words in addition to just "True" (or "False"); those words will have to be translated too. And, depending on the language and those other words, you might not be able to do simple string concatenation: string message = baseMessage + b.ToString();

Instead, you should store your strings in resource files and retrieve the right one.

bool b = ...;
string message = b ? Properties.Resources.TrueMessage : Properties.Resources.FalseMessage;

See How to use localization in C# for more details.

Community
  • 1
  • 1
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • I am not the only one asking about that, so I don't know about "unlikely". If is not useful, why does Microsoft returned "True" as bool.ToString() instead of "1"/"0"? Why "True" and not "Vrai", "Varadero" or "OK"... More frustrating it has a IFormatProvider as parameter... so Anglophone users are favored over others: `"C'est vrai ?:" + true.ToString(new CultureInfo("fr-FR"))` – serge Mar 23 '17 at 09:14
1

As per the docs, Boolean.ToString(IFormatProvider) will not reflect culture specific strings.

However, one workaround could be to create an extension method on the Boolean object:

public static class BoolExtensions
{
    public static string ToSpanishString(this bool val)
    {
        return val ? "Verdadero" : "Falso";
    }
}
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
  • 1
    I don't know if a global library based solution exists or not. However you can pass an optional CultureInfo and start an if ... else if .... else block to handle the culture you are interested to where in case of missing CultureInfo you return the default TrueString or FalseString from the Boolean class.. – Steve Mar 22 '17 at 17:16
0

You can achieve this in the following way:

bool test = true;
Console.WriteLine(test ? "Verdadero" : "Equivocado");

The first value is always the truthy one, the second is the falsy.

NotTelling
  • 462
  • 3
  • 11