myermian
myermian

Reputation: 32515

FxCop CA1305: CurrentCulture vs. CurrentUICulture

So, I have a few resource files for localization. They're strings that can be formatted.

For example:

MyResource.resx
 Title: "MyApp - Ver: {0}"

I then return it by doing like so:

public String Title
{
    get { return String.Format(CultureInfo.CurrentUICulture, MyResources.Title, 1); }
}

I understand the difference between CurrentUICulture and CurrentCulture, but FxCop is telling me to use CurrentCulture instead?

Upvotes: 3

Views: 2011

Answers (3)

Paweł Dyda
Paweł Dyda

Reputation: 18662

To some extent FxCop is right: you should not use CurrentUICulture in this case. As others already said, CurrentCulture is meant for Locale-aware formatting, whereas CurrentUICulture is meant for reading translatable strings from resources.
What you did here, was formatting number, therefore FxCop complains that you used incorrect CultureInfo. Unfortunately, what FxCop did not tell you is, you should in fact use CultureInfo.InvariantCulture. Why? Because version number is something that does not depend on Locale. You will always see something like 1.9 and not something like 1,9. Thus InvariantCulture is the way to go.
Microsoft even provided specific class to store version information - oddly enough its name is Version (AFAIR it is in System namespace). This will always present you version numbers like I mentioned before, when you do ToString(). Its constructor also expects Locale-invariant version string when you instantiate it.

Upvotes: 7

NoviceProgrammer
NoviceProgrammer

Reputation: 3365

Another thing is that CurrentUICulture can be a neutral culture while CurrentCulture is always a specific culture.

The XXXFormatInfo types do not work with neutral cultures and will raise a NotSupportedException exception.

Upvotes: 4

dlev
dlev

Reputation: 48596

String.Format is often going to be used for numeric or date formatting, which is based on CurrentCulture rather than CurrentUICulture.

Upvotes: 5

Related Questions