Last post Nov 27, 2008 08:30 AM by MAlex001
Nov 19, 2008 01:46 AM|MAlex001|LINK
Hi, I am implementing a multi language web site.
Everything is ok for static informacion. But I do not know what to do it with controls that the text is dinamic, when the text is set up dinamically.
For example, in the Log In page, this is a label that can show different message, like "Username/Password wrong" or "Server not working " etc.
One way to do it can be :
else if (lang="en-GB")
Is that the right way to do it ? Is there another way using resources?
Also I have created for each aspx page three resources files, one for each language I need to implement. For example, for an aspx page called Controller.aspx, I have created
Can I create a single resx file for all the pages for each language? Or it have to be a resource file for each page and with the name of the resource file containing the aspx name ??
I have done same test and It seems that only work when the name of the resource files is like asppagename.aspx.resx
Thank you in advance
Nov 24, 2008 10:48 PM|Nai-Dong Jin - MSFT|LINK
Generally, you do not need to handle it manually. ASP.NET 2.0 comes with a new auto-culture handling feature to make the task of localizing your application easier.
Auto-culture handling can be enabled for each page by including the Culture="auto" and UICulture="auto" attributes in the Page directive of each page. Once enabled, the ASP.NET runtime will automatically map Accept-Language headers to CultureInfo objects and
attach them to the current thread (unlike ASP.NET 1.x, where you need to do this manually). As a developer, you simply need to prepare the resources for the different cultures you want to support in your application. ASP.NET will then do the work of loading
the appropriate resources for each culture as they are needed.
For more instruments and tutorials on how to achieve that, see:
Nov 25, 2008 03:23 AM|saedh|LINK
To supplement what Nai kinldy said, you could also consult MSDN. They have a nice and helpful tutorials about Localisation and Globalisation, in particular working with Resource files, which helped me alot progressing in that direction.
Simply put, you could either work Locally or Globally. Locally implies operating on a page level, i.e. one resource set for each page as you did, which you need to place inside
App_LocalResources. However, you don't need to do that on Global level; one set of resource files for entire website would be enough, which need to be placed inside
App_GlobaResources; one file per language. This is the common and recommended practice my friend. For example, you could create the following set to hold localisation of Controls:
Controls.resx (the default, which will be used for fallback)
Note naming convention: no reference to aspx. All these resource files need to have identical Resource Keys (Names), but different values. The easiest way to do it is to create the fallback file first (i.e. Controls.resx), identify all the Keys with their
values, then copy and rename it as above. Now for each language, simply open its associated file and change the values. Of course, you can have more than one set, for instance,
Controls to hold Control related localisation,
Messages to hold your localised messages, and so on.
The name of Resource set, e.g. Controls, Messages, etc is known as
BaseName and will form the reference to it as a class, which paves the way ahead to reference their Keys inside your code. Back to your example, assuming you define a Resource Key as
Name, you dynamically set Label text to
Name's Value as follows:
this.Label.Text = Resources.Controls.Name
or if you wish to utilise Messages set:
this.Label.Text = Resources.Messages.Msg1
That's all! ASP.NET will extract Key value from the resource file relevant to current Culture... as simple as that. Therefore, you should ensure that switching between different languages is properly detected and handled in order to successfully set
Current Culture, hence, successful resource referencing.
Hope this helps.
Nov 27, 2008 08:30 AM|MAlex001|LINK
Thanl you very much for your answer guys. I really appreciate your time.