Web Design that taps into the haromny and vision of your dreams.

Localising Microsoft .Net Applications with C#

Localising Microsoft .Net applications and resource files

Written By on in C#

651 words, estimated reading time 3 minutes.

In this tutorial, we will have a look at localising Microsoft .net applications using resource files and satellite assemblies.

Using Visual Studio Series
  1. Getting Started with Visual Studio
  2. Using the Visual Studio Integrated Development Environment
  3. Using the Visual Studio Debugger
  4. XML Documentation in Visual Studio
  5. Using Microsoft .Net Command Line Tools
  6. What is an Assembly in Microsoft .Net?
  7. Creating and Using a .NET Assembly in C#
  8. Creating and Implementing Satellite Assemblies
  9. Creating Strong Named Assemblies in Microsoft .Net
  10. Visual Studio Task List
  11. Resources and Localisation in C# Projects
  12. Localising Microsoft .Net Applications with C#
  13. Using Resource Files in C# Applications
  14. Using XSD Tool to Generate Classes from XML
  15. 10 Visual Studio Tools You May Not Know About

Localisation is the process of writing software that is able to sense the user's locale and change its behaviour based on that information. This includes language, date and number formatting and currency.

We will revisit the Hello World application and localise it to demonstrate the techniques and features used in created global applications.

using System;
class Hello
  public static void Main()
    Console.WriteLine("Hello World!");
    Console.WriteLine("The Time is: " + DateTime.Now.ToString());
    Console.WriteLine("Please enter your name: ");
    string name = Console.ReadLine();
    Console.WriteLine("Hello " + name + ". Welcome to our test program.");

This application will look the same regardless of the user's locale, be it Japanese, Chinese, Arabic or Italian. In order to localise the application we need to remove all references to hard coded values, but first, we need to create our resource file. This is done by adding a resource file to the project from the "add new item" menu. It is advisable to create these resources within a separate folder in your solution to keep the together. This resource file will be our default language so name it "resources.resx".

By default, Visual Studio will show the resource editor, if it hasn't shown you can open the file from the solution explorer.

We are going to need to add a few string resources to the file, one for each string that has been hard-coded in the application.

Key nameValue
HelloWorldHello World!
TheTimeThe Time is: {0}
EnterNameHello World!
HelloMessageHello {0}. Welcome to our test program.

Now our program needs a few changes in order to display these new values. Since the resources file is a strongly typed data object we can use resources as an object and access its properties (resources are the name of the resource file. You can change the name to match the file you created).

using System;
namespace ConsoleApplication1
  class Hello 
    public static void Main()
      Console.WriteLine(string.Format(resources.TheTime, DateTime.Now.ToString()));
      string name = Console.ReadLine();
      Console.WriteLine(string.Format(resources.HelloMessage, name));

Now we have an application with no hard coded strings. You will notice that I have used a string.Format to output data. This allows different languages to change the position of a name or any data object within the string.

Adding Languages

To create different language resources simply copy the default language file and rename it including the culture identification, e.g. for France our resources.resx will be renamed resources.fr-FR.resx.

Visual Studio does not compile all the resources into a single output assembly. Instead, it compiles each of these localised resource files into a separate assembly called satellite assemblies.

Your project structure will now look like this:

- ConsoleApplication1.exe
- fr-FR
- - ConsoleApplication1.resources.dll
- it-IT
- - ConsoleApplication1.resources.dll
- de-DE
- - ConsoleApplication1.resources.dll

You will notice that the default resources are not shown, this is because they have been compiled into the application executable.

You can also generate satellite assemblies manually using the Assembly Linker tool, details of which can be found in our tutorial Creating and Implementing Satellite Assemblies.

This technique can also be used for using application settings.

Localized Formatting

In addition to strings, other data such as currency, dates and numbers must also be localised so that they display in the way the user will expect.

For example, in England, one million is written 1,000,000.00 but in France, it is written 1 000 000,00 and in Germany 1.000.000,00. Numbers can be written using the correct formatting using the ToString method of the object.

int i = 1000000;
Console.WriteLine(" " + i.ToString("N"));

Where N is the format character for an int. If you are outputting currency you can use C instead, which will format the number as well as add a currency symbol in the correct place.

Last updated on: Friday 8th September 2017



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me