Winform is more complicated to change language types and requires applying language resources according to different languages. When the software switches language, it needs to refer to the current UI culture thread to the corresponding language type. There are three common methods, two are used here. Comparison to find the advantages and disadvantages:
/// <summary> /// Get cultural information of UI/// Creator: Yang Zhao/// Creation time: 2019.05.20/// </summary> /// <param name="language">Language</param>/// Abbreviation of <returns> locale. For example: en-US in English; Chinese: zh-CN</returns>public static string GetUICulture(LanguageType language) { string lang = "en-US"; if (language == ) { lang = "zh-CN"; } else if (language == ) { lang = "en-US"; } return lang; } /// <summary> /// Set language/// </summary> /// <param name="control">Objects that need to switch language</param>/// <param name="language">Language</param>/// Abbreviation of <returns> locale. For example: en-US in English; Chinese: zh-CN</returns>public static string SetLanguage( this Control control, LanguageType language) { string lang = GetUICulture(language); //In this way, if the control event added by writing the code yourself (that is, the addition of the event is not in the InitializeComponent method), you need to rewrite and add it once. //All selected settings need to be rewrite once. Not suitable for creating a form that only calls //Application Language Resources // = new (lang); /////Clean the language resources that the control has applied //(); /////Reinitialize the interface //Type type = (); ////MethodInfo[] methodInfos = ( ); //MethodInfo method = ("InitializeComponent", | ); //if (method != null) //{ // (form, null); //} //The following method will have problems. In a form with a dataGridView control, if it is instantiated first and then applied, new resources will not be applied. Then I found that the header of the DataGridView control is not inherited from Control // And if there is a control with the same name, the application will error. For example: There is a child control in the combination control that is lblTitle, but there is also a control in the form that is lblTitle, and the application resource will make an error. if ( != language) { = new (lang); = language; } if (control != null) { var frmtype = (); ComponentResourceManager resources = new ComponentResourceManager(frmtype); (control, "$this"); AppLang(control, resources); } return lang; } /// <summary> /// Modification: Zhang Zhengxuan/// Time: 2017.12.26/// Reason: The unit display is too small, and the "Cancel" display will be wrapped in English/// </summary> /// <param name="control"></param> /// <param name="resources"></param> private static void AppLang(Control control, ComponentResourceManager resources) { if (control is Control) { foreach (Control c in ) { (c, ); AppLang(c, resources); //The header of the DataGridView control is not inherited from Control if (c is DataGridView) { DataGridView dgv = c as DataGridView; foreach(var item in ) { (item, (item as DataGridViewColumn).Name); } } } } }
The above is the detailed content on how to change the language type of Winform form. For more information about changing the language type of Winform form, please pay attention to my other related articles!