Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to change language at runtime

Tags:

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 > Restart Application > Load languge choice before InitializeComponent();
  3. Using Form constructor to replace instance of active from (if this is even possible)
  4. Something else

There is so much half written threads about this but none provides real answer on what is proper way to do this?

UPDATE:
To clarify my question:

Doing something like this:

public Form1() {     Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");     this.InitializeComponent(); } 

works fine and all my controls and everything else in resources get translated correctly. And doing something like:

private void button1_Click(object sender, EventArgs e) {     Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); } 

does nothing, Form stays in language I set up before InitializeComponent();

like image 611
formatc Avatar asked Jul 29 '12 17:07

formatc


People also ask

How do I change the language on Android at runtime and don't go mad?

Use SharedPreferences to keep track of the language the user chose, and then set the activities to use that language in the onCreate (), and maybe onResume() method. This way it will persist across app restarts etc. do all developer uses this way in there apps, I feel its not very clear !

How do I change the whole language on Android?

Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have to choose qualifiers as Locale from the available list and select the language as Hindi from the drop-down list. Below is the picture of the steps to be performed.


1 Answers

I believe the solution shown in Hans Passant's comment might be the only (general) solution.

Personally, I use this base class for all forms that need to be localized:

public class LocalizedForm : Form {     /// <summary>     /// Occurs when current UI culture is changed     /// </summary>     [Browsable(true)]     [Description("Occurs when current UI culture is changed")]     [EditorBrowsable(EditorBrowsableState.Advanced)]     [Category("Property Changed")]     public event EventHandler CultureChanged;      protected CultureInfo culture;     protected ComponentResourceManager resManager;      /// <summary>     /// Current culture of this form     /// </summary>     [Browsable(false)]     [Description("Current culture of this form")]     [EditorBrowsable(EditorBrowsableState.Never)]     public CultureInfo Culture     {         get { return this.culture; }         set         {             if (this.culture != value)             {                 this.ApplyResources(this, value);                  this.culture = value;                 this.OnCultureChanged();             }         }     }      public LocalizedForm()     {         this.resManager = new ComponentResourceManager(this.GetType());         this.culture = CultureInfo.CurrentUICulture;     }      private void ApplyResources(Control parent, CultureInfo culture)     {         this.resManager.ApplyResources(parent, parent.Name, culture);          foreach (Control ctl in parent.Controls)         {             this.ApplyResources(ctl, culture);         }     }      protected void OnCultureChanged()     {         var temp = this.CultureChanged;         if (temp != null)             temp(this, EventArgs.Empty);     } } 

Then instead of directly changing Thread.CurrentThread.CurrentUICulture, I use this property in static manager class to change UI culture:

public static CultureInfo GlobalUICulture {     get { return Thread.CurrentThread.CurrentUICulture; }     set     {         if (GlobalUICulture.Equals(value) == false)         {             foreach (var form in Application.OpenForms.OfType<LocalizedForm>())             {                 form.Culture = value;             }              Thread.CurrentThread.CurrentUICulture = value;         }     } } 
like image 74
mnn Avatar answered Sep 20 '22 08:09

mnn