Today I’m going to start a series on Internationalization. Today’s post will start with the basics, but I plan to cover such issues as:
- Using Resources
- Detecting the language of the browser
- What about images?
- Dealing with Search Engine Spiders
- Internationalizing your database.
Using resource files is pretty easy, but there are some choices you will need to make.
Let’s assume you’ve already created your web site. You have a bunch of labels on the screen that you need to get into a resource file. What’s the best way?
The Easy Way
Well, the easy way would be to create a local resource. To do this, you bring up the ASPX or ASCX file you want to convert and choose “Tools” > “Generate Local Resource” from the menu. This will place a file with the same name as the ASPX or ASCX file in App_LocalResource with an RESX extension.
That’s the easy way. It also might make maintenance of the file easier than the alternative because you have a one-to-one relationship between the file you are Internationalizing and the RESX files you are updating.
The Flexible Way
But there is another way. You could create one resource file, or a set of resource files that are globally available. To do this, first create a directory named App_GlobalResources by right-clicking the web project in the solution explorer and selecting “Add ASP.NET Folder” > “App_GlobalResources” from the context menu. Then place a new resource file in the directory. I normally call mine “Resource.resx.”
For each string you want to provide internationalization for, you provide a name and a value. When I am working with Local Resources, I make the name of the resource element the same as the ID of the element, and this is done for you if you opted for the automated method. But when I’m working with Global Resources, I make the name as close to the string that I’m going to be using as possible. Most of the time it is exactly the same except that I’ve taken out any white space the string may have.
The reason I do this is because the whole reason I put the strings in the global context is so that I can re-use them between my pages.
Referencing In Code
To reference a Global Resource in your ASPX or ASCX file, you use the following syntax,
"<%$ Resources:Resource, OK %>"
where “Resource” is the name of the base resx file and “OK” is the name of the element in the resource file.
You can also access your resource file from code using the format:
Button1.Text = Resources.Resource.OK;
Once you have your default Resource file (or files) created, you can then move on to creating a resource file for each language and culture you want to support.
You do this by using the format:
So if I have a resource file name, Resource.resx, and I want to create a resource file for French in Canada, I’d use, Resources.fr-CA.resx
Any element that isn’t defined in the fr-CA version would be pulled from the default resource file.
Other post in internationalization
- DotNetNuke - Internationalization - June 25th, 2008
- DotNetNuke Modules - Internationalization (part 2) - June 30th, 2008
- Internationalization - Resource Files - March 24th, 2009
- Silverlight - RESX Files and Internationalization - April 2nd, 2009
- Internationalization – The Database - May 5th, 2009
- ASP.NET Internationalization – Themes - July 3rd, 2012
- 24 March 2009 at 8:03am
- Dew Drop - March 24, 2009 | Alvin Ashcraft's Morning Dew
[...] Internationalization - Resource Files (Dave M. Bush) ...