C#Learning Notes-English Switching (XML)

These days, because the software needs to be in English, I have searched for a long time and found relevant information. Source reference: http://blog...

These days, because the software needs to be in English, I have searched for a long time and found relevant information. Source reference: http://blog.csdn.net/softimite_zifeng

There are two ways to switch between Chinese and English for web search: 1. Load resource files dynamically 2. Make XML to save the control name and content, and finally choose to learn using XML.

XML Preparations

Start with some XML preparations: https://www.ibm.com/developerworks/cn/xml/x-newxml/#list1

XML is an abbreviation for Extensible Markup Language and is used to store data only.All elements must have closing tags.

The first line of an XML document can be an XML declaration.This is an optional part of the file, which recognizes the file as an XML file and helps tools and humans recognize the XML (it is not mistaken for SGML or other tags).You can simply write this declaration as <?Xml?>, or include an XML version (<?Xml version="1.0"?>), or even a character encoding, such as <?Xml version="1.0" encoding="utf-8"?> for Unicode.

1. The XML document must have a root element

An XML document must have one element that is the parent of all other elements.This element is called the root element.

The start and end tags of the root element surround the content of the XML document.A file can only have one root element, and it needs to be included in a wrapper.Listing 1 shows a truncated example where the root element is named <recipe>.

Listing 1. Root element

<?xml version="1.0" encoding="UTF-8"?> <recipe> </recipe>

When building a document, content and other tags must be placed between <recipe>and </recipe>

2. Tags are capitalized

When creating XML, make sure that the case of the start and end tags is the same.If the case is inconsistent, errors will occur when using or viewing XML.For example, if the case is inconsistent, Internet Explorer will not be able to display the contents of the file, but it will display messages with inconsistent start and end tags.

3. Nested Elements

Nesting places an element inside another element.These new elements are called child elements and the elements that contain them are called parent elements.

4. Add Attributes

There are times when attributes are added to an element.Attributes consist of a name-value pair with values enclosed in double quotes ('), such as type='dessert'.Attributes are a way to store additional information when using elements.In the same document, different attribute values can be applied to different instances of each element as needed.

You can enter one or more attributes inside the element's start tag, such as: <recipe type="dessert">.If you want to add multiple attributes, separate the attributes with spaces, such as: <recipename cuisine="american" services="1">.

5. Comments in XML

The syntax for writing comments in XML is very similar to that in HTML: <!-- This is a comment -->
In XML, spaces are retained in the XML and are not truncated in the document.

6.XML Naming Rules

XML elements must follow the following naming rules:

    • Names can contain letters, numbers, and other characters
    • Names cannot start with numbers or punctuation
    • Names cannot start with the character "xml" (or XML, Xml)
    • Name cannot contain spaces

You can use any name without reserving words.

7. Well-formed XML documents

Well Formed XML documents follow the XML grammar rules described in previous chapters:

    • XML document must have a root element
    • XML documents must have closing tags
    • XML tags are case sensitive
    • XML elements must be properly nested
    • XML attributes must be quoted
Specific implementation of English-Chinese switching

There are three interfaces: login interface, password modification interface and main interface.Each interface has a Chinese XML and an English XML.

Chinese XML for login interface:

1 <?xml version="1.0" encoding="utf-8" ?> 2 <Softimite Language="Simplified Chinese"> 3 <Form> 4 <!--Login interface--> 5 <Name>LoginForm</Name> 6 <Controls> 7 <Controls name="LoginForm" Text="Sign in" /> 8 <Control name="accountLab" text="Account number"/> 9 <Control name="passwordLab" text="Password"/> 10 <Control name="loginBtn" text="Sign in"/> 11 <Control name="passwordBtn" text="Change Password"/> 12 <Control name="label_eg" text="Chinese"/> 13 </Controls> 14 </Form> 15 </Softimite>

English XML for the main interface:

1 <?xml version="1.0" encoding="utf-8" ?> 2 <Softimite Language="Simplified Chinese"> 3 <Form> 4 <!--Main interface--> 5 <Name>MainForm</Name> 6 <Controls> 7 <Control name="MainForm" text="User-xxx"/> 8 <Control name="button1" text="Project"/> 9 <Control name="button2" text="Active Issues"/> 10 <Control name="button3" text="All Issues"/> 11 <Control name="button4" text="Issues statistics"/> 12 <Control name="button5" text="To-Do List"/> 13 <Control name="button6" text="Assign To Me"/> 14 <Control name="button7" text="My Assignment"/> 15 <Control name="button8" text="Notifications"/> 16 <Control name="button9" text="Version Management"/> 17 <Control name="xtraTabPage1" text="Project"/> 18 <Control name="xtraTabPage2" text="Active Issues"/> 19 <Control name="xtraTabPage3" text="All Issues"/> 20 <Control name="xtraTabPage4" text="Issues Statistics"/> 21 <Control name="xtraTabPage5" text="To-Do list"/> 22 <Control name="xtraTabPage6" text="Assign to me"/> 23 <Control name="xtraTabPage7" text="My Assignment"/> 24 <Control name="xtraTabPage8" text="Notifications"/> 25 <Control name="xtraTabPage9" text="Version Management"/> 26 </Controls> 27 <DataGridViewCells> 28 <DataGridViewCell name="Column1" HeaderText="Number"/> 29 <DataGridViewCell name="Column2" HeaderText="Type"/> 30 <DataGridViewCell name="Column3" HeaderText="Title"/> 31 <DataGridViewCell name="Column4" HeaderText="State"/> 32 <DataGridViewCell name="Column5" HeaderText="Priority"/> 33 <DataGridViewCell name="Column6" HeaderText="Assign"/> 34 <DataGridViewCell name="Column7" HeaderText="Planned Date"/> 35 <DataGridViewCell name="Column8" HeaderText="Deadline"/> 36 <DataGridViewCell name="Column9" HeaderText="Modification Times"/> 37 <DataGridViewCell name="dataGridViewTextBoxColumn1" HeaderText="Number"/> 38 <DataGridViewCell name="dataGridViewTextBoxColumn2" HeaderText="Type"/> 39 <DataGridViewCell name="dataGridViewTextBoxColumn3" HeaderText="Title"/> 40 <DataGridViewCell name="dataGridViewTextBoxColumn4" HeaderText="State"/> 41 <DataGridViewCell name="dataGridViewTextBoxColumn5" HeaderText="Priority"/> 42 <DataGridViewCell name="dataGridViewTextBoxColumn6" HeaderText="Assign"/> 43 <DataGridViewCell name="dataGridViewTextBoxColumn7" HeaderText="Handler"/> 44 <DataGridViewCell name="dataGridViewTextBoxColumn8" HeaderText="Assigner"/> 45 <DataGridViewCell name="dataGridViewTextBoxColumn9" HeaderText="Creation Time"/> 46 <DataGridViewCell name="Column10" HeaderText="Modification Times"/> 47 </DataGridViewCells> 48 </Form> 49 </Softimite>

MultiLanguage code:

1 static class MultiLanguage 2 { 3 //Current default language 4 public static string DefaultLanguage = "ChineseSimplified"; 5 6 /// <summary> 7 /// Read the current default language 8 /// </summary> 9 /// <returns>Current default language</returns> 10 public static string GetDefaultLanguage() 11 { 12 string defaultLanguage = "ChineseSimplified"; 13 XmlReader reader = new XmlTextReader("Languages/DefaultLanguage.xml"); 14 XmlDocument doc = new XmlDocument(); 15 doc.Load(reader); 16 XmlNode root = doc.DocumentElement; 17 //Selection DefaultLangugae node 18 XmlNode node = root.SelectSingleNode("DefaultLanguage"); 19 if (node != null) 20 { 21 //Remove content from node 22 defaultLanguage = node.InnerText; 23 } 24 reader.Close(); 25 reader.Dispose(); 26 return defaultLanguage; 27 } 28 29 /// <summary> 30 /// Modify Default Language 31 /// </summary> 32 /// <param name="lang">Default language to set</param> 33 public static void SetDefaultLanguage(string lang) 34 { 35 DataSet ds = new DataSet(); 36 ds.ReadXml("Languages/DefaultLanguage.xml"); 37 DataTable dt = ds.Tables["Softimite"]; 38 dt.Rows[0]["DefaultLanguage"] = lang; 39 ds.AcceptChanges(); 40 ds.WriteXml("Languages/DefaultLanguage.xml"); 41 DefaultLanguage = lang; 42 } 43 44 /// <summary> 45 /// Load Language 46 /// </summary> 47 /// <param name="form">Load Language Window</param> 48 public static void LoadLanguage(Form form, string FormName) 49 { 50 //Get the display text of the table based on the language selected by the user 51 Hashtable hashText = ReadXMLText(form.Name, FormName); 52 Hashtable hashHeaderText = ReadXMLHeaderText(form.Name, FormName); 53 if (hashText == null) 54 { 55 return; 56 } 57 //Get all controls of the current window 58 Control.ControlCollection sonControls = form.Controls; 59 try 60 { 61 //Traverse all controls 62 foreach (Control control in sonControls) 63 { 64 if (control.GetType() == typeof(Panel)) //Panel 65 { 66 GetSetSubControls(control.Controls, hashText, hashHeaderText); 67 } 68 else if (control.GetType() == typeof(GroupBox)) //GroupBox 69 { 70 GetSetSubControls(control.Controls, hashText, hashHeaderText); 71 } 72 else if (control.GetType() == typeof(TabControl)) //TabControl 73 { 74 GetSetSubControls(control.Controls, hashText, hashHeaderText); 75 } 76 else if (control.GetType() == typeof(TabPage)) //TabPage 77 { 78 GetSetSubControls(control.Controls, hashText, hashHeaderText); 79 } 80 else if (control.GetType() == typeof(DataGridView)) //DataGridView 81 { 82 GetSetHeaderCell((DataGridView)control, hashHeaderText); 83 } 84 if (hashText.Contains(control.Name.ToLower())) 85 { 86 control.Text = (string)hashText[control.Name.ToLower()]; 87 } 88 } 89 //If a control is found, its name is assigned to the past 90 if (hashText.Contains(form.Name.ToLower())) 91 { 92 form.Text = (string)hashText[form.Name.ToLower()]; 93 } 94 } 95 catch (Exception ex) 96 { 97 string s = ex.ToString(); 98 } 99 } 100 101 102 /// <summary> 103 /// Gets and sets the child control in the control 104 /// </summary> 105 /// <param name="controls">Parent Control</param> 106 /// <param name="hashResult">Hashtable</param> 107 private static void GetSetSubControls(Control.ControlCollection controls, Hashtable hashText, Hashtable hashHeaderText) 108 { 109 try 110 { 111 foreach (Control control in controls) 112 { 113 if (control.GetType() == typeof(Panel)) //Panel 114 { 115 GetSetSubControls(control.Controls, hashText, hashHeaderText); 116 } 117 else if (control.GetType() == typeof(GroupBox)) //GroupBox 118 { 119 GetSetSubControls(control.Controls, hashText, hashHeaderText); 120 } 121 else if (control.GetType() == typeof(TabControl)) //TabControl 122 { 123 GetSetSubControls(control.Controls, hashText, hashHeaderText); 124 } 125 else if (control.GetType() == typeof(TabPage)) //TabPage 126 { 127 GetSetSubControls(control.Controls, hashText, hashHeaderText); 128 } 129 else if (control.GetType() == typeof(TableLayoutPanel)) //TableLayoutPanel 130 { 131 GetSetSubControls(control.Controls, hashText, hashHeaderText); 132 } 133 else if (control.GetType() == typeof(DataGridView)) 134 { 135 GetSetHeaderCell((DataGridView)control, hashHeaderText); 136 } 137 else if (control.GetType() == typeof(XtraTabControl)) 138 { 139 GetSetSubControls(control.Controls, hashText, hashHeaderText); 140 } 141 else if (control.GetType() == typeof(XtraTabPage)) 142 { 143 GetSetSubControls(control.Controls, hashText, hashHeaderText); 144 } 145 if (hashText.Contains(control.Name.ToLower())) 146 { 147 control.Text = (string)hashText[control.Name.ToLower()]; 148 } 149 } 150 } 151 catch (Exception ex) 152 { 153 throw new Exception(ex.Message); 154 } 155 } 156 157 /// <summary> 158 /// from XML Read from file needs modification Text Contents 159 /// </summary> 160 /// <param name="frmName">Window name, used to get that part of the corresponding window</param> 161 /// <param name="xmlName">target language</param> 162 /// <returns></returns> 163 private static Hashtable ReadXMLText(string frmName, string xmlName) 164 { 165 try 166 { 167 Hashtable hashResult = new Hashtable(); 168 XmlReader reader = null; 169 //Determine if a configuration file exists for the language 170 if (!(new System.IO.FileInfo("Languages/" + xmlName + ".xml")).Exists) 171 { 172 return null; 173 } 174 else 175 { 176 reader = new XmlTextReader("Languages/" + xmlName + ".xml"); 177 } 178 XmlDocument doc = new XmlDocument(); 179 doc.Load(reader); 180 XmlNode root = doc.DocumentElement; 181 //Obtain XML The contents of the corresponding window in the file 182 XmlNodeList nodeList = root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control"); 183 foreach (XmlNode node in nodeList) 184 { 185 try 186 { 187 //Modify as control's Text value 188 XmlNode node1 = node.SelectSingleNode("@name"); 189 XmlNode node2 = node.SelectSingleNode("@text"); 190 if (node1 != null) 191 { 192 hashResult.Add(node1.InnerText.ToLower(), node2.InnerText); 193 } 194 } 195 catch (Exception ex) 196 { 197 string s = ex.ToString(); 198 } 199 } 200 reader.Close(); 201 reader.Dispose(); 202 return hashResult; 203 } 204 catch 205 { 206 return null; 207 } 208 } 209 210 /// <summary> 211 /// from XML Read from file needs modification HeaderText Contents 212 /// </summary> 213 /// <param name="frmName">Window name, used to get that part of the corresponding window</param> 214 /// <param name="xmlName">target language</param> 215 /// <returns></returns> 216 private static Hashtable ReadXMLHeaderText(string frmName, string xmlName) 217 { 218 try 219 { 220 Hashtable hashResult = new Hashtable(); 221 XmlReader reader = null; 222 //Determine if a configuration file exists for the language 223 if (!(new System.IO.FileInfo("Languages/" + xmlName + ".xml")).Exists) 224 { 225 return null; 226 } 227 else 228 { 229 reader = new XmlTextReader("Languages/" + xmlName + ".xml"); 230 } 231 XmlDocument doc = new XmlDocument(); 232 doc.Load(reader); 233 XmlNode root = doc.DocumentElement; 234 //Obtain XML The contents of the corresponding window in the file 235 XmlNodeList nodeList = root.SelectNodes("Form[Name='" + frmName + "']/DataGridViewCells/DataGridViewCell"); 236 foreach (XmlNode node in nodeList) 237 { 238 try 239 { 240 //Modify as control's Text value 241 XmlNode node1 = node.SelectSingleNode("@name"); 242 XmlNode node2 = node.SelectSingleNode("@HeaderText"); 243 if (node1 != null) 244 { 245 hashResult.Add(node1.InnerText.ToLower(), node2.InnerText); 246 } 247 } 248 catch { } 249 } 250 reader.Close(); 251 reader.Dispose(); 252 return hashResult; 253 } 254 catch 255 { 256 return null; 257 } 258 } 259 260 /// <summary> 261 /// Get and set DataGridView Column header of 262 /// </summary> 263 /// <param name="dataGridView">DataGridView name</param> 264 /// <param name="hashResult">Hashtable</param> 265 private static void GetSetHeaderCell(DataGridView dataGridView, Hashtable hashHeaderText) 266 { 267 foreach (DataGridViewColumn column in dataGridView.Columns) 268 { 269 if (hashHeaderText.Contains(column.Name.ToLower())) 270 { 271 column.HeaderText = (string)hashHeaderText[column.Name.ToLower()]; 272 } 273 } 274 } 275 276 277 }

Flowchart of the code above:

Effect Chart:

10 July 2019, 12:58 | Views: 1971

Add new comment

For adding a comment, please log in
or create account

0 comments