Switching Input Languages Dynamically with Visual Basic

Introduction

Hello and welcome to today’s article. Today, I will show you how to change the input language dynamically.

First, the complicated stuff.

Globalization versus Localization

Globalization

Globalization is basically the process of making your application culture-friendly. You have to realize that the users of your application may speak a different language than you, and therefore their language adheres to different rules, apart from being a different language. For example, some languages are displayed left to right, while others should be displayed right to left. Now, you have to not only think about the different language, but also different currencies, date and time settings, as well as different colours being used with different cultures.

With Globalization, you can make your application useable with any culture, irrespective of their language.

Here are some more articles on Globalization:

Localization

Localization allows you to customize your application according to a certain culture. Let’s do a small project!

Design

Design your application to resemble Figure 1.

Design
Figure 1: Our Design

Set the Tag property of each of the Textboxes to languages of your choice. I chose English and Afrikaans (which is my native language).

Code

As usual, add the Declarations first:

Private Const _ENGLISH As String = "ENGL"
Private Const _AFRI As String = "AFRI"

These constants will help identify which languages we want to use. Obviously, you may use your own languages.

Add the following piece of code to dynamically change your input type to a different language, once the focus has been set to either one of the textboxes:

'Method to handle GotFocus events for both textboxes

Private Sub TextBoxes_GotFocus(ByVal sender As Object, _
   ByVal e As System.EventArgs) _
   Handles txtNameEnglish.GotFocus, txtNameAfri.GotFocus

   'We wll use the case statement to inspect the sender object's .Tag property.

   Select Case sender.Tag
      Case _AFRI   'If the Tag contains the word "AFRi"

      'Loop through all the installed languages on this system.
      For Each Lng As InputLanguage In _
         InputLanguage.InstalledInputLanguages

         'If there exists a language whose DisplayName has the
         'word "AFRI" in it

         If Lng.Culture.DisplayName.ToUpper.StartsWith(_AFRI) Then

            'Change current input language to that

            InputLanguage.CurrentInputLanguage = Lng

            'Exit for - coz we have found our language and no need
            'to go through the rest of the loop

            Exit For

         End If

      Next

   Case _ENGLISH   'If the Tag contains the word "ENGL"

      'Loop through all the installed languages on this system

       For Each Lng As InputLanguage In _
         InputLanguage.InstalledInputLanguages

         'If there exists a language whose DisplayName has the
         'word "ENGL" in it

         If Lng.Culture.DisplayName.ToUpper.StartsWith(_ENGLISH) Then

            'Change current input language to that

            InputLanguage.CurrentInputLanguage = Lng

            'Exit for - coz we have found our language and no need
            'to go through the rest of the loop

            Exit For

         End If

      Next

   End Select

End Sub

All that happens here is the following:

  • We make use of the GotFocus event for the TextBoxes. You could actually have used the LostFocus event as well, depending on when you want this to fire. Notice the Handles clause handles both textboxes.
  • I determine the current object’s Tag with the use of a Select Case statement.
  • I started a For Loop to loop through all of the installed languages on the machine.
  • With a little string manipulation, I determine what the current installed language starts with. If it starts with either of our languages, then,
  • Make use of InputLanguage to change the language dynamically.

Here is some more information on InputLanguage:

Here is some more information the CultureInfo object:

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.110%29.aspx

I have attached a working sample along with this project for you to play with.

Conclusion

Thank you for reading my article. Until we meet again, this is me signing off. Bye!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read