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: , , ,

Thursday, 3 May 2007

Visual Studio Orcas Beta

I only downloaded the March CTP about 2 weeks ago. Then last week Microsoft released Beta 1.
Its not like I'm complaining... I have 20gb a month so whats the problem?

http://www.microsoft.com/downloads/details.aspx?FamilyID=5d9c6b2d-439c-4ec2-8e24-b7d9ff6a2ab2&DisplayLang=en

That's it. Download it. There's also a white paper on Orcas:
http://www.microsoft.com/downloads/details.aspx?FamilyId=17319EB4-299C-43B8-A360-A1C2BD6A421B&displaylang=en

You can find Virtual PC versions floating around for the March CTP but even more interesting is that you can also download Beta 1 of Team Foundation Server and Team Suite as Virtual PC images as well.

Personally, I'm looking forward to LINQ, XLINQ, and intellisense for Javascript amongst other things. ASP.NET Ajax is also fully integrated and supported in Orcas. Check the white paper appendix for a full list of all the new features.

Going with this release is the Beta release of .Net Framework 3.5. I must say that I am a little lost when it comes to Microsoft's version numbering. Anyway, no need to install all those extensions floating around... Just go for 3.5. Suffice to say that there are a lot of cheaper hosting companies that do not support .Net 2.0 yet (well mine anyway!) let alone, 3.0, or 3.5. The releases are coming thick and fast, although it seems an eternity since I first saw LINQ at Tech Ed last year.

Oh finally, check out the video from ScottGu on the Silverlight "Getting Started" page.
http://silverlight.net/GetStarted/

You can download the 1.1 alpha tools for building Silverlight applications in Orcas. And Expression Blend is available for 6 month trial from the Microsoft web site. You'll be able to fully recreate the integration that Scott demonstrates in the video.

I certainly will be once this 5gb download finishes...

Labels: , , , , , , ,