When to use CultureInfo
One thing that developers constantly do, is forget to specify culture (CultureInfo) within their applications.
This is one of the more important classes in the framework that is often overlooked.
There are a few different types of culture to set :
- InstalledUICulture
- CurrentUICulture
- CurrentCulture
- InvariantCulture
InstalledUICulture
InstalledUICulture represents the culture installed with the operating system. For Window products that offer a Multilingual User Interface, it will return English (en-US). This is not commonly used.
CurrentUICulture
CurrentUICulture is what should be used to display correct text, colors and images. For example, in an ASP.NET web application where you want to serve localized content; this would be the way to go.
CurrentCulture
CurrentCulture is an important one that should be always be used for formatting before displaying them to a user. For example, if the user’s Culture is “(fr-FR”) the days would be returned as “dimanche”, “lundi”, “mardi”, etc…
var days = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
Keep in mind that this property is read from the executing thread and can also be overridden. Another reason to use this is for parsing Locale specific input. This allows users to input data in their regional format.
InvariantCulture
InvariantCulture is on the opposite side of CurrentCulture. When handling internal data in the application or transferring across network, or storing data; this is the way to go. It should always be used. It specifically allows you to parse input in a reliable manner, no matter what the current culture happens to be. This is not used to parse user input.
Keep these properties in mind when working on your next project. If you ever happen to distribute your software to another country, these could save you from tracking down bugs that you can’t reproduce locally.