Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change system language for one/active window in WPF

Tags:

c#

windows

wpf

Is there a posibility to change system language only for one window in WPF?

I know about InputLanguageManager but I assume that it changes language in whole system.

like image 215
Mateusz Dembski Avatar asked Nov 30 '22 20:11

Mateusz Dembski


2 Answers

InputLanguageManager does exactly what you are asking for. It changes the keyboard layout for the current application.

The keyboard layout is kept by the OS for each running application. Eg. if you open Notepad and switch to Russian, the open IE and switch to English, when you activate the Notepad application, your keyboard locale will still be Russian.

The following line changes the keyboard locale just for the current application:

InputLanguageManager.Current.CurrentInputLanguage = new CultureInfo("el-GR");

The system language (or rather, the system locale) and the keyboard layout are completely different concepts. The keyboard layout is the layout of your keyboard.

There are three different locales used in a .NET application:

  • The UI locale is the locale used to display messages and select localized UI strings and layouts. You can change a thread's UI locale by setting its Thread.CurrentUICulture property. Its initial value is governed by the OS's display language in regional settings
  • The thread's locale is used to parse strings and convert dates and numerics to string. You can change it by setting the Thread.CurrentCulture property. Its original value is governed by the OS's regional settings Format property
  • The system locale is used by non-Unicode applications or when writing to ASCII files and the console.

You can also take advantage of WPF data binding and use InputLanguage as an attached property. In your XAML you can add the InputLanguageManager.InputLanguage property to an element's declaration like this:

<TextBox InputLanguageManager.InputLanguage="en-US"></TextBox>

You can then bind the property to a property in your code-behind or you ViewModel. Eg.

<TextBox InputLanguageManager.InputLanguage="{Binding MyLanguageInfo}"></TextBox>

Setting this property to a specific value will cause the keyboard of the UI element to change:

MyLanguageInfo = new CultureInfo("en-US");

or

MyLanguageInfo = new CultureInfo("el-GR");

You can go further with this and bind the InputLanguage property other elements, eg. a listbox of language options

like image 98
Panagiotis Kanavos Avatar answered Dec 04 '22 07:12

Panagiotis Kanavos


For Keyboard Layout Changing are you InputLanguageManager on the right way.

InputLanguageManager.SetInputLanguage(this,CultureInfo.CreateSpecificCulture("ru"));

With the first Parameter of the SetInputLanguage() methode you set the DependencyObject which is the target of your Keyboard Layout.

like image 26
Smartis Avatar answered Dec 04 '22 06:12

Smartis