简体   繁体   中英

How to change the language of the text on all control in the form

Is there a way to change the language of all the controls in the form in runtime?

For example, I have a button in the form and it has a text of "Hello". How can I change that into different language on runtime? dynamically every language that I can set. Is there a way to do it??

I've found an answer, but it seems that its not working it has something to do with cultureinfo. Any suggestions how to do it?

This is my code

public partial class Form1 : BaseLanguageForm
    {
        public Form1()
        {
            InitializeComponent();   
        }



        private void button1_Click(object sender, EventArgs e)
        {
            this.TriggerLanguageChange("fr-FR");
        }
    }
 public class LanguageArgs : EventArgs
    {
        string _languageSymbol;
        /// <summary>
        /// Gets the language symble.
        /// </summary>
        public string LanguageSymbol
        {
            get { return _languageSymbol; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LanguageArgs"/> class.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        public LanguageArgs(string symbol)
        {
            this._languageSymbol = symbol;
        }
    }
    public class BaseLanguageForm : Form
    {
        /// <summary>
        /// Triggers the language change event.
        /// </summary>
        /// <param name="languageSymbol">The language symbol.</param>
        protected void TriggerLanguageChange(string languageSymbol)
        {
            if (Form1.onLanguageChanged != null)
            {
                LanguageArgs args = new LanguageArgs(languageSymbol);
                Form1.onLanguageChanged(this, args);
            }
        }

        /// <summary>
        /// This should be triggered whenever the combo box value chages
        /// It is protected, just incase you want to do any thing else specific to form instacne type
        /// </summary>
        protected static event EventHandler<LanguageArgs> onLanguageChanged;

        /// <summary>
        /// This will be called from your form's constuctor 
        /// (you don't need to do anything, the base class constuctor is called automatically)
        /// </summary>
        public BaseLanguageForm()
        {
            //registering to the event
            BaseLanguageForm.onLanguageChanged += new EventHandler<LanguageArgs>(BaseLanguageForm_onLanguageChanged);
        }

        /// <summary>
        /// The function that was regidtered to the event
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        void BaseLanguageForm_onLanguageChanged(object sender, LanguageArgs e)
        {
            string lang = e.LanguageSymbol;
            foreach (Control c in this.Controls)
            {
                ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
                crm.ApplyResources(c, c.Name, new CultureInfo(lang));
            }
        }

    }

There are two kinds of things you must translate: The controls that are visible on the form and the strings you you need during runtime to say, change a button's Text or a MessageBox's caption etc..

You translate the controls you have on the form in the designer:

  • First do all the layout stuff in the Laguage = Standard.
  • Then change the Languge in the properties tab to another language.
  • Now translate all Texts in all controls.
  • You also may need to change the layout a little to allow for longer texts; this is fine!

Look at the project explorer: For each form and for each language there is now a new FormN.lang.resx file, eg:

Form1.resx
Form1.en.resx
Form1.de.resx

Now when you call a changeLanguage function, maybe like this one:

private void ChangeLanguage(Control ctl, string lang)
{
   resources.ApplyResources(ctl, ctl.Name, new CultureInfo(lang));
   foreach (Control c in ctl.Controls) ChangeLanguage(c, lang);
}

maybe like this:

private void cb_language_Click(object sender, EventArgs e)
{
    if (cb_language.Text == "DE")
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
        resources = new ComponentResourceManager(typeof(Form1));
        ChangeLanguage(this, "de-DE");
        cb_language.Text = "EN";
    }
    else
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        resources = new ComponentResourceManager(typeof(Form1));
        ChangeLanguage(this, "en-US");
        cb_language.Text = "DE";
    }
}

..all visible controls will change their Texts.

The second thing to do is to create strings resource files, best one for standard and one for each language (repeating the standard values!): Add new element - resource file with names like these:

strings.resx
strings.de.resx
strings.en.resx

Then you add one name-value pair for each string you need in code, eg:

msgCap_OW           File Exists
msgTxt_OW           Overwrite (Yes)
                    Use next free Index? (No)
                    Cancel?
cb_autoScrapeStart  Start Timed Screenshots

Note: By shift-enter in a value field you can enter multiline values..

And finally you must change your code to use the strings resources, eg like this:

MessageBox.Show(strings.msgTxt_OW, strings.msgCap_OW, ..);

or:

cb_autoScrape.Text = (scrapeTimer.Enabled ? 
                      strings.cb_autoScrapeStop : strings.cb_autoScrapeStart);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM