In my previous essay, "Fast multilingual processing in WinForm applications", the multi-language processing solution in WinForm development is introduced, the whole multi-language solution is driven by the actual demand, to reduce code churn, efficient processing for the purpose of reducing code modification through the way of base class inheritance, By introducing a translation API approach to reduce translation processing time, this essay continues to deepen the presentation of this multi-lingual processing scheme, meaning that integration is seamlessly used in the development framework.
1, multi-lingual processing step 1) Extract the multi-language processing JSON file
We can put the program interface or the code inside the Chinese extract, into the JSON file, the different modules may be divided into different JSON files, as shown below.
Then use the interface call of the helper class to translate the English content, as shown in the following code:
// interface of Chinese new System.Globalization.CultureInfo ("en-us"); // English Interface // If the language pack has blank translation content, you can call the following statement (Baidu translation) to obtain the translation content, reduce the tedious manual translation // after translation, the content will be reviewed or adjusted for use // The official release program needs to block this call JsonLanguage.Default.Translate ();
The content can be translated and adjusted well.
These JSON resource files, according to the program run relative directory lang/en-us/abc.json directory, can be separated according to different modules or requirements of different files, the program load multilingual information, will be loaded.
2) Inherit form base class and use uniform information hint helper class
After preparing a lot of language files, then the processing of multiple languages requires some auxiliary processing to be implemented, because we have put the logic of multi-language processing in the form of the base class, such as the BaseForm form base class, if the original form is inherited from the base class, So the multi-language processing logic will be processed automatically after the form is loaded.
Through the processing of inheritance relationships, we can automatically have multi-lingual processing without having to modify any of the subform's code, and when the subform is loaded, it automatically iterates through the internal controls to implement the multi-language processing logic.
If we have some forms that are not easy to integrate with these base classes, you can also handle them in the form of load or shown events with the addition of code, as shown in the following code.
Private void Form1_shown (object sender, EventArgs e) { // the form is loaded and displayed, and the form is implemented in multi-lingual processing if (! This . DesignMode) { languagehelper.initlanguage (this); } }
There is also a special interface, which is a hint of some information, such as the message box prompt, this default processing method, such as the following is the general message prompt code.
Xtramessagebox.show (" Welcome to the WinForm Development framework "" prompt information ", MessageBoxButtons.OK, MessageBoxIcon.Information);
We are in the framework is also through the helper class encapsulation way to prompt, so the multi-lingual only need to modify this helper class, do not have to modify our form inside the completed code.
Messagedxutil.showtips (" Welcome to the WinForm Development framework ");
Since we use message prompt processing in the framework, the processing of multiple languages can be implemented internally, as shown below.
/// <summary> ///Show General Tips/// </summary> /// <param name= "message" >Prompt Information</param> /// <param name= "args" >the contents of the arguments inside the string</param> /// <returns></returns> Public StaticDialogResult Showtips (stringMessageparams Object[] args) { //Multi-lingual processing of the contents of a message message = JsonLanguage.Default.GetString (message); if(Args! =NULL) {Message=string. Format (message, args); } returnDevExpress.XtraEditors.XtraMessageBox.Show (Message, caption_tips, MessageBoxButtons.OK, MessageBoxIcon.Information); }
If it is a multi-lingual processing with some parameter placeholders, we can implement it as well.
Messagedxutil.showtips (" user" {0} "authenticated correctly ", LoginName);
Through the form base class BaseForm internal logic modification, as well as some auxiliary classes such as Messagedxutil's modification, we quietly realized the multi-language processing logic, the original code or does not have the modification to support the multi-language processing, greatly saves the workload.
Of course, if it is a special case, we can still get its content through a unified multi-language processing logic, as shown in the following code.
JsonLanguage.Default.GetString (message);
3. Modular Multi-lingual support
In addition to some of the general logic described above, in the WinForm development process, we often use a modular development approach to improve development efficiency, such as using the framework of common class library, basic interface module, paging control module, dictionary module, Rights Management system, etc., these modules are part of the entire modular process, Of course, multi-lingual support is also necessary, because we have the base class and redundant and module independent way to simplify it, but the inside of the multilingual resources we should also be provided in a modular way, translated and integrated in the system can be used.
The above Basic.json is the framework of the entire base of the interface (such as paging, dictionaries, etc.) English reference resources, Security.json for the rights system interface resources, Commons.json for some other modules of the multilingual resources and so on.
and the multi-language reference resource of the permission system
For example, the display interface of pagination control, our English version of the effect is as follows (for example, user management interface)
The editing interface effect is as follows
The overall interface effect of the rights management system is as follows, including toolbars, menus, tabs, and all the controls inside, and the test is OK.
Of course, the data source inside must be Chinese, unless all of our system data is entered in English context.
Rapid implementation of multilingual processing in WinForm applications (2)--integration of development framework Modules