Proper way to change language at runtime

后端 未结 5 1714
陌清茗
陌清茗 2020-11-29 01:37

What is the proper way to change Form language at runtime?

  1. Setting all controls manually using recursion like this
  2. Save language choice to file > Rest
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 02:15

    I've discovered this kind of approach a few minutes ago. Just quick and simple restart of the main form. Meybe it will help to someone. Event is created inside the form on my own, called when user selects the language from menu but after the selected culture's name is saved into the settings. Culture names are then loaded from that settings. Works exactly as I need and looks like proper solution.

    static class Program
    {
        private static bool doNotExit = true;
        private static FormMain fm;
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
    
    
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            while(doNotExit)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(Properties.Settings.Default.language);//
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(Properties.Settings.Default.language);//
    
                doNotExit = false;
                fm = new FormMain();
                fm.lanugageChangedEvent += new EventHandler(main_LanugageChangedEvent);
                Application.Run(fm);
            }
        }
    
    
    
        static void main_LanugageChangedEvent(object sender, EventArgs e)
        {  
            doNotExit = true;
            fm.Close();   
        }
    }
    

提交回复
热议问题