Internationalization – Resource Files

C01H0025 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;

How would you do the same with a Local Resource?  Well, you wouldn’t.  You could, but compared to using a Global Resource, it’s much harder so you once you find out how, you won’t want to do it.

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:

BaseFileName.language-culture.resx

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.

Related Post

  • Silverlight – RESX Files and InternationalizationSilverlight – RESX Files and Internationalization One of the toughest things to get your head around in Silverlight is Internationalization and Localization.  But once you figure out the quirks and the parts that are missing in Visual Studio...
  • Internationalization – The DatabaseInternationalization – The Database Over the last several weeks we’ve been examining the various aspects of internationalization using ASP.NET. But it doesn’t help to have your resources and images set up for internationalization i...
  • ASP.NET Internationalization – ThemesASP.NET Internationalization – Themes A couple of weeks ago I started a series on ASP.NET Internationalization where we started by covering the basics, that is, using RESX files to supply language-specific strings in our code. Howe...
  • DotNetNuke – InternationalizationDotNetNuke – Internationalization One of the core features of DotNetNuke is that all of the text that shows up on a page either comes out of the database or out of resource files.  Anything that comes out of the resource files can ...
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it wrong, which...