Virtual Developer Workshop: Containerized Development with Docker
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
With Globalization, you can make your application useable with any culture, irrespective of their language.
Here are some more articles on Globalization:
Localization allows you to customize your application according to a certain culture. Let's do a small project!
Design your application to resemble Figure 1.
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).
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:
I have attached a working sample along with this project for you to play with.
Thank you for reading my article. Until we meet again, this is me signing off. Bye!