Simple implementation of winForm Internationalization

Source: Internet
Author: User

To be internationalized, you must switch the multilingual interface. First, we cannot use the if else or other statements to set values for all controls one by one based on different languages. This is too troublesome .. Net provides support for internationalization, mainly in the System. Globalization namespace. The following is a simple implementation, which is small and dirty.
This program is a recently written program. On this basis, the key is to understand the principle.
1> first, add the Resource file.
Add three resource files to the VS project --> Add a new project. The Resource file is used to store the display strings of our interface control. Here we want to implement the switching of 3 languages, all added 3, named respectively: Resource. en-US.resx, Resource. zh-CN.resx, Resource. ja-JP.resx. Note that the naming rules must be the same in the first part. The second part is the culture name of different languages. I define English, Chinese, and Japanese.
2> define the interface-related string information in the three files.
Note that the defined names must be consistent among the three files; otherwise, they cannot be found.
Resource. en-US.resx
Tsmi_language_Name Language
Tsmi_japanese_Name Japanese
Tsmi_english_Name English
Tsmi_close_Name Close
Tsmi_chiniese_Name Chinese

Resource. zh-CN.resx
Tsmi_language_Name
Tsmi_chiniese_Name Chinese
Tsmi_english_Name
Tsmi_japanese_Name Japanese
Tsmi_close_Name exit

Resource. ja-JP.resx
Tsmi_assistage_name statement
Tsmi_chiniese_Name China Region
Tsmi_english_Name English speaker
Tsmi_japanese_Name Japanese Region
Tsmi_close_Name already exist
3> use the ResourceManager class to read data.
ResourceManager reads different resource files based on different cultrue values. Therefore, the key to implementation is to change the cultrue value of the current program thread based on different languages. The value assignment code of the interface control Text will remain unchanged. When a new language is added, that is, a resource file is added, there will be almost no impact on the code changes.
Basic code:
Resource Reading Class


Class ResourceCluture
{
/// <Summary>
/// Set the language environment
/// </Summary>
/// <Param name = "strClutrue"> </param>
Public static void SetLocalClutrue (string strClutrue)
{
If (string. IsNullOrEmpty (strClutrue ))
{
StrClutrue = "zh-CN ";
}
CultureInfo currentClutrue = new CultureInfo (strClutrue );
Thread. CurrentThread. CurrentCulture = currentClutrue;
}
 
/// <Summary>
/// Value
/// </Summary>
/// <Param name = "id"> </param>
/// <Returns> </returns>
Public static string GetString (string id)
{
String strValue = string. Empty;
Try
{
ResourceManager resManager = new ResourceManager ("ApplicationActive. Properties. Resource", Assembly. GetExecutingAssembly ());
StrValue = resManager. GetString (id, Thread. CurrentThread. CurrentCulture );
}
Catch
{
StrValue = "No id:" + id + "please add ";
}
Return strValue;
}
}
Class ResourceCluture
{
/// <Summary>
/// Set the language environment
/// </Summary>
/// <Param name = "strClutrue"> </param>
Public static void SetLocalClutrue (string strClutrue)
{
If (string. IsNullOrEmpty (strClutrue ))
{
StrClutrue = "zh-CN ";
}
CultureInfo currentClutrue = new CultureInfo (strClutrue );
Thread. CurrentThread. CurrentCulture = currentClutrue;
}

/// <Summary>
/// Value
/// </Summary>
/// <Param name = "id"> </param>
/// <Returns> </returns>
Public static string GetString (string id)
{
String strValue = string. Empty;
Try
{
ResourceManager resManager = new ResourceManager ("ApplicationActive. Properties. Resource", Assembly. GetExecutingAssembly ());
StrValue = resManager. GetString (id, Thread. CurrentThread. CurrentCulture );
}
Catch
{
StrValue = "No id:" + id + "please add ";
}
Return strValue;
}
}

Code for setting the control text:

/// <Summary>
/// Set the text of the Interface Control Based on Different Languages and Cultures
/// </Summary>
/// <Param name = "strClutrue"> "en-US", "ch-CN", "ja-JP" etc .. </param>
Private void SetFormTextByLanguage (string strClutrue)
{
ResourceCluture. SetLocalClutrue (strClutrue );
This. lblPath. Text = ResourceCluture. GetString ("lblPath_Name ");
This. tsmi_language.Text = ResourceCluture. GetString ("tsmi_language_Name ");
This. tsmi_chiniese.Text = ResourceCluture. GetString ("tsmi_chiniese_Name ");
This. tsmi_english.Text = ResourceCluture. GetString ("tsmi_english_Name ");
This. tsmi_japanese.Text = ResourceCluture. GetString ("tsmi_japanese_Name ");
This. tsmi_close.Text = ResourceCluture. GetString ("tsmi_close_Name ");
This. gbx_file.Text = ResourceCluture. GetString ("gbx_file_Name ");
This. lblPath. Text = ResourceCluture. GetString ("lblPath_Name ");
This. chkExpire. Text = ResourceCluture. GetString ("chkExpire_Name ");
This. btn_startPgm.Text = ResourceCluture. GetString ("btn_startPgm_Name ");
This. lbl_Note.Text = ResourceCluture. GetString ("lbl_Note_Name ");
This. groupBox_shortcut.Text = ResourceCluture. GetString ("groupBox_shortcut_Name ");
This. lbl_shortcutName.Text = ResourceCluture. GetString ("lbl_shortcutName_Name ");
This. btn_CreateIcon.Text = ResourceCluture. GetString ("btn_CreateIcon_Name ");
}
/// <Summary>
/// Set the text of the Interface Control Based on Different Languages and Cultures
/// </Summary>
/// <Param name = "strClutrue"> "en-US", "ch-CN", "ja-JP" etc .. </param>
Private void SetFormTextByLanguage (string strClutrue)
{
ResourceCluture. SetLocalClutrue (strClutrue );
This. lblPath. Text = ResourceCluture. GetString ("lblPath_Name ");
This. tsmi_language.Text = ResourceCluture. GetString ("tsmi_language_Name ");
This. tsmi_chiniese.Text = ResourceCluture. GetString ("tsmi_chiniese_Name ");
This. tsmi_english.Text = ResourceCluture. GetString ("tsmi_english_Name ");
This. tsmi_japanese.Text = ResourceCluture. GetString ("tsmi_japanese_Name ");
This. tsmi_close.Text = ResourceCluture. GetString ("tsmi_close_Name ");
This. gbx_file.Text = ResourceCluture. GetString ("gbx_file_Name ");
This. lblPath. Text = ResourceCluture. GetString ("lblPath_Name ");
This. chkExpire. Text = ResourceCluture. GetString ("chkExpire_Name ");
This. btn_startPgm.Text = ResourceCluture. GetString ("btn_startPgm_Name ");
This. lbl_Note.Text = ResourceCluture. GetString ("lbl_Note_Name ");
This. groupBox_shortcut.Text = ResourceCluture. GetString ("groupBox_shortcut_Name ");
This. lbl_shortcutName.Text = ResourceCluture. GetString ("lbl_shortcutName_Name ");
This. btn_CreateIcon.Text = ResourceCluture. GetString ("btn_CreateIcon_Name ");
}

4> basic completion. Let's take a look.

 



From xiashengwang's column

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.