Monday, October 19, 2009

Localizing images

It is a common practice these days to localize applications. Well there are are number of options to localize the static text labels in an application. But how many times you have wanted to have some texts in your images and load them based on the current-thread's locale? One option is you can programatically set the ImageUrl to the asp.net image controls based on the current locale. But how many lines of code you would have to write to get this done? With .Net 2.0 Implicit Localization you can achieve this with zero code lines. Here is how we would do it.


step 1 : have your images placed on different directories based on the locale. i.e images/en/image1.jpg , images/es/image1.jpg and so on.

step 2 : have a resource key in the respective resource files pointing to the above images.
i.e in Page.es.resx file we might have a key BannerImage.ImageUrl with the value "~/images/es/image1.jpg"

step 3 : make the implicit localization do the rest of the job by properly formatting it.



Now when ever the BannerImage is rendered in the page life cycle, the ImageUrl will be picked from the resource files and based on the value for the current locale.

Hope that helps.

/BB