How to set two different Cultures for two different Controls on a single Form at the same time?

890 views Asked by At

I need to show an demo of how the control can be localized using the satellite assemblies. While doing this I have stuck with one place where i have two controls on a form both are same. I used to derive it from the Label. Now I need to display the control1 with culture fr-FR and control2 with the culture de-DE.

Is there any options is available to set different cultures for the same controls that displayed in a form.

The following screenshot will show my need.

enter image description here

Please suggest me is that possible or not. If its possible, let me know how can I achieve this.

3

There are 3 answers

4
Reza Aghaei On BEST ANSWER

You can rely on localization feature of windows forms. This way you can setup your controls with different properties for different cultures. Then you can show the whole form with properties set for a specific culture.

Also you have option to show each control with a different culture. To do so it's enough to use such code in your form Load event handler:

System.Threading.Thread.CurrentThread.CurrentUICulture =
    System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
var resources = new System.ComponentModel.ComponentResourceManager(this.GetType());
resources.ApplyResources(button1, button1.Name);

And simply for button2 use above code with de-DE culture.

Note

  1. Above solution is not limited to Text property, it supports all Localizable properties.

  2. You can simply make it as an extension method for control class.

  3. It has designer-support for creating localized appearance of your controls.

1
Fabio On

Because you want to demonstrate localization feature for your application you can change current thread culture when you updating value for your control

private void SetLocalizedTextForLabel(Label label, string language)
{
    var original = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

    // Here value will be retrieved from YourResource based on the current culture
    label.Text = Properties.YourResource.YourText;

    Thread.CurrentThread.CurrentUICulture = original;
}

Then use it

SetLocalizedTextForLabel(frenchLabel, "fr-FR");
SetLocalizedTextForLabel(germanLabel, "de-DE");
1
Arjun Singh On

Try to use Following code.

if(langCode=="fr-FR")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
}
if(langCode=="de-DE")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
}