Monday, March 5, 2012

Embedding Application Resource Dictionaries

Recently I have been working on a large scale project that requires localization into dozens of different languages. The localization is required not only for the content itself, which is displayed by touch screen applications, but also by the email services in the back-end, which are used by the applications to send information to users via email.

The email services application was implemented using ASP.NET MVC and WCF to expose not only a RESTFul service but also a web interface to view emails on-line. Along with this, we created a small test app that we use to test the email services and generate emails on demand.

Now, since the email's copy don't change often, we decided to use resource dictionaries to localize the email's copy. The advantage of using resource dictionaries is that you define a master template for the resources, and then just add localized copies of it which will be used depending on the region the content is associated with.

To embed a new resource dictionary, follow these steps:

1- Make sure you have your global resource dictionaries setup with valid Culture Info region and that you have your base/default resource file. You will get mysterious errors if you are trying to use a resource dictionary that doesn't have a valid .NET Culture Info code.

2- To add a new resource file, you could right click on the "App_GlobalResources" folder, select "Add New Item" and chose the "Resources File" template, but is a lot easier just to select one of the existing resource files which already contains all the string keys and copy it there. That way you will have all your string keys populated.

3- Once you have the copy, rename it to the corresponding Culture Info. Then you can open the resources file and enter all your localized values.

4- Don't forget to check the resources files properties. You need to make sure that it is set to be an "Embedded Resource" and use the appropriate resource file generation tool.

Re-build your app and test it. You should get the corresponding localized resource dictionary when selecting the corresponding region. In our case, we have the content associated to a given region, so when the touch application sends the requests to generate and send an email, we check the content region and use the corresponding email localized resource file.

This will also work if you are displaying the localized content of your app associated with the browser locale. You can read more about ASP.NET resource files here.

Happy localized coding !

No comments:

Post a Comment