CHAPTER 1
Every spoken language has its own rules of formatting when it comes to dates, currency, and numbers. Rules vary from how a date is printed to what the thousand separator is for numbers. These rules can also vary for the same language, but from country to country.
Localization is about formatting all information that we present in a way that is familiar to the user. Thankfully for us, most of these rules have already been captured and implemented in .NET and the JavaScript plugin Globalize.
All code examples that do not include a method must be placed in one, for instance in a new Console project. You must also add a using to System.Globalization.
To be able to control how to format information we must have some ways to represent the language and country that we are targeting. In most programming languages, this identification is made through a string called culture code.
The culture code itself consists of two parts delimited by an underscore or a colon. The actual delimiter varies between programming languages. A string can be “sv-FI” representing Swedish language as spoken in Finland or “en-US” for English as spoken in the US.
The first part of the code is one of the codes defined in ISO639, and the country code is defined in ISO3166. The actual combination is defined in RFC4646, which is a 59-page long document.
The neutral culture is used to format strings in a generic way for a language, meaning it doesn’t adopt the formatting for a specific country, but for the language in general.
Neutral cultures are identified by strings where the country is omitted. For example, “en” instead of “en-US”.
I prefer to see the invariant culture as the coding culture. It formats strings and numbers just as we write them in our code. For instance, with no digit grouping and a dot as the decimal separator.
Hence it’s also a great format to use if you have to store information as strings, as the culture will never change.
Most of the language/culture handling in .NET is controlled by a single class called CultureInfo. It defines the language to use and how things like numbers and dates should be formatted. All classes used for localization are provided in that namespace.
.NET contains a set of built-in combinations that you can use without any additional configuration. These are defined in the following MSDN article. The linked article does not include Windows 8, but all codes work in Windows 8 too. You can also generate your own cultures using the CultureAndRegionInfoBuilder.
Two different cultures exist in .NET. The first one is called UICulture and controls which language the user interface should be presented in. The other one is just called Culture, which controls formatting of strings and numbers. They are both set through the current thread using the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture classes.
This culture is by default set to reflect the installed OS language, i.e., the language that all strings are in (for instance in the Start menu in Windows as shown in the following figure).

There is another culture setting which represents formats used for dates, strings, currency etc. Unless you have specified a value, by default it’s the same setting in Windows, as shown in the following figure.

You can assign a new culture like this:
static void Main(string[] args) { Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE"); Thread.CurrentThread.CurrentCulture = new CultureInfo(1053); //Swedish locale name |
There are two shortcuts that can be used to access the culture settings. They provide read-only access to the culture settings.
static void Main(string[] args) { Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE"); Console.WriteLine(CultureInfo.CurrentCulture.Name); // Prints "sv-SE" |
You can also access the culture that the operating system was installed with. The property is named CultureInfo.InstalledUICulture.
To format using the invariant culture simply pass it to the ToString() method:
static void Main(string[] args) { var number = 22.30; number.ToString(CultureInfo.InvariantCulture); Console.WriteLine(number); // Prints "22.30" Console.ReadLine(); } |
.NET also contains a class called RegionInfo that provides information about a country. For instance, you can use it to get the current currency symbol or to determine if the metric system is used:
static void Main(string[] args) { var info = new RegionInfo("sv-SE"); Console.WriteLine("Uses the metric system: {0}", info.IsMetric); Console.WriteLine("I wish I had 100 {0}", info.CurrencySymbol); |

JavaScript has support for basic formatting and parsing. But it is just that: basic. For example, you cannot control if a short or long format should be used or how many decimals to include.
I’m therefore going to use the Globalize plugin in all code examples where vanilla JavaScript is not enough.
You can use the client side to specify which culture to use. It’s typically done with the help of the “Accept-Language” header. It contains the languages that the user understands, in the order in which order he/she prefers them. The following figure shows an example of the header.

The setting itself is specified through your web browser settings. Since Internet Explorer is configured using the regional settings in Windows, I’ve included an sample for Google Chrome. You can find the setting for Google Chrome under “Advanced” as demonstrated by the following figure:

To access that setting you can use window.navigator.language in JavaScript. That property does not exist in IE, however. Instead you have to combine it with the IE property like this:
var language = window.navigator.userLanguage || window.navigator.language; |
Do note that the IE property does not return the browser language, but the specified language in IE or the Windows Control Panel (it depends on the IE version).
JavaScript has a couple of functions that you can use to localize strings, such as toLocaleString and toLocaleDateString. However, the support is different depending on the browser. You can only expect the browser to format using the culture that the browser was installed with (or at worst using the OS culture).
Note that some of the methods do allow you to specify the culture as an argument, and the support will be probably improved over time.
Hence I’m going to show you JS examples that use those methods, but also using the alternative below.
Globalize is a JavaScript plugin that has excellent support for different cultures and various formatting formats.
It’s an open source library maintained by the jQuery foundation. The library itself is standalone, however, and not dependent of jQuery.
To use Globalize, we have to include the script itself in the HTML header:
<script src="http://ajax.aspnetcdn.com/ajax/globalize/0.1.1 /globalize.min.js"></script> |
However, that script only contains all the handling. We also have to include the cultures that define all rules for each specific country/language. We can either include only the cultures that we need, or use the script that includes all cultures:
<script src="http://ajax.aspnetcdn.com/ajax/globalize/0.1.1/cultures /globalize.cultures.js"></script> |
To switch culture using Globalize we can either change by globally setting the culture:
Globalize.culture("se-SE"); |
But we can also specify the culture in every method that we invoke:
<script type="text/javascript"> // Says that we want to parse the string as Swedish var result = Globalize.parseFloat("1234,56", 10, "sv-SE");
// output using the default culture (i.e. en-US) document.writeln(Globalize.format(result, "n2")); </script> |
The result is shown in the following figure.

Globalize is available at github.
The goal of this chapter is to introduce you to the settings that control how the language and formatting is made in .NET.
The effects of these settings will be discussed throughout this book.
You can read more about CultureInfo in MSDN.