Switch language (localization) during execution

M

Martin

Morning,

I am developing a C# 2.0 application and I have created two language
(English and French) for my main form. In Visual Studio 2005, I can
switch language with the Language property without any problem.

My problem is that I want to switch the display language during the
execution of the application. I have created a button with the
following code on the click but that doesn't work.

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");

So, how can I switch the language during the execution ? Thanks for you
help!

Martin
 
M

Morten Wennevik

Hi Martin,

Try using the culture "fr-FR" instead of just "fr".
Try setting both CurrentCulture and CurrentUICulture if the above is not
enough.
 
M

Martin

Hi Morten,

I tried the following code :

Thread.CurrentThread.CurrentCulture = new
CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("fr-FR");

The display language don't change. However, I have two ressources
files, frmMain.en.resx and frmMain.fr.resx...

Thanks for you help!
 
M

Morten Wennevik

There should be a separate resource file for each language.
Which resource file to be used is determined by CurrentUICulture.
How to change culture is described on this page

http://msdn2.microsoft.com/en-us/library/b28bx3bh.aspx



Hi Morten,

I tried the following code :

Thread.CurrentThread.CurrentCulture = new
CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("fr-FR");

The display language don't change. However, I have two ressources
files, frmMain.en.resx and frmMain.fr.resx...

Thanks for you help!
 
M

Mathieu Cartoixa

Martin a écrit :
I am developing a C# 2.0 application and I have created two language
(English and French) for my main form. In Visual Studio 2005, I can
switch language with the Language property without any problem.

My problem is that I want to switch the display language during the
execution of the application. I have created a button with the
following code on the click but that doesn't work.

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");

So, how can I switch the language during the execution ? Thanks for you
help!

Hi,

My guess is that the resource manager uses the
Thread.CurrentThread.CurrentUICulture property only once to set the Text
values of all UI components in your form, and this happens in the
InitializeComponent() method. Once initialized, the Text property of the
UI components will not be affected by changing the current culture. But
that should work for new forms you would create.
So one way to achieve this would be to add a call to
InitializeComponent() after changing the culture. Unfortunately, not
only does this method initialize the property of the UI components, but
it also creates them, so that you would have to remove the previous ones
"by hand" before calling the method.
Another way would be to duplicate the code found in the
InitializeComponent() method that is culture specific (like Text
properties assignments), and execute it every time you change your culture.
Or maybe you could use reflection mecanisms to automatically reset the
Text properties of all the UI components that are in the form. This
looks like the more promising to me, though the most complicated,
especially because of the editable components, filled with user data,
that you would want to exclude from this process.

Quite abstract, but I hope it helps anyway...

Mathieu
 
M

Martin

It works! Thanks Mathieu!

Mathieu said:
Martin a écrit :

Hi,

My guess is that the resource manager uses the
Thread.CurrentThread.CurrentUICulture property only once to set the Text
values of all UI components in your form, and this happens in the
InitializeComponent() method. Once initialized, the Text property of the
UI components will not be affected by changing the current culture. But
that should work for new forms you would create.
So one way to achieve this would be to add a call to
InitializeComponent() after changing the culture. Unfortunately, not
only does this method initialize the property of the UI components, but
it also creates them, so that you would have to remove the previous ones
"by hand" before calling the method.
Another way would be to duplicate the code found in the
InitializeComponent() method that is culture specific (like Text
properties assignments), and execute it every time you change your culture.
Or maybe you could use reflection mecanisms to automatically reset the
Text properties of all the UI components that are in the form. This
looks like the more promising to me, though the most complicated,
especially because of the editable components, filled with user data,
that you would want to exclude from this process.

Quite abstract, but I hope it helps anyway...

Mathieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top