Howto: Restart application after CurrentCulture (CurrentUICulture) change

In our applications we use something like this to get/set Frontend Language

      try
      {
          //get language from last user setting...
          Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(Settings.Default.CultureSelected);
      }


CurrentCulture will be set from a settings string...





     Settings.Default.CultureSelected = newLanguage; //"it-IT";
     Settings.Default.Save();

     //Manual restart necessary
     MessageBox.Show(Resources.YouHaveToRestartTheApplicationToAffectTheChangeToTheLanguage, Resources.Warning,
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Bad Example - Messagebox that the user should restart application, why not do it by code?



     Settings.Default.CultureSelected = newLanguage; //"it-IT";
     Settings.Default.Save();
    
     //Restart necessary? yes
     System.Diagnostics.Process.Start(Application.ExecutablePath);
    
     MessageBox.Show(Resources.ApplicationWillBeRestarted, Resources.Warning,
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

     // Use this since we are a WinForms app
     System.Windows.Forms.Application.Exit();
Good Example - Automatically restart from code



     Settings.Default.CultureSelected = newLanguage; //"it-IT";
     Settings.Default.Save();
    
     //Restart necessary? yes
     System.Diagnostics.Process.Start(Application.ExecutablePath);
    
     MessageBox.Show(Resources.ApplicationWillBeRestarted, Resources.Warning,
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

     
     if (MessageBox.Show(Resources.YouMustRestartTheComputerForTheChangesToTakeEffectNLNLRestartNow, Resources.Warning,
          MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
     {
         //Restart necessary? yes
         System.Diagnostics.Process.Start(Application.ExecutablePath);

         // Use this since we are a WinForms app
         System.Windows.Forms.Application.Exit();
     }    
Good (even better) Example - Ask user if we should restart now...

Thanks to Jury Strumpflohner for his suggestion!


PS
Remember, to start new process and THEN exit... else it want be started



PPS
Remember to set start parameters if you use them



PPPS
If user is in special application area the restart is maybe a bit distracting, cause he's loosing his actual context

  1. i navigated threw a list of articles
  2. selected one
  3. go into modify mode of that article
  4. then change the language of the application --> Restart?

8 comments:

Unknown said...

Interesting post.
What I would do however is to always explicitly ask the user for confirmation. I'm having something similar as the Windows update message in mind where you have the possibility to press "continue" or "restart later" or something like that. In that way you also avoid the problems you mentioned having the user to loose his current context :)

Peter Gfader said...

THANKS
i've added your suggestion...

Anonymous said...

hi peter....
i'm using the form language and put the design for each language for in the form now i want to change the language without restart all application and make user to login again....any idea..

Peter Gfader said...

Hi Anonymous,

What is the "from language"?

Anonymous said...

sorry i mean the from resource

Peter Gfader said...

you have to restart the application... there is no other way...
maybe you can trick the .net framework to reload the appdomain or something similar ...

Anonymous said...

i want to ask u about the example up this function cant run in my solution "CulturedSelected"

Peter Gfader said...

you mean the line "Settings.Default.CultureSelected = newLanguage;".

this is just to save the selected culture for the next start-up.
this is a user setting (string) in the configuration file (app.config)

try to remove it completely and see if it "runs"

Post a Comment

Latest Posts

Popular Posts