A preface
The interface supports multiple languages and encounters the following problems when using ASP.net's self-contained multilanguage scenario:
When you do the functions of the management class, have to add, modify and view the page, need to support multi-language control is basically the same, but to maintain multiple, resulting in redundancy (ASP.net has shared resources, but it is global, can not be divided into modules, we can not be the module of the information into the global resources);
The key in the resource file must be specified in the page;
When the page slow down a long time, the page and resource matching is difficult to maintain;
So I think an ideal support for multi-language frameworks requires the following features:
The data redundancy problem is solved by the module.
Automatically match the link between the page and the resource file;
Easy to maintain, can be quickly positioned through the page to the resource file;
Multi-language implementation to support background messages
Multi-language implementation to support the foreground JS message
Realization of two-background message multi-language
In the implementation of background message, the main use of asp.net characteristics, by modifying the current thread of the regional language, to obtain the corresponding version of resources. Because ASP.net uses a separate thread to execute all the requested code at the time it processes the request, we only need to rewrite the language information of the current thread in one place, and to get the current language version of the resource file anywhere else. The code to override the locale language of the current thread is as follows:
protected override void InitializeCulture ()
{
String language = "en";/the default is English
if (session["language"]!= NULL) language = Session
["Language"]. ToString ();
Thread.CurrentThread.CurrentCulture = cultureinfo.createspecificculture (language);
Thread.CurrentThread.CurrentUICulture = cultureinfo.createspecificculture (language);
}
When you name a resource file name, the rules are as follows:
The default language version is directly named as normal, such as named Orderresource.resx
Other versions of the resource file name should be added with the zone name, such as Orderresource.zh-cn.resx, Orderresource.en-us.resx.
After a background message is placed in a resource file, only the master's resource file is referenced in the program, so that when a thread's zone information changes, it automatically references other language versions of the resource. For example, use the following code in your program:
Response.Write (orderresource.filedrequired)/////When zone information is in English, output "The Filed is required!" When the zone information is Chinese, the output field must be filled in! "
The realization of three-foreground JS multi-language
The multilingual implementation of the foreground, that is, in the JS need to pop up some prompts also need multi-language. Front-desk multilingual implementation of a number of implementations, in this case, we adopt a unified management approach, the JS multilingual information is also placed in the resource file, but in a public place, the resource sequence into a JSON object, and then JS can be used. Of course, when implementing this functionality, you need to do some caching work. The main code is as follows:
public static dictionary<string, string> jsonresources = new dictionary<string, string> (); Serializes the resource file into JSON public static string Getjsonresource () {string key = string. Format ("jsresource.{
0} ", System.Threading.Thread.CurrentThread.CurrentCulture.Name); if (! Jsonresources.containskey (key)) {JavaScriptSerializer Serialzer = new JavaScriptSerializer ()
; ResourceSet rs = JSResource.ResourceManager.GetResourceSet (System.Threading.Thread.CurrentThread.CurrentCulture,
True, true); String json = string. Format (' [{0}] ', Serialzer. Serialize (Rs. Oftype<dictionaryentry> ().
ToDictionary (x => x.key, x => x.value)));
Jsonresources.add (key, JSON);
return Jsonresources[key]; protected override void Onprerendercomplete (EventArgs e) {//Output JSON object to page Page.clie Ntscript.registerstartupscript(Page.gettype (), "", "<script language= ' JavaScript ' >var json=eval (" + resourcecache.getjsonresource () + ");
Jsresource=json[0];</script> "); }
You can use the Jsresource object in the page, such as alert (window). JSRESOURCE.JSMSG);
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/