Internationalizing an application

Internationalization is the process of enabling your application to run correctly all over the world. It has two parts: globalization and localization.

Globalization is about writing your code to accommodate multiple languages and regions. The combination of a language and a region is known as a culture. It is important for your code to know both the language and region because the date and currency formats are different in Quebec and Paris, despite them both using French.

There are International Standards Organization (ISO) codes for all culture combinations. For example, in the code da-DK, da indicates the Danish language and DK indicates the country of Denmark.

Localization is about customizing the user interface to support a language. Since localization is just about the language, it doesn't need to know about the region.

Internationalization is a huge topic on which entire books have been written. In this section, you will get a brief introduction to the basics using the CultureInfo type in the System.Globalization namespace.

Note

.NET Core does not currently allow threads to get or set their CurrentCulture or CurrentUICulture properties. An alternative for getting these two properties (but not setting) is to use the CultureInfo class' static properties, but you cannot set them.

Globalizing an application

Add a new console application project named Ch04_Internationalization. At the top of the file, import the following types and namespaces:

    using static System.Console; 
    using System; 
    using System.Globalization; 

In the Main method, enter the following statements:

    CultureInfo globalization = CultureInfo.CurrentCulture; 
    CultureInfo localization = CultureInfo.CurrentUICulture; 
    WriteLine($"The current globalization culture is
    {globalization.Name}: {globalization.DisplayName}"); 
    WriteLine($"The current localization culture is
    {localization.Name}: {localization.DisplayName}"); 
    WriteLine(); 
    WriteLine("en-US: English (United States)"); 
    WriteLine("da-DK: Danish (Denmark)"); 
    WriteLine("fr-CA: French (Canada)"); 
    Write("Enter an ISO culture code: "); 
    string newculture = ReadLine(); 
    if (!string.IsNullOrEmpty(newculture)) 
    { 
      var ci = new CultureInfo(newculture); 
      CultureInfo.CurrentCulture = ci; 
      CultureInfo.CurrentUICulture = ci; 
    } 
    Write("Enter your name: "); 
    string name = ReadLine(); 
    Write("Enter your date of birth: "); 
    string dob = ReadLine(); 
    Write("Enter your salary: "); 
    string salary = ReadLine(); 
    DateTime date = DateTime.Parse(dob); 
    int minutes = (int)DateTime.Today.Subtract(date).TotalMinutes; 
    decimal earns = decimal.Parse(salary); 
    WriteLine($"{name} was born on a {date:dddd} and is {minutes:N0}
    minutes old and earns {earns:C}."); 

When you run an application, it automatically sets its thread to use the culture of the operating system. I am running my code in London, UK, so the thread is already set to English (United Kingdom).

The code prompts the user to enter an alternative ISO code. This allows your applications to replace the default culture at runtime.

The application then uses standard format codes to output the day of the week, dddd; the number of minutes with thousand separators, N0; and the salary with the currency symbol, C. These adapt automatically, based on the thread's culture.

Run the console application and view the output. Enter en-GB for the ISO code and then enter some sample data. You will need to enter a date in a format valid for British English:

Enter an ISO culture code: en-GB
Enter your name: Alice
Enter your date of birth: 30/3/1967
Enter your salary: 23500
Alice was born on a Thursday, is 25,469,280 minutes old and earns
£23,500.00.

Rerun the application and try a different culture, such as Danish in Denmark (da-DK):

Enter an ISO culture code: da-DK
Enter your name: Mikkel
Enter your date of birth: 12/3/1980
Enter your salary: 34000
Mikkel was born on a onsdag, is 18.656.640 minutes old and earns kr.
34.000,00.

Tip

Good Practice

Consider whether your application needs to be internationalized and plan for that before you start coding! Write down all the pieces of text in the user interface that will need to be localized. Think about all the data that will need to be globalized (date formats, number formats, and sorting text behavior).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.12