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

Resources and Localisation in C# Projects

Using resources to localise and application with C#

Written By on in C#

577 words, estimated reading time 3 minutes.

In this tutorial we will see how to use global resource files to create a web application that can cope with multiple languages as well as localising a display to a user's location.

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

This article relates to an old version of the .Net Framework. While the contents are still relevant, future versions of the framework may change, or improve upon, the contents of this article.

Creating Resource Files

Create a new website using Visual Studio or Visual Web Developer, then create a new ASP.Net folder by right-clicking on the project in Solution Explorer, followed by Add ASP.Net Folder, then App_GlobalResources. Next, right click on this new folder and Add New Item. Select Resource and call it 'GlobalLanguages.resx'. This will create the default language file and Visual Studio will then display the resource designer window where we can enter our data into. GlobalLanguages can be any name you wish, just substitute in your name for GlobalLanguages in the code below.

On the left column (name) you enter an identifier name for the string, for example, "WelcomeMessage" and in the right-hand column (value) enter the actual message, for example, "Hello and Welcome to our Website". You can optionally add information into the comments column.

Save and close this file, then from Solution Explorer duplicate this file, giving it a name of GlobalLanguages.it.resx (it being the two-letter country code for Italy/Italian). Replace "Hello and Welcome to our Website" with the Italian "Ciao e benvenuto al nostro Web site". Save and close this file.

Resources are strongly typed, meaning that you can access them with IntelliSense using the methods shown below. After you make changes to the resource files you will need to build the project for IntelliSense to pick up on the extra or modified values.

Displaying Resource Messages

Now, all we need to do is output these messages on the screen. There are a few methods for this, so pick the best one for your needs.

Setting the Value of a Label in C# Code behind (preferred method)

Label1.Text = Resources.GlobalLanguages.WelcomeMessage;

Raw HTML in the ASPX

<a href="#"><%=Resources.GlobalLanguages.WelcomeMessage %></a>

Reading the Value into a String object

string WelcomeMessage = Resources.GlobalLanguages.WelcomeMessage;

When you run the application you will see the English message written out on the screen.

Changing Resource Languages

You can change the resource file language that is displayed by changing the current resource provider culture. The constructor of the CultureInfo class takes the five character language code to use from the Messages.xx.resx filename. Shown below is the code to change to Italian.

Resources.GlobalLanguages.Culture = new CultureInfo("it");

For the purposes of this tutorial I have hard-coded the language, but it can be loaded from a configuration file, query string, session variable or profile information or any other method you desire. If you specify a language to the CultureInfo that does not have a Messages.xx.resx file then the default Messages.resx will be used.

You can also use string placeholders in the resource file, so you could have a string "Welcome back {0}, you have {1} new messages" and use a string.format to enter usernames and numbers. You can see an example of this in action in the sample download below.

The example uses three languages - English, French and Italian with buttons to change the language. I am also outputting the date so I have changed the System.Threading.Thread.CurrentThread.CurrentCulture to match the language selected so that when the day is outputted the system automatically outputs the correct day for the language.

Last updated on: Thursday 22nd June 2017

 

Comments

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