Change Language in C#
Asked Answered
A

5

13

I am developing a multilingual program in C# on Windows

How to change Windows writing language on certain actions...
e.g. to change from English to Arabic on focus event.

Thanks

Abdominous answered 19/7, 2010 at 8:16 Comment(1)
These similar post may shed some light #397856 #271329 Cheers!Devalue
W
16

To select a whole new culture, set the CurrentThread.CurrentCulture to a new culture, e.g. to set to French:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("fr-FR");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;

You can find a list of the predefined CultureInfo names here and here.

If you want to change certain aspects of the default culture, you can grab the current thread's culture, use it it's name to create a new CultureInfo instance and set the thread's new culture with some changes, e.g. to change the current culture to use the 'Euro' symbol:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo( System.Threading.Thread.CurrentThread.CurrentCulture.Name);
ci.NumberFormat.CurrencySymbol = "€";
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
Whig answered 19/7, 2010 at 8:31 Comment(2)
For some situations it is also necessary to set System.Threading.Thread.CurrentThread.CurrentUICulture. (Note the "UI" in the middle of the property name.)Aubin
I had to set the UICulture also while running unit tests. Comment above saved me :)Bloodshot
B
3
Thread.CurrentThread.CurrentCulture = yournewculture;

Also see the CurrentUICulture property.

Bulbiferous answered 19/7, 2010 at 8:22 Comment(1)
Can you please state how to get "yournewculture"Abdominous
U
3

In load Event insert the code below:

InputLanguage.CurrentInputLanguage =
      InputLanguage.FromCulture(new System.Globalization.CultureInfo("fa-IR"));
Unquote answered 10/12, 2014 at 17:54 Comment(0)
M
2

In addition, if you want to refresh all the controls' resources at runtime, you will need to use something like this:

private void RefreshResources(Control ctrl, ComponentResourceManager res)
{
    ctrl.SuspendLayout();
    res.ApplyResources(ctrl, ctrl.Name, CurrentLocale);
    foreach (Control control in ctrl.Controls)
        RefreshResources(control, res); // recursion
    ctrl.ResumeLayout(false);
}

If you want a better example check my blog.

Malm answered 22/6, 2012 at 14:47 Comment(0)
B
2

This statements were helpful for me:

string myLanguage = "HE-IL";
InputLanguage.CurrentInputLanguage =
   InputLanguage.FromCulture(new System.Globalization.CultureInfo(myLanguage));
Berck answered 14/10, 2014 at 18:53 Comment(1)
this is great too, amending that first line, can do it for whatever country lingoes.net/en/translator/langcode.htmMariano

© 2022 - 2024 — McMap. All rights reserved.