Reputation: 2830
I need to format boolean as string for multilanguage support "Ja"/"Nein". What is the right format string that I need for DisplayFormat and EditFormat? I am using DevExpress with repositoryItemTextEdit as column editor in design but I think it's the same with any other binding string format. Is there another approach?
Upvotes: 1
Views: 1382
Reputation:
The easyest way is to use a different property or column for the formated value. You can also use the Parse/Format events of the data binding:
repositoryItemTextEdit1.DataBindings[0].Format += new ConvertEventHandler(repositoryItemTextEdit1_Format);
repositoryItemTextEdit1.DataBindings[0].Parse += new ConvertEventHandler(repositoryItemTextEdit1_Parse);
void repositoryItemTextEdit1_Format(object sender, ConvertEventArgs e)
{
return e.Value ? "Ja" : "Nein";
}
void repositoryItemTextEdit1_Parse(object sender, ConvertEventArgs e)
{
return e.Value.Equals("Ja") ? yes : no;
}
Upvotes: 1
Reputation: 23626
You should definitely extract literal values "Ja"/"Nein" into localization resources. Laoujin makes a great response, but I will expand it a little bit with examples.
First, define custom format provider, that will use localization in some sort
public class LocalizedBoolFormatter : IFormatProvider, ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
bool value = (bool)arg;
format = (format == null ? null : format.Trim().ToLower());
switch (format)
{
case "yn":
return GetLocalizedBool(value);
default:
return HandleDefaultFormat(arg, format, formatProvider);
}
}
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
}
Private methods for LocalizedBoolFormatter
could look like:
private string HandleDefaultFormat(object value, string format, IFormatProvider formatProvider)
{
if (value is IFormattable)
return ((IFormattable)value).ToString(format, formatProvider);
else
return value.ToString();
}
private string GetLocalizedBool(bool value)
{
//extract from localization resources
//or use CultureInfo.CurrentCulture for poors man localization
return value ? "Ja" : "Nein";
}
Then you can simply format value using custom formater, which would be localized by a formatter
bool f = false;
string formatted = string.Format(new LocalizedBoolFormatter(), "{0:yn}", f);
Console.WriteLine (formatted);
With DevExpress RepositoryItemTextEdit you can use the Custom Formatting as follows:
repositoryItemTextEdit.DisplayFormat.Format = new LocalizedBoolFormatter();
repositoryItemTextEdit.DisplayFormat.FormatType = FormatType.Custom;
Upvotes: 4
Reputation: 10229
Boolean value cannot be translated to the current locale automatically. You could use an extension method to translate them:
public static string ToPrettyString(this bool value) {
return value ? YourResource.TrueValue : YourResource.FalseValue;
}
If you need more flexibility, check the answer Boolean Format String - Yes/No instead of True/False where there is also an example of implementing IFormatProvider
.
Upvotes: 3