Summary of ASP. NET Internationalization

Source: Internet
Author: User

Introduction

The system must support both Chinese and English. I have never heard of internationalization and I have never done it myself. When I mention internationalization, I must first think of resource files, as a result, I have invested a total of four time periods before and after starting to internationalize the system. Each time it takes about 1-2 hours, so that I don't think I should despise him. Otherwise, it will be messy.

What kind of work does internationalization require?

1. Of course, it is a resource file. It is the first entry of the system's internationalization work, the Resource file needs to prepare both Chinese and English, that is, the Resource. resx and Resource-en.resx files, I use the global Resource file.

2. System menu. For normal business logic, our system menu data is stored in the database. Therefore, how can we determine whether to read Chinese or English fields from the database, this identifier is the Session, which is used to record the language selected by the current system.

3. Text on the aspx page.

4. Chinese characters in the js file.

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

 

Preparations

1. Resource file. Corresponding to Chinese and English resource files

 

2. For the BasePage page, all pages dependent on resource files must inherit the BasePage page, which should be well known.

1 public partial class BasePage: System. web. UI. page 2 {3 # region variable 4 5 protected ResourceManager rmCommon = new ResourceManager ("Resources. resource. zh-cn ", Assembly. getExecutingAssembly (); 6 # endregion 7 8 # region Page_Load 9 10 protected void Page_Load (object sender, EventArgs e) 11 {12 13} 14 # endregion 15 16 # region System Language 17 18 protected SysLanguage JrscLanguage 19 {20 get 21 {22 if (Sess Ion ["Language"]! = Null) 23 {24 if (Session ["Language"]. toString () = "zh-CN") 25 {26 return SysLanguage. chinese; 27} 28 else 29 {30 return SysLanguage. english; 31} 32} 33 else 34 {35 // if the system language cannot be obtained currently, the default value is 36 return SysLanguage. chinese; 37} 38} 39 40 41} 42 # endregion 43 44 # region initialize a multi-language environment 45 46 // <summary> 47 // initialize a multi-language environment 48 /// </summary> 49 protected override void InitializeCulture () 50 {51 try 52 {53 54 55 string language = Request. Form ["_ EventTarget"]; 56 57 if (! String. IsNullOrEmpty (language) 58 {59 string LanguageId = Request. Form [language]; 60 if (! String. IsNullOrEmpty (LanguageId) 61 {62 SetCulture (LanguageId); 63} 64} 65 else 66 {67 if (Session ["Language"]! = Null) 68 {69 string curL = Session ["Language"]. ToString (); 70} 71} 72 73 if (Session ["Language"]! = Null) 74 {75 string curL = Session ["Language"]. toString (); 76 if (string. isNullOrEmpty (curL) 77 curL = "zh-CN"; 78 else if (curL. toLower (). contains ("zh") 79 curL = "zh-CN"; 80 else 81 curL = "en-US"; 82 83 84 CultureInfo ci = Thread. currentThread. currentCulture; 85 string cur = ci. twoLetterISOLanguageName; 86 string curName = ci. name; 87 88 if (curName. toLower ()! = CurL. toLower () 89 SetCulture (curL); 90} 91 92 base. initializeCulture (); 93} 94 catch (Exception ex) 95 {96 Console. writeLine (ex. toString (); 97} 98} 99 protected bool IsLogin = true; 100 protected override void OnInit (EventArgs e) 101 {102 base. onInit (e); 103} 104 105 protected void SetCulture (string languageId) 106 {107 try108 {109 // Session ["Language"] = languageId; 110 111 CultureInfo ci = CultureInfo. createSpecificCulture (languageId); 112 Thread. currentThread. currentCulture = ci; 113 ci = new CultureInfo (ageid); 114 Thread. currentThread. currentUICulture = ci; 115 116} 117 catch (Exception ex) 118 {119 Console. writeLine (ex. toString (); 120 languageId = "en-US"; 121 CultureInfo ci = CultureInfo. createSpecificCulture (languageId); 122 Thread. currentThread. currentCulture = ci; 123 ci = new CultureInfo (ageid); 124 Thread. currentThread. currentUICulture = ci; 125 126 127} 128 129 131 # endregion130 132 133 134 135 136}View Code

 
3. Text to be internationalized stored in js

Var js_language = ""; js_language = $. cookie ('js _ language'); var Logout = new Array (); Logout ["zh-CN"] = "are you sure you want to exit the system? "; Logout [" en-US "] =" Are you sure to exit? ";View Code
Aspx page Internationalization

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

 

International System Menu

After you select a language on the logon page, save the language option to the Session. when loading the menu on the home page, you can read the text displayed in the corresponding menu according to the option. The text needed on other pages is also the same. Sometimes the international switch function needs to be added on the home page. When switching on the home page, the Session ID needs to be updated, that is, the Session ["Language"] defined in the BasePage.

 

Internationalization of text in js files

When talking about the internationalization of the text in the js file, it is not a small amount of physical activity. If you write the js script in the aspx page, you can access it by page access, if it is a separate js file, it requires labor for migrant workers. Therefore, the js file mentioned in the preparation work is used. Each phrase to be translated defines an array. The array contains the corresponding language and the corresponding translation text. A Cookie is also used here, you can select a language based on the Cookie value during use.

 

Server Control text Internationalization

This is strange to me. It was only found today. Why did I report an error when I directly wrote <%> in the Text of the server control? If there is no way, you can use a stupid method to translate it based on Session ["Language"] in the Page_Init function.

 

Summary

First, internationalization is generally a pure physical activity. You need to consider resource files, aspx pages, and js files respectively. I didn't care at first. I thought it was enough to prepare the resource file. After four major changes, I found that these details should be taken seriously, during my second major change, I used Session and Cookie in the background. After I switched the language, I found that the Cookie stored value could not be found after the page was crossed, you must know that Cookie exists across pages. Once again, it is depressing, so the third and fourth major changes have basically been completed.

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.