ASP.NET 2.0 Culture – Web Site Internationalization

Last night I watched the movie Paris, Je T’Aime, a collection of short films set in various arrondissements. This is undoubtedly one of the best recent films to see before you travel to Paris. I especially liked the short film 14e arrondissement (XIVe arrondissement) – written and directed by Alexander Payne. It is a brutally honest and mundane vignette about a middle-aged American mail carrier from Denver on vacation in Paris who is sad that she has to experience it alone. It is narrated in her atrocious attempt at French. This will probably be embarrassingly similar to my experience!

However, this blog post is about the technical issues of web development for international sites. I’ve never been asked to give any consideration to foreign languages or cultures in my work because I haven’t worked on any major web sites that are intended to serve the world market. I’ve mostly done back end administration sites which certainly don’t need to be bilingual. Web developers have enough headaches just making sure web sites appear the same in every browser without worrying about a number of foreign languages in addition to that.

Fortunately ASP.NET is slightly more advanced than other web development frameworks in this regard. If you want your web page to format dates, times, and currency for France you just need to set the culture in the page directive:

   1: <% Page Language="VB" MasterPageFile="~/Default.master" Culture="fr-FR" UICulture="fr-FR" AutoEventWireup="false" CodeFile="Langue.aspx.vb" Inherits="Langue" title="French Culture Test Page"  %>

The Internet-standard string that identifies the French language is fr-FR (French in France). fr-CA would be French in Canada. This changes the display of full dates to something like mercredi 18 février 2009, short dates to 18/02/2009, short times to something like 15:21, and currency amounts to 56,95. Notice how the full date automatically uses the French words for the weekday and the month without any effort on your part. The short date places the day before the month in the European fashion. The time is in military time because the French do not use AM and PM. And the currency uses a comma instead of a period and automatically added the Euro symbol.

You could also set the Culture and  UICulture attributes to “auto” and get the same effect if your browser language is set to fr-FR. However changing your browser language can have unintended consequences as every web site you visit may redirect you to a French version of the site or display some things in the French format. It does give you a good way to test sites for internationalization. It is surprising how many sites use country flags to direct visitors to translated pages when they should just use the browser’s language settings to determine this.

If you want to dynamically replace strings with foreign words or phrases then you’ll have to do a lot more work. ASP.NET 2.0 provides a standard way of accomplishing this using local resource files. First you have to create an ASP.NET Folder for this. Right click your project in Solution Explorer and select Add ASP.NET Folder, select App_LocalResources. Then with an ASP.NET page in design view, select Tools > Generate Local Resource. This command automates the creation of a local resource file for the default culture. It also adds meta:resourcekey attributes throughout the page and creates corresponding string values in the local resource file to act as targets for meta:resourcekey attribute entries. That should give you the English version of your page.

To create a French version of the page you’ll have to create a local resource file containing French resources. You need a resource file named Default.aspx.fr.resx for Firefox and a resource file named Default.aspx.fr-FR.resx for Internet Explorer. When you open the resx resource files in Visual Studio you’ll see a lot of name / value pairs where the name will be the name of a web control and the value will be the foreign words you want to appear there.  If you want to localize static content you can use the special ASP.NET tag asp:Localize for anything which does not get referenced in the code behind page.

   1: <asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1"></asp:Localize>

PHP does not have a standard way to do this kind of localization. You’ll find various methods for replacing language specific strings in different web applications should they be language aware. For example, Elgg uses a language folder where you place PHP files, each defining an array of phrases for a particular language. Then your other PHP files must use the key name to get the array value which will be a string in the target language. This is very similar to ASP.NET’s approach but other PHP web applications use entirely different methods.

I will be doing additional research on this topic as I spend more time brushing up on my French.

This entry was posted in ASP.NET, General, Programming, Web and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.