1

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Adhi
  • 147
  • 1
  • 11

3 Answers3

1

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");
}
Arjun Singh
  • 125
  • 9
1

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");
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • For demo this is okay. It is possible in a real work application. how can I provide support for these in a real application like, my control will have the default support for two different cultures on a same form in the core. – Adhi Jan 02 '17 at 10:39
1

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • For more information about multi-language applications using windows forms, see: [How to make multi language app in winforms](http://stackoverflow.com/q/32989100/3110834) – Reza Aghaei Jan 02 '17 at 15:59
  • Is that the ApplyResources is only need to get the localized string from the resx file. – Adhi Jan 03 '17 at 11:18
  • In a localized form, the `ApplyResources` will be automatically called in `InitializeComponent` for each control. What we did here is calling `ApplyResources` for a specific control after setting `CurrentUICulture` to a specific culture. This way can load different cultures for different controls if we need. – Reza Aghaei Jan 03 '17 at 12:30
  • Thanks for your valuable response. it helps me to find the solution for my problem. – Adhi Mar 23 '17 at 03:41