.comment-link {margin-left:.6em;}
Books & Articles I wrote.

Wednesday, May 10, 2006


Localizing ASP.Net 2.0 Web Apps

I woke up at 5 Am this morning fairly sane. By 7AM i was in the street, naked, talking Japanese (which i don't speak a word of) to small invisible snakes.

Why? Because of ASP.Net 2.0 resource files. They are NUTS and really need to be made easier. If you are localizing your web app then you can use resource files in .Net to take care of the correct ouput. Let's skip that though and get to the crux of the problem and how i solved it.

You can just creat a resx file in the App_GlobalResources directory and ASP.Net takes care of it for you. BUT, if you want to programatically access that resource file you are in for a headache and it is a dynamic assembly and so you don't actually know the full namespace until compile time (and then it's too late - i'd be interested if anyone used reflection successfully to get the dynamic namespace and loaded the resource file). Quite simply i tried a few things and then stopped, deciding there must be an easier way. You see i wanted to do this :


I just kept getting this :

Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.baseName: BlahResources locationInfo: fileName: BlahResources.resources

I'm sure it worked the first time - seems so long ago that i can't be sure anymore. Anyway, I followed a host of sites saying "add the project name" - well because the aseembly is dynamically compiled then you cannot state what the final name will be. So none of these "solutions" were working (i think most applied to .Net 1.x to be honest).

So here is my solution:

1. Create a .resx file and add your localized data in the normal way. Create a neutral file and then language specific files as desired (myresource.resx, myresource.en.resx, myresource.es.resx ...)

2. *Important* - Compile each resx file to a .resources file using ResGen.exe.

3. Place this .resources file in some other directory (say a "resources" directory).

4. In CreateFileBasedResourceManager() reference the .resources file location.

CreateFileBasedResourceManager("MyResource", Server.MapPath("/resources"), null);

That's it - it should work fine now. That gotta make this easier!

This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com