Steven Nagy .NET

Tuesday, 22 May 2007

Introduction to Globalisation

Introduction to Globalisation

Tonight I attended a presentation as part of QMSDNUG and learnt a little about globalisation. I was quite impressed by VS2005 capability and thought it required a little further investigation.

I'll run through a very brief, simple example of using cultures to control resources on an ASP.NET page. To start, open a new web site in VS2005. You get the usual 'default.aspx' open by default. Drag a label onto your form, name it and give it some text. If you were to view the HTML, it could look something like this:

<asp:Label ID="lblWelcome" runat="server" Text="Welcome"></asp:Label>

One thing about dealing with globalisation is that you will NEVER directly insert text into a form. Always use a label, even for supposedly static text. This allows us to change the text, either programatically, or through the globalisation techniques I am about to show you.

Switch back to design mode. If you click on 'Tools' you will see a menu option called 'Generate Local Resource'. If it is not selectable, you need to click somewhere on your page, such as your label. Click 'Generate Local Resource'. A window pops up at the bottom of the screen indicating something about creating the resource.

When it's finished, there will be some key changes to your solution now. First, in solution explorer there will be a new special ASP.NET folder called 'App_LocalResources' and in it will be a new file called 'Default.aspx.resx'. This is the resource file for your default.aspx page. We'll look at it in a second.

Now, go back to view the HTML of your label and it might look something like this:

<asp:Label ID="lblWelcome" runat="server" Text="Welcome" meta:resourcekey="lblWelcomeResource1"></asp:Label>

Notice the new attribute: 'meta:resourcekey'. This indicates what resource key will be used for this server side control. In fact that key can be found in the new resource file. Open the .resx and you will see that there are some (3) properties for that key 'lblWelcomeResource1' such as Text and Tooltip. By default they have some values in it. Lets change them a little. I'll change the text to 'Welcome everyone' and the tooltip to 'This is english'.

Run the application, and you see that the text on the label is not 'Welcome' as expected, but 'Welcome everyone'. The default text is ignored, and the resource file attributes are applied.

So now lets make this truly culture specific. Duplicate the 'Default.aspx.resx' (copy and paste to same folder). Name it: 'Default.aspx.en-US.resx'. Now open the new resource file and change the text to 'Welcome PARTNER' and the tooltip to 'This is US english'. You'll notice that the key is still the same for the resources in this file. This is a good thing because it means the label on our form will use this key for this particular culture (en-US).

Run the application, and you see... NOTHING HAPPENS! That's a good thing because, if like me you are in Australia, our browsers are configured for Aussie english, ie. en-AU

Keep the browser running, and go to internet options, Languages, and remove en-AU and add en-US. Close modal windows, and refresh browser. "Welcomer PARTNER".

So you see how this works. When the http headers pass the user's culture, .NET loads the resource file for that culture. If it doesn't find that culture it defaults to the default culture settings. In fact, you can specify general languages, such that if it doesn't find 'en-US' it will fall back to a 'en' and if it doesn't find that, will fall back to the default (invariant) settings.

This is just the surface of what you can do, but here's a few quick pointers that might save you some hassle:

  • You can control lots of properties of items through resource file

  • You can add your own entries into the resource files

  • The resource files can be accessed programatically (when I learn it, I'll show it)

  • If you add more controls to your form, you need to regenerate resources again

  • Multiple controls can share the same resource key (just set it to the key you want in the HTML)

  • If you delete a control, it does not delete the resource key (because of previous point)

  • It is possible to store resources in adatabase by custom implementing the resource provider (too advanced to discuss right now)



Here's an article that will give you more info on CodeProject.

Have fun globalising, localising, and internationalising!

Labels: , , ,