Hi �How can I change the language of my multilingual application in run time WITHOUT restarting the application
Application contains many windows (about 30). I create resource files for 3 languages; I change the current culture and prompt for restart the application. How I can do this without restart. |
| Lucifer L. Saturday, September 26, 2009 5:54 PM |
Hi Lucifer L.,
Yes, I understand what you mean. Based on my understanding, it is hard to change the culture of an opened form. Could you please follow my suggestion to change the structure of your program?
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- Marked As Answer byLucifer L. Wednesday, September 30, 2009 6:22 PM
-
|
| Aland Li Wednesday, September 30, 2009 2:07 AM |
I just created a very simple test app which comprised of a form with 2 radiobuttons and a label plus two resx files (base.resx and base.de-DE.resx). I added appropriate values for the Text properties to the resx files. I then added the following code to the form and the text of all controls changed as expected when the RadioButtons were changed (checkedChanged handler added to radioButton1):
public Form1()
{
InitializeComponent();
LocalizeControls();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
string language = "en-GB";
if (radioButton2.Checked)
{
language = "de-DE";
}
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
LocalizeControls();
}
private void LocalizeControls()
{
this.Text = _base.frmCaption;
label1.Text = _base.txtLanguage;
radioButton1.Text = _base.optEnglish;
radioButton2.Text = _base.optGerman;
}
This didn't require a restart of the app or closure of the form and is (I think) a very simple solution.
Mick Doherty
http://dotnetrix.co.uk- Marked As Answer byAland LiMSFT, ModeratorFriday, October 02, 2009 2:52 AM
-
|
| Mick Doherty Wednesday, September 30, 2009 6:01 PM |
Thanks for help... right way to prompting user to restart the application. no sense to change structure of application , since the login form shows when application runs. ))) - Marked As Answer byAland LiMSFT, ModeratorThursday, October 01, 2009 2:46 AM
-
|
| Lucifer L. Wednesday, September 30, 2009 6:26 PM |
Thread manipulations should be foremost, so in esence you are advised to first show some UI to select Culture, then Login, then loadMainForm and all afterward initialization. VB.NET to C#
http://www.developerfusion.com/tools/convert/vb-to-csharp/- Marked As Answer byAland LiMSFT, ModeratorThursday, October 01, 2009 2:46 AM
-
|
| Se3ker385 Wednesday, September 30, 2009 6:37 PM |
I change the current culture and prompt for restart the application. How I can do this without restart.
How are you changing the current culture? I don't know much about multilingual apps but, don't you just change the current culture and then invalidate?
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
foreach (Form form in Application.OpenForms)
form.Invalidate(true);
Mick Doherty
http://dotnetrix.co.uk |
| Mick Doherty Sunday, September 27, 2009 10:35 AM |
Iuse that code... thats not working here is My code.
Call Mod_Main.CreateCulture(OPT.Item.GetLanguage) ' getting language en,ru and creating culture named Myculture
Threading.Thread.CurrentThread.CurrentUICulture = MyCulture ' changing current thread
Threading.Thread.CurrentThread.CurrentCulture = MyCulture
Call Mod_Dictionary.GetResourceFile() ' getting additional resource file for messageboxes and other
For Each Frm As Form In Application.OpenForms ' setting language
Frm.Invalidate(True)
Next
thats changes all words containing in additional resource file.Also changes all the closed forms. but in main form and options dialog formstill not changed. |
| Lucifer L. Sunday, September 27, 2009 1:06 PM |
Hi Lucifer L.,
From your code snippet, the user select the language on the main form. I suggest that we can decide the UI culture when the user logins. The steps below show the running sequences in the main method:
1. Show the login form to allow the user enter the name, password and select the language.
2. Change the culture of the current thread.
3. Run the main form.
This is a code snippet:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm form = new LoginForm();
if (form.ShowDialog() == DialogResult.OK)
{
//Get selected language here and set the culture of the application.
CultureInfo culture = new CultureInfo(form.GetSelectedLanguage());
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
//Run the main form.
Application.Run(new MainForm());
}
}
Let me know if this does not help. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, September 28, 2009 7:28 AM |
Thanks for answer. in that case Your code works perfectly... but in My case user logins (login form contains only host,username,password) .. after application runs.. its shows splash screen and loading. after shows the main form and on it modaly shows login form... user after logon change the language from options form , which opens from menu in main form. thats all. after changing with My code oll changes aplies only in forms , which closed.. and opens after changing culture... but forms which already opening- not effect (et MainForm and options form)... |
| Lucifer L. Monday, September 28, 2009 2:20 PM |
Hi Lucifer L.,
Yes, I understand what you mean. Based on my understanding, it is hard to change the culture of an opened form. Could you please follow my suggestion to change the structure of your program?
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- Marked As Answer byLucifer L. Wednesday, September 30, 2009 6:22 PM
-
|
| Aland Li Wednesday, September 30, 2009 2:07 AM |
I just created a very simple test app which comprised of a form with 2 radiobuttons and a label plus two resx files (base.resx and base.de-DE.resx). I added appropriate values for the Text properties to the resx files. I then added the following code to the form and the text of all controls changed as expected when the RadioButtons were changed (checkedChanged handler added to radioButton1):
public Form1()
{
InitializeComponent();
LocalizeControls();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
string language = "en-GB";
if (radioButton2.Checked)
{
language = "de-DE";
}
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
LocalizeControls();
}
private void LocalizeControls()
{
this.Text = _base.frmCaption;
label1.Text = _base.txtLanguage;
radioButton1.Text = _base.optEnglish;
radioButton2.Text = _base.optGerman;
}
This didn't require a restart of the app or closure of the form and is (I think) a very simple solution.
Mick Doherty
http://dotnetrix.co.uk- Marked As Answer byAland LiMSFT, ModeratorFriday, October 02, 2009 2:52 AM
-
|
| Mick Doherty Wednesday, September 30, 2009 6:01 PM |
Thanks for reply Mick Doherty , your code works ok... but try the same way with the 30 forms and 10-40 controls on each one... ))) |
| Lucifer L. Wednesday, September 30, 2009 6:21 PM |
Thanks for help... right way to prompting user to restart the application. no sense to change structure of application , since the login form shows when application runs. ))) - Marked As Answer byAland LiMSFT, ModeratorThursday, October 01, 2009 2:46 AM
-
|
| Lucifer L. Wednesday, September 30, 2009 6:26 PM |
Thread manipulations should be foremost, so in esence you are advised to first show some UI to select Culture, then Login, then loadMainForm and all afterward initialization. VB.NET to C#
http://www.developerfusion.com/tools/convert/vb-to-csharp/- Marked As Answer byAland LiMSFT, ModeratorThursday, October 01, 2009 2:46 AM
-
|
| Se3ker385 Wednesday, September 30, 2009 6:37 PM |
Thanks for reply Mick Doherty , your code works ok... but try the same way with the 30 forms and 10-40 controls on each one... )))
You didn't say you wanted a quick and easy solution that requires no work. You asked if you could change the culture without restarting the app. My simple example showed that you can do exactly what you asked so long as you are prepared to modify the way in which you currently assign the resources and in fact is relatively simple. Whilst I would agree that the Login structure as outlined by Aland Li is the correct solution to this problem, and is exactly the solution that I used in the simple App that I created before making my first post, it is not the answer to the question that you asked.
How I can do this without restart.
I can see no reason to change the language during the normal operation of an application, but since that is what you asked, that is what I showed you.
Mick Doherty
http://dotnetrix.co.uk |
| Mick Doherty Thursday, October 01, 2009 6:24 PM |