Asp. NET System Internationalization Summary

Source: Internet
Author: User

Introduction

System requirements at the same time to support the Chinese and English, before the internationalization of this piece is just heard, never do their own, referring to internationalization so the first must think of is a resource file, it is indeed so, I started from the beginning of the system internationalization function back and forth altogether invested 4 times, each time about 1-2 hours or so, So that I can not despise him, or will be confused.

What does internationalization have to do with the work?

1, of course, is a resource file. It is the first gateway to the internationalization of the system, and the resource files need to be prepared in both Chinese and English, that is, the Resource.resx and Resource-en.resx files, the global resource files that I use.

2, the System menu. Normal business logic, our system menu data are in the database, then how to decide to read from the database in the text or English fields, you need to have a logo to the decision, this is the session, with the session to record the current system user selected language.

3. The text in the ASPX page.

4, JS file in the Chinese characters.

5. What if you use server controls in addition to HTML controls?

Preparatory work

1, resource files. corresponding to Chinese and English resource documents respectively

2, BasePage page, dependent on the resources of the file pages are required to inherit the BasePage page, this page should be known.

1  Public Partial classBasePage:System.Web.UI.Page2     {3         #regionVariable4 5         protectedResourceManager Rmcommon =NewResourceManager ("RESOURCES.RESOURCE.ZH-CN", assembly.getexecutingassembly ());6         #endregion7 8         #regionPage_Load9 Ten         protected voidPage_Load (Objectsender, EventArgs e) One         { A  -         } -         #endregion the  -         #regionSystem language -  -         protectedsyslanguage jrsclanguage +         { -             Get +             { A                 if(session["Language"] !=NULL) at                 { -                     if(session["Language"]. ToString () = ="ZH-CN") -                     { -                         returnSyslanguage.chinese; -                     } -                     Else in                     { -                         returnSyslanguage.english; to                     } +                 } -                 Else the                 { *                     //Chinese is default if system language is not currently available $                     returnSyslanguage.chinese;Panax Notoginseng                 } -             } the  +  A         } the         #endregion +  -         #regionInitializing a multi-lingual environment $  $         /// <summary> -         ///initializing a multi-lingual environment -         /// </summary> the         protected Override voidinitializeculture () -         {Wuyi             Try the             { -                 Wu  -            stringLanguage = request.form["__eventtarget"]; About  $                 if(!string. IsNullOrEmpty (language)) -                 { -                     stringLanguageID =Request.form[language]; -                     if(!String.IsNullOrEmpty (LanguageID)) A                     { + setculture (LanguageID); the                     } -                 } $                 Else the                 { the                     if(session["Language"] !=NULL) the                     { the                         stringCurL = session["Language"]. ToString (); -                     } in                                     } the  the                 if(session["Language"] !=NULL) About                 { the                     stringCurL = session["Language"]. ToString (); the                     if(string. IsNullOrEmpty (CurL)) theCurL ="ZH-CN"; +                     Else if(Curl.tolower (). Contains ("ZH")) -CurL ="ZH-CN"; the                     ElseBayiCurL ="en -US"; the  the  -CultureInfo ci =Thread.CurrentThread.CurrentCulture; -                     stringCur =CI. Twoletterisolanguagename; the                     stringCurname =CI. Name; the  the                     if(Curname.tolower ()! =curl.tolower ()) the setculture (CurL); -                 } the  the                 Base. InitializeCulture (); the             }94             Catch(Exception ex) the             { the Console.WriteLine (ex. ToString ()); the             }98         } About         protected BOOLIsLogin =true; -         protected Override voidOnInit (EventArgs e)101         {102             Base. OnInit (e);103         }104  the         protected voidSetculture (stringLanguageID)106         {107             Try108             {109                 //session["Language"] = LanguageID; the 111CultureInfo ci =cultureinfo.createspecificculture (LanguageID); theThread.CurrentThread.CurrentCulture =ci;113CI =NewCultureInfo (LanguageID); theThread.CurrentThread.CurrentUICulture =ci; the  the             }117             Catch(Exception ex)118             {119 Console.WriteLine (ex. ToString ()); -LanguageID ="en -US";121CultureInfo ci =cultureinfo.createspecificculture (LanguageID);122Thread.CurrentThread.CurrentCulture =ci;123CI =NewCultureInfo (LanguageID);124Thread.CurrentThread.CurrentUICulture =ci; the 126             }127         } - 129         #endregion the 131       the 133         134 135         136}
View Code


3, stored in JS need to be internationalized text

var js_language = ""= $.cookie (' js_language '); var New Array (); logout["ZH-CN" = "Are you sure you want to exit the system?" "; logout["en-us"] = "is sure to exit?";
View Code
ASPX page internationalization

After the page inherits BasePage, you can use <%=this. GetGlobalResourceObject ("Resource", "Home_Page")%> get the corresponding text

System Menu Internationalization

When the page is selected by the login page, the language option is stored in the session, and when the first page loads the menu, it reads the corresponding menu display text, and the other pages need the same text to resolve. Sometimes the home page also need to add the international switching function, when the first page in the switch needs to update the session logo, that is, I defined in BasePage session["Language".

Text internationalization in JS file

Referring to the text in the JS file internationalization can be a small physical life, the JS script written in the ASPX page, then you can follow the page to access the way to access, if it is a separate JS file, it is necessary for migrant labor. So using the JS files mentioned in the preparation work, each phrase that needs to be translated defines an array, the corresponding language and the corresponding translated text in the array, and a cookie is used to select the appropriate language according to the value of the cookie when you use it.

Server control text Internationalization

I was surprised, and I found it today, for the hair I am in the server control text also directly write <%%> error? There is no way to use a stupid method, only in the Page_Init function according to session["Language" to translate.

Summarize

First, the internationalization of the overall situation is a pure physical activity, need to consider the resource file, aspx page, JS file. At first I did not care, think the resource files ready, after 4 times after the big change I found that these details are still to be taken seriously, in my second big change when I was in the background is the session also used, cookies are also used, after switching the language found that the value of the cookie store after the page can not be found, To know that the cookie cross-page is sure to exist, and again depressed, so there is a third and the fourth time the big change to basically complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.