When working as a software developer in multi-cultural countries, like Malaysia and Singapore, localization of your app is very important sometimes. To reach out more users, it’s better if your app can communicate with the users no matter what language they speak.
Resource Files for ASP.NET
I tried doing localization for OneSync two years ago when I was developing its third version. The localization for C# desktop app is easy and straight-forward. It just needs to have different resource files for different languages and then retrieve the information from the files based on user’s selected language.
So, how about ASP.NET? It is basically the same thing. Resources are available in ASP for web pages contents localization as well (Reference: http://msdn.microsoft.com/en-us/library/ms228208.aspx). In ASP, there are two types of resource files: Global Resource Files and Local Resource Files. According to MSDN (http://msdn.microsoft.com/en-us/library/ms247246.aspx), global resource files are available to any web page in the web site and local resource files are only associated with a single web page. Does that mean we should store the resources that will be used in more than one web page under the Global Resource Files? I guess so. However, I once tried putting all my localized texts under the Local Resource Files folder. It did not actually stopping me from retrieving the localized texts in the local resource files from more than just one web page. I’m still not sure why it’s so.
Where to Store Language Setting?
One place to store the user language setting for your ASP website can be cookies. This is because information about user language selection should be saved permanently. Actually it’s not really permanent storage because the Expires field of a cookie must be set. If it’s not set, then the cookie will be treated as a session, meaning the cookie expires when the session ends. Thus, no expiry date means the cookies won’t be stored in the user’s hard disk at all (Reference: http://msdn.microsoft.com/en-us/library/ms178194.aspx).
To create a new cookie, I currently use the following codes.
Response.Cookies[“language”].Value = “Bahasa Melayu”;
Response.Cookies[“language”].Expires = DateTime.Now.AddDays(365);
Here, I make the cookies expiration to be one year later of its creation.
To read the user language setting from the cookie, I just need to use the following code: Request.Cookies[“language”].Value and then I can happily get the user language selection.
Yup, that’s all with my first attempt in ASP localization.