How to change Input Language programmatically?

Do you know that your Windows Forms App could switch between installed languages at run-time?

An Input language is a Culture/Keyboard layout pair that determines how the physical keys on a keyboard map or plot to characters in a language (read more about Input Language in MSDN Library). Microsoft Windows allows installing more than one input language but international users usually use two: English and a national.

Since version 1.0 .NET Framework supports a special public sealed class - InputLanguage (in System.Windows.Forms namespace, assembly – System.Windows.Forms.dll). The property we are going to use today is CurrentInputLanguage of the InputLanguage type.

Enforcing to use a particular input language could be useful in many cases but please note that this function changes CurrentInputLanguage for the whole App and not just for a TextBox or any other selected control.

I created four overloaded functions that demonstrate this functionality. Actually only 4th function (the only one in VB example) does change input language, first demonstrates how to switch between languages and two others - how to create a InputLanguage class based on  string or integer language codes (please see code comments for more details).

C# code:

 /// <summary>
 /// Changing Current Input Language to a next installed language
 /// </summary>
 public void ChangeInputLanguage()
 {
    // Nothing to do if there is only one Input Language supported:
     if (InputLanguage.InstalledInputLanguages.Count == 1)
        return;
 
    // Get index of current Input Language
     int currentLang = InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.CurrentInputLanguage);
 
    // Calculate next Input Language
     InputLanguage nextLang = ++currentLang == InputLanguage.InstalledInputLanguages.Count ? 
        InputLanguage.InstalledInputLanguages[0] : InputLanguage.InstalledInputLanguages[currentLang];
 
    // Change current Language to the calculated:
     ChangeInputLanguage(nextLang);
 }
 
 /// <summary>
 /// Changing current Input Language to a new one passed in the param
 /// </summary>
 /// <param name="ISOLang">ISO Culture name string code e.g. "en" for English</param>
 public void ChangeInputLanguage(string ISOLang)
 {
    // Convert ISO Culture name to InputLanguage object. Be aware: if ISO is not supported
     // ArgumentException will be invoked here
     InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(ISOLang));
    ChangeInputLanguage(nextLang);
 }
 
 /// <summary>
 /// Changing current Input Language to a new one passed in the param
 /// </summary>
 /// <param name="LangID">Integer Culture code e.g. 1033 for English</param>
 public void ChangeInputLanguage(int LangID)
 {
    // Convert Integer Culture code to InputLanguage object. Be aware: if Culture code is not supported
     // ArgumentException will be invoked here
     InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(LangID));
    ChangeInputLanguage(nextLang);
 }
 
 /// <summary>
 /// Changing current Input Language to a new one passed in the param
 /// </summary>
 /// <param name="InputLang">New Input Language as InputLanguage object</param>
 public void ChangeInputLanguage(InputLanguage InputLang)
 {
    // Check is this Language really installed. Raise exception to warn if it is not:
     if (InputLanguage.InstalledInputLanguages.IndexOf(InputLang) == -1)
        throw new ArgumentOutOfRangeException();
 
    // InputLAnguage changes here:
     InputLanguage.CurrentInputLanguage = InputLang;
 }
 

VB.NET code:

 Public Sub ChangeInputLanguage(ByVal InputLang As InputLanguage)
    If InputLanguage.InstalledInputLanguages.IndexOf(InputLang) = -1 Then
         Throw New ArgumentOutOfRangeException()
    End If
     InputLanguage.CurrentInputLanguage = InputLang
 End Sub
 

Happy New Year!

Technorati Tags: Globalization

Comments

  • Anonymous
    December 30, 2008
    PingBack from http://www.codedstyle.com/how-to-change-input-language-programmatically/

  • Anonymous
    June 20, 2012
    Can we do it with the web application??and yes then how??

  • Anonymous
    June 30, 2014
    Even after I change input langage input still shows in English. How to input chinese language?

  • Anonymous
    November 19, 2015
    As I understand is changes only input language in Application, not in the operating system