5

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?

tanascius
  • 53,078
  • 22
  • 114
  • 136

4 Answers4

6

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.

M4N
  • 94,805
  • 45
  • 217
  • 260
1

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 = ".";
Pavels
  • 1,256
  • 1
  • 12
  • 19
  • 1
    your country sounds like my country :) – tanascius May 28 '09 at 11:27
  • 4
    Funny that you would complaint about the decimal separator and then use a culture that writes the day in the middle of the date. ;) But I feel your pain! – ANeves Jun 28 '11 at 13:33
1

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.

Niklas
  • 11
  • 1
0

I always place the Culture property at the start of the ASPX or Master page and it does all the conversions for me. I never need to do any hardcoding or Converting of date/time to my own required culture...

.
<%@ Page Language="C#" ..etc... Culture="en-AU" %>
.

Simplez

Fandango68
  • 4,461
  • 4
  • 39
  • 74