Change system language for single / active window in WPF

Is it possible to change the system language for only one window in WPF?

I know about InputLanguageManager , but I assume that it changes the language throughout the system.

+4
source share
2 answers

The InputLanguageManager does exactly what you ask. It changes the keyboard layout for the current application.

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

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

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

The system language (or rather, the system language) and the keyboard layout are completely different concepts. A keyboard layout is a layout for your keyboard.

There are three different locales in a .NET application:

  • The user interface locale is the language used to display messages and select localized strings and layouts of the user interface. You can change the locale of the thread's user interface by setting its Thread.CurrentUICulture property. Its initial value is determined by the OS display language in regional settings.
  • The stream locale is used to parse strings and convert date and number to string. You can change it by setting the Thread.CurrentCulture property. Its initial value is determined by the regional OS settings. Format Property
  • System locales are used by applications other than Unicode, or when writing to ASCII files and the console.

You can also use WPF data binding and use InputLanguage as an attached property. In your XAML, you can add the InputLanguageManager.InputLanguage property to the element declaration as follows:

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

You can then bind the property to the property in code or in the ViewModel. For instance.

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

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

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

or

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

You can continue with this and associate the InputLanguage property with other elements, for example. list of language options

+14
source

To change the layout of the keyboard, you InputLanguageManager on the right track.

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

With the first parameter of the SetInputLanguage() method, you set DependencyObject , which is the target of your keyboard layout.

+2
source

Source: https://habr.com/ru/post/1489468/


All Articles