Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle localization / CultureInfo

Some methods like string.Format() or .Parse() require an IFormatProvider. How do you provide it?

  • In closed environment application (where you know that localization will never be required), do you just skip it and call the methods without the IFormatProvider?

  • In applications that might get localized, do you think about the correct value for every method call and just set it right there? That would be probably 'CultureInfo.CurrentCulture' or 'CultureInfo.CurrentUiCulture'.

  • Or do you use global variables like 'MyUiCultureInfo' and 'MyCultureInfo' to be able to switch the localization by changing their values? How and where do you store these variables?

  • Is there anything to consider when I develop libraries or frameworks - how to handle localization in this case?

like image 492
tanascius Avatar asked May 28 '09 10:05

tanascius


3 Answers

I'm always setting CurrentThread.Current(Ui)Culture to the correct value in our (ASP.NET) applications. This is usually done at the begin of each request, based on the user's preferences or a default value stored in a config file (if the user has not defined a preference).

After setting these properties of the current thread, you can stop thinking about it - numbers, dates, etc. will be correctly formatted/parsed, even when no IFormatProvider is provided to these methods. Otherwise, you have to ensure that the correct IFormatProvider is passed everywhere.

I the case of a library, I think it should just rely on the application for these things and should not have to worry about these things.

like image 104
M4N Avatar answered Oct 06 '22 08:10

M4N


I set the current culture to US english because in my country few 'intelligent' persons decided that decimal separator is comma. Sometimes decimal number is with ',' and sometimes with '.'. Not all computers have regional settings configured correctly and you shouldn't rely on that.

To set that application-wide settings:

Application.CurrentCulture = new CultureInfo("en-US");
Application.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
like image 30
Pavels Avatar answered Oct 06 '22 09:10

Pavels


As you say you can in a closed environment call the methods without any IFormatProvider supplied.

As you also write you can supply a CultureInfo object like this:

        Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0}", DateTime.Now));
        Console.WriteLine(String.Format(new CultureInfo("en-US"), "{0}", DateTime.Now));

This would for me display the date in two different ways since my CurrentCulture is swedish like this:

2009-05-28 13:12:43

5/28/2009 1:12:43 PM

CultureInfo.CurrentCulture handles the formating of dates etc and is supplied from the setting on you current machine.

CurrentCulture.CurrentUiCulture has to do with localization that is translation. Meaning what shows on menus etc in windows.

My guess is that default behaviour of the methods is to use CurrentCulture if none is supplied.

like image 35
Niklas Avatar answered Oct 06 '22 09:10

Niklas