[Serialization] Design and Implementation of C # communication (Serial Port and network) framework-13. Design of switching between Chinese and English versions,

Source: Internet
Author: User

[Serialization] Design and Implementation of C # communication (Serial Port and network) framework-13. Design of switching between Chinese and English versions,

Contents

Chapter 2 switch between Chinese and English versions... 2

13.1 reasons for not using built-in resource files... 2

13.2 configuration file... 2

13.3 language management... 3

13.4 application management... 12

13.5 summary... 12

 

Chapter 2 switch between Chinese and English versions design 13th reasons for not using built-in resource files

You can use the resx resource file for multilingual design. The resx file itself is a kv type resource file. After the resource file is designed, you can set the language to be displayed through the CurrentCulture attribute when starting the software. The implementation code is as follows:

// Set it to the English version

Thread. CurrentThread. CurrentCulture = CultureInfo. GetCultureInfo ("en-us ");

However, when the software involves applications such as multithreading, thread pool, and Asynchronization, the current thread sets the English version, and other threads are still the default language culture. For example, the main thread sets en-US, however, the newly created thread and other existing threads are still zh-CN. If the UI of each part is not updated in the same thread, the Language and Culture settings are different, therefore, there is no way to achieve unified language display.

So, is it a good idea to obtain all thread information through the process and set language and culture information in a unified manner. However, it turns out that this operation is not feasible, and the anti-counterfeiting software may exit unexpectedly. Why does this happen? I guess a process contains not only custom threads but also system-level threads. This operation is dangerous.

Is there no way to implement it? It is impossible for a person to be suffocated. In. NET 4.5, the DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture attributes of the CultureInfo type in the System. Globalization namespace are much simpler. After setting, the CurrentUICulture and CurrentCulture attributes of each new thread will be consistent with this value. How to Implement the CultureInfo class has not been studied.

To be compatible with the XP operating system, the. NET4.0 framework is still in use. I believe that it can also implement the functions of the CultureInfo class, but it is better to design a language version scheme that is more direct and time-saving. If you have time, you can study the implementation of the CultureInfo class.

13.2 configuration file

First, design the language configuration file. The file format is XML, and the storage mode is KV. The file name can be customized, such as cn. xml and en. xml. For example:

 

Key can be defined in two ways. The first method is form naming. Control naming, which allows you to change the Display language information for the form controls. Type 2: Define keywords directly. You can change the language information displayed for individual entries such as prompt information and status information. Value is the specific content of the language to be displayed. It is completely customized.

13.3 language Management
[Serializable] the cascade ID of the public class CultureItem {/// <summary> // control ". "separator /// </summary> [XmlAttribute] public string Key {set; get ;} /// <summary> /// Chinese or English description /// </summary> [XmlAttribute] public string Value {set; get ;}}
Public enum CultureLanguage {[EnumDescription ("Chinese")] Chinese, [EnumDescription ("English")] English}
Public class CultureMananger {private static Dictionary <string, string> _ dic = new Dictionary <string, string> (); private static string _ cnPath = Application. startupPath + "\ SuperIO \ Language \ cn. xml "; private static string _ enPath = Application. startupPath + "\ SuperIO \ Language \ en. xml "; private static object SyncObject = new object (); // <summary> // load the language file to the cache /// </summary> public static voi D LoadCulture () {lock (SyncObject) {if (IsLanguage) {try {_ dic. clear (); string path = String. empty; if (Language = CultureLanguage. chinese) {path = _ cnPath;} else if (Language = CultureLanguage. english) {path = _ enPath;} if (File. exists (path) {List <CultureItem> itemList = SerializeOperation. serializeOperation. getSerialize <List <CultureItem> (path); foreach (CultureItem item in itemList) {_ Dic. add (item. key, item. value) ;}} catch (Exception ex) {GeneralLog. writeLog (ex) ;}}/// <summary> // clear the language information in the cache /// </summary> public static void sort AchE () {lock (SyncObject) {_ dic. clear () ;}/// <summary> // set and obtain the Language type attributes /// </summary> public static CultureLanguage Language {set {if (GlobalProperty. getInstance (). language! = Value) {GlobalProperty. getInstance (). language = value; GlobalProperty. getInstance (). save (); LoadCulture () ;}} get {return GlobalProperty. getInstance (). language ;}} /// <summary> /// obtain the description corresponding to the entry // </summary> /// <param name = "formName"> form name </param>/ // <param name = "field"> description of the entry field </param> // <returns> </returns> public static string GetString (string formName, string field) {return GetStri Ng (String. format ("{0 }. {1} ", formName, field ));} /// <summary> /// obtain the description corresponding to the entry. /// </summary> /// <param name = "key"> Field keyword </param> /// <returns> </returns> public static string GetString (string key) {lock (SyncObject) {if (IsLanguage) {string val = String. empty; if (_ dic. containsKey (key) {_ dic. tryGetValue (key, out val);} return val;} else {return String. empty ;}}/// <summary> // modify the application form Variable language display // </summary> // <param name = "frm"> </param> public static void ApplyResourcesForm (Form frm) {if (IsLanguage) {string frmText = GetString (frm. name); if (! String. isNullOrEmpty (frmText) {frm. text = frmText;} ApplyControls (frm. name, frm. controls) ;}/// <summary> // use the BarManager tool, change the language display /// </summary> /// <param name = "name"> </param> /// <param name = "bar"> </param> public static void AppResourceBarItem (string name, barManager bar) {if (IsLanguage) {string key = String. empty; foreach (BarItem item in bar. items) {key = String. format ("{0 }. {1} ", n Ame, item. Name); string val = GetString (key); if (! String. isNullOrEmpty (val) {item. caption = val ;}}}/// <summary> // apply the control, change the language display /// summary> /// <param name = "name"> </param> /// <param name = "ctrls"> </param> public static void ApplyControls (string name, control. controlCollection ctrls) {if (IsLanguage) {foreach (Control ctrl in ctrls) {if (ctrl is MenuStrip) // MenuStrip StatusStrip {ApplyMenuStrip (name, (MenuStrip) ctrl );} else if (ct Rl is StatusStrip) {ApplyStatusStrip (name, (StatusStrip) ctrl);} else if (ctrl is ListView) {ApplyListView (name, (ListView) ctrl );} else {ApplyControls (name, ctrl);} if (ctrl. hasChildren) {ApplyControls (name, ctrl. controls) ;}}} internal static bool IsLanguage {get {if (File. exists (_ cnPath) & File. exists (_ enPath) {return true;} else {return false ;}} private static void ApplyC Ontrols (string name, Control ctrl) {string key = String. format ("{0 }. {1} ", name, ctrl. name); string text = GetString (key); if (! String. isNullOrEmpty (text) {ctrl. text = text ;}} private static void ApplyMenuStrip (string name, MenuStrip menu) {foreach (ToolStripMenuItem item in menu. items) {ApplyMenuItem (name, item) ;}} private static void ApplyMenuItem (string name, ToolStripMenuItem item) {string key = String. format ("{0 }. {1} ", name, item. name); string text = GetString (key); if (! String. isNullOrEmpty (text) {item. text = text;} if (item. dropDownItems. count> 0) {foreach (ToolStripMenuItem subItem in item. dropDownItems) {ApplyMenuItem (name, subItem) ;}} private static void ApplyStatusStrip (string name, StatusStrip status) {string key = String. empty; foreach (ToolStripItem item in status. items) {key = String. format ("{0 }. {1} ", name, item. name); string val = GetString (ke Y); if (! String. isNullOrEmpty (val) {item. text = val ;}} private static void ApplyListView (string name, ListView lv) {string key = String. empty; foreach (ColumnHeader header in lv. columns) {key = String. format ("{0 }. {1} ", name, header. tag = null? "": Header. Tag. ToString (); string val = GetString (key); if (! String. IsNullOrEmpty (val) {header. Text = val ;}}}}

 

13.4 Application Management

When the software is started, you can use the CultureMananger management class. The specific application code is as follows:

CultureMananger.LoadCulture();CultureMananger.ApplyControls("MainForm",this.Controls);string state=CultureMananger.GetString("State.Normal");
Conclusion 13.5

This is a small tool component with a certain degree of versatility.

 

 

Author: Wei Xiaozhi

Email: 504547114@qq.com

QQ: 504547114

. NET Development Technology Alliance: 54256083

Document Download: http://pan.baidu.com/s/1pJ7lZWf

Http://www.bmpj.net

Related Article

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.