Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Language in C#

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

like image 200
Betamoo Avatar asked Jul 19 '10 08:07

Betamoo


People also ask

What does Setlocale do in C?

The setlocale function installs the specified system locale or its portion as the new C locale. The modifications remain in effect and influences the execution of all locale-sensitive C library functions until the next call to setlocale .

What is C language and its features?

C is a statically typed programming language, which gives it an edge over other dynamic languages. Also, unlike Java and Python, which are interpreter-based, C is a compiler-based program. This makes the compilation and execution of codes faster.


2 Answers

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;
like image 163
Dr Herbie Avatar answered Sep 27 '22 21:09

Dr Herbie


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.

like image 24
FvZ Avatar answered Sep 27 '22 21:09

FvZ