General idea:
Json file series stored in the Dictionary, and then by traversing the specified control and its child controls matched replacement,
note: to your specific needs (such as: only a partial translation of the String), and individual controls (such as: ToolStrip) need to add processing method, the subsequent will explain in detail how to traverse the special controls,
Specific implementation and presentation:
1, preparation:
1.1 add Json configuration, there are many online about Json configuration related articles do not detail
C # using json
Click tools - NuGet package manager - package management console, enter the following command:
Install - Package Newtonsoft. Json
Can use Newtonsoft. Json packages for Json operation
PM> Install - Package Newtonsoft. Json
1.2 the corresponding Json file editor: key translated into the value
English translation of the Json configuration
{
"Test" : "Test",
"Language", "Language",
"Set", "Settings",
"Chinese (default)" : "(the default) in Chinese,"
"English", "English"
}
Chinese-english translation Json configuration
{
"Test", "Test",
"Language", "Language",
"Settings", "Set",
(the default), "Chinese", "Chinese" (the default),
"English", "English",
"English", "English"
}
1.3 put all sorts of commonly used controls to the form and the corresponding text edit as shown in figure 1
:
2, custom translation class library
Using Newtonsoft. Json;//Json serialization
Using System. Windows. Forms;//control traverse the
Using System. Text. RegularExpressions;//the String handling
The namespace Interpret
{
Public class InterpretBase
{
//the code below
}
}
2.1 the existing resource loading method
//define the dictionary used to store Json resource configuration file
The static DictionaryResources=new Dictionary (a);
///& lt; Summary>
///the current project folder Debug \ Language \ parameter folder
///& lt;/summary>
///& lt; Param name="language" & gt; Configuration files in the folder name & lt;/param>
Public static void LoadLanguage (string language="")
{
If (string. IsNullOrEmpty (language))
{
Language=System. The Threading. Thread. CurrentThread. CurrentUICulture. Name;
}
Resources=new Dictionary(a);
String dir=Path.Com bine (AppDomain. CurrentDomain. BaseDirectory, the string. Format (" Language/{0} ", Language));
If (Directory. The Exists (dir))
{
Var jaonFile=Directory. GetFiles (dir, "*. Json," SearchOption. AllDirectories);
The foreach (string file in jaonFile)
{
LoadFile (file);
}
}
}
///& lt; Summary>
///loading configuration file
///& lt;/summary>
///& lt; Param name="path" & gt; The absolute path to the configuration file (including the file itself) & lt;/param>
Public static void LoadFile (string path)
{
Var content=File. ReadAllText (path, Encoding UTF8);
if (! String. IsNullOrEmpty (content))
{
Var dict=JsonConvert. DeserializeObject(content);
Foreach (string key in dict. Keys)
{
if (! Resources. Either ContainsKey (key))
{
Resources. The Add (key, dict [key]);
}
The else
The resources [key]=dict [key];
}
}
}
2.2 normal controls traversal ( because involves other related is not traverse the list controls, here by Combox traversal, is the same as the other controls thought
///& lt; Summary>
///traverse translation forms or controls and its child controls
///& lt;/summary>
///& lt; Param name="control" & gt; The control or form need to be translated & lt;/param>
Public static void InitLanguage Control (Control)
{
SetControlLanguage (control);
Foreach (Control CTRL in Control. Controls)
{
InitLanguage (CTRL);
}
//a toolbar or menu dynamically build forms or controls, the pair to control for processing
Control. ControlAdded +=(sender, e)=& gt;
{
InitLanguage (e.c. with our fabrication: ontrol);
};
}
///& lt; Summary>
///controls and child controls translation
///& lt;/summary>
///& lt; Param name="control" & gt; Need to be translated controls & lt;/param>
Public static void SetControlLanguage Control (Control)
{
If (control is ComboBox)
{
ComboBox combox=control as ComboBox.
String [] NewItems=new string [combox. Items. The Count];
For (int I=0; I & lt; Combox. Items. The Count; I++)
{
If (resources. Either ContainsKey (combox. Items [I] the ToString ()))
{
NewItems [I]=resources [combox Items [I] the ToString ()];
}
The else
NewItems. [I]=combox Items [I] the ToString ();
}
Combox. Text=(resources. Either ContainsKey (combox. Text))? The resources [combox Text] : combox. Text;
Combox. Items. The Clear ();
Combox. Items. AddRange (NewItems);
}
//control is other controls or special controls such as: the TreeView
Else if (control is TreeView)
{
//TreeView TreeView=control as the TreeView.
//if (treeView Nodes!=null)
//{
//TreeViewNodes (treeView. Nodes);
//}
}
The else
{
Control. The Text=(resources. Either ContainsKey (control. The Text))? Resources [control. The Text] : control. The Text;
}
}
2.3 special controls traverse the train of thought (idea basic can meet most of the control, because the details relating to the other can't complete the upload apologize)
Special controls the traversal ideas like normal controls traversal is actually only need according to the special control different situation to traverse the special attributes of judging translation line (