Implementation of C # Multi-language real-time switching

Source: Internet
Author: User

See the online implementation of real-time switching, feeling really tired ah!   Write Yourself a bar! Boast: simple, elegant, efficient. If there's a better way, you can tell me.

Implementation is the way it is localized in C # itself ..... For example, you have implemented two languages on C # without problems.

1. Language Manager


public enum Languageenum
{
LANGUAGECN,
Languageen,
}


Class Managelanguage
{
public static Managelanguage Instance = new Managelanguage ();

ArrayList objectList = new ArrayList ();

<summary>
Register form
</summary>
<param name= "Item" ></param>
public void Regobject (Formbase item)
{
if (objectlist.contains (item) = TRUE)
{
Objectlist.add (item);
}
}

<summary>
Set Language
</summary>
<param name= "LG" > Language category </param>
public void SetLanguage (languageenum LG)
{
Switch (LG)
{
Case LANGUAGEENUM.LANGUAGECN:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo ("ZH-CN");
Callbacklanguage ();
Break
Case Languageenum.languageen:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo ("EN");
Callbacklanguage ();
Break
}
}

<summary>
Traverse a registered form and switch languages
</summary>
void Callbacklanguage ()
{
Traverse all the form to toggle its language
foreach (formbase form in objectList)
{
Form. Language ();
}
}


}

2. Language Switching interface

public partial class Formbase:form
{
Public Formbase ()
{
InitializeComponent ();

ManageLanguage.Instance.RegObject (this);
}


<summary>
Interface for language switching
</summary>
public virtual void Language ()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager (this . GetType ());


ArrayList list = new ArrayList ();
FindControls (list, this);

This. Text = resources. GetString ("$this. Text ");

foreach (Control ctl in list)
{
Resources. Applyresources (CTL, ctl. Name);
}
}

<summary>
To put a localized control into the list
</summary>
<param name= "List" ></param>
<param name= "CTL" ></param>
private void FindControls (ArrayList list, Control ctl)
{
Containers cannot be localized
if (CTL is ContainerControl)
{
MessageBox.Show (ctl. Name);
}
Else
{
List. ADD (CTL);
}

if (CTL. HasChildren)
{
foreach (Control c in CTL.) Controls)
{
if (c is Form)
{
}
Else
{
FindControls (list, c);
}
}
}
}

}

3. When you use it, just inherit it.

Public partial class Formmain:formbase, Icaseview

{

}

Implementation of C # Multi-language real-time switching

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.