Switching Input Languages Dynamically with Visual Basic

Monday Mar 9th 2015 by Hannes Du Preez
Share:

You can change your input language dynamically. Make it easier for those who speak other languages to use your software.

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!

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved