Changing Localization on the fly

  • Thread starter Thread starter greg
  • Start date Start date
G

greg

I've got a windows forms application that uses french and english. We're
localizing it right now. It does appear to work if I set the language

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

Just before we run it.

How can I setup a button that will toggle the language while the program is
running? It seems once it's running, changing the language has no effect.

Thanks,
Greg
 
greg said:
I've got a windows forms application that uses french and english. We're
localizing it right now. It does appear to work if I set the language

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

Just before we run it.

How can I setup a button that will toggle the language while the program
is
running? It seems once it's running, changing the language has no effect.

The language is processed in the Constructor of the Form. So once a form
is already constructed, it won't change language. After you change the
Thread.CurrentThread.CurrentUICulture, new forms that you open will show in
the new language, but the form itself from which you executed that change,
as well as any previously opened forms, will not change. You can close and
reopen the current form if you want it to show in the new language.
 
Localization data(strings and other data if any) are stored in satellite
dlls. So, once a dll is loaded, you can't unload that dll and load
another dll. The only way to change that (as far as I know) is to
restart your application. It applies to both .Net applications and MFC
applications.

Thanks & Regards,
Ashutosh
 
Hi Greg,

you can change it, look how it's done in
Windows Form Designer generated code.

Basically it's working like this:


private void localize(CultureInfo culture)
{
Thread.CurrentThread.CurrentUICulture = culture;

foreach (Form form in Application.OpenForms)
ApplyControlResources(new
ComponentResourceManager(form.GetType()), form);
}

private void ApplyControlResources
(ComponentResourceManager resourceManager, Control control)
{
if (control is Form)
resourceManager.ApplyResources(control, "$this");
else
resourceManager.ApplyResources(control, control.Name);

if (control is IContainerControl)
{
foreach (Control childControl in control.Controls)
{
ApplyControlResources(resourceManager, childControl);
}
}
}

Additional, you have to iterate through each forms menus and do the same.

Walter
 
Hi Greg,

Walter has provided a great solution to this question.

This solution works because the ComponentResourceManager.ApplyResources
will load the resource dll at runtime. It will not impact the performance
because the loading will only happen in the first time to apply the
resource and later the resource dll will stay in memory.

Please try Walter's solution and let us know if you have any problem when
implementing it. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Greg,

I have not heard from you for several days. I am writing to check the staus
of this issue. Would you mind letting us know any progress from your side?
If you have any problem regarding this issue, please feel free to let us
know. It is our pleasure to help you. Thanks.

Have a nice day.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top