Localization at runtime

前端 未结 4 1157
鱼传尺愫
鱼传尺愫 2021-01-05 22:49

I have created Windows Form Program in C#. I have some problems with localization. I have resource files in 3 languages. I want to click each language button and change lang

相关标签:
4条回答
  • 2021-01-05 23:01

    I wrote a RuntimeLocalizer class with following features:

    • Changes and updates localization for all Controls and SubControls in a Form
    • Also changes localization for all SubItems of all MenuStrips

    Usage Example: RuntimeLocalizer.ChangeCulture(MainForm, "en-US");


    using System.Windows.Forms;
    using System.Globalization;
    using System.Threading;
    using System.ComponentModel;
    

    public static class RuntimeLocalizer
    {
        public static void ChangeCulture(Form frm, string cultureCode)
        {
            CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);
    
            Thread.CurrentThread.CurrentUICulture = culture;
    
            ComponentResourceManager resources = new ComponentResourceManager(frm.GetType());
    
            ApplyResourceToControl(resources, frm, culture);
            resources.ApplyResources(frm, "$this", culture);
        }
    
        private static void ApplyResourceToControl(ComponentResourceManager res, Control control, CultureInfo lang)
        {
            if (control.GetType() == typeof(MenuStrip))  // See if this is a menuStrip
            {
                MenuStrip strip = (MenuStrip)control;
    
                ApplyResourceToToolStripItemCollection(strip.Items, res, lang);
            }
    
            foreach (Control c in control.Controls) // Apply to all sub-controls
            {
                ApplyResourceToControl(res, c, lang);
                res.ApplyResources(c, c.Name, lang);
            }
    
            // Apply to self
            res.ApplyResources(control, control.Name, lang);
        }
    
        private static void ApplyResourceToToolStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang)
        {
            for (int i = 0; i < col.Count; i++)     // Apply to all sub items
            {
                ToolStripItem item = (ToolStripMenuItem)col[i];
    
                if (item.GetType() == typeof(ToolStripMenuItem))
                {
                    ToolStripMenuItem menuitem = (ToolStripMenuItem)item;
                    ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang);
                }
    
                res.ApplyResources(item, item.Name, lang);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-05 23:05

    You will need to reload the controls for it to reflect the New culture values

    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
    

    and then you would have to apply for each control using resources.ApplyResources

    Please have a look here

    0 讨论(0)
  • 2021-01-05 23:14

    Changing the CurrentUICulture will not automatically reload the resources. You need to perform it manually (http://msdn.microsoft.com/en-us/magazine/cc163609.aspx#S8)

    You can copy the code related to the localization from InitializeComponent() into another function:

    void LoadResources(){
    
        this.Title = MyApp.Resources.MainFormCaption;
        this.lblWelcomeMessage.Text = MyApp.Resources.UserWelcome;
    
    }
    
    0 讨论(0)
  • 2021-01-05 23:17

    Thanks V4Vendetta and others.. Solution is...

    private void RussianFlag_Click(object sender, EventArgs e)
            {
                if (currentLanguage != "RUS")
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
                    ChangeLanguage("ru-RU");
                }
            }
    

    .... .... ...

    private void ChangeLanguage(string lang)
            {
                foreach (Control c in this.Controls)
                {
                    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
                    resources.ApplyResources(c, c.Name, new CultureInfo(lang));
                    if (c.ToString().StartsWith("System.Windows.Forms.GroupBox"))
                    {
                        foreach (Control child in c.Controls)
                        {
                            ComponentResourceManager resources_child = new ComponentResourceManager(typeof(Form1));
                            resources_child.ApplyResources(child, child.Name, new CultureInfo(lang));
                        }
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题