Localization is more than just words on a screen. There are also issues of how you display times, dates, and monetary values to the user. The good news is that these features will work automatically if you globalize your program properly. Just as each .NET program maintains a "user interface culture" (which we played with in the sample program previously), it also has a "general culture" used for string manipulation of times, dates, financial values, and other similar culture-dependent things.
If you use core methods like CDate to extract date values, instead of scanning through a user-entered date string by hand, you get culture-specific date processing for free. Also for output, if you use the predefined formats for the Format method (and other similar string output methods), you get correct culture-specific formatting for no additional effort on your part. Let's try a quick sample that displays money using the local currency.
I'm creating a new Windows Forms application. I'll add the following code to the ApplicationEvents.vb file.
Private Sub MyApplication_Startup(ByVal sender As Object, _ ByVal e As Microsoft.VisualBasic.ApplicationServices. _ StartupEventArgs) Handles Me.Startup If (MsgBox("Switch from English to Japanese?", _ MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = _ MsgBoxResult.Yes) Then My.Application.ChangeCulture("ja-JP") End If End Sub
This code block is almost identical to the one we used in the previous sample, but I'm calling My.Application.ChangeCulture instead of My.Application.ChangeUICulture (the "UI" part is missing). This changes the string-manipulation culture instead of the user interface culture.
Now I'll add the following code to Form1's class.
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load MsgBox(Format("500", "Currency")) End Sub
Figure 18-9 shows the results of this code when run in both English and Japanese modes.
Figure 18-9. Spending money in two places at once
The Framework Class Libraries include even more culture management features in the System.Globalization namespace. The classes in this namespace let you manually adjust the output of culture-sensitive strings to meet your needs. Most of them are pretty esoteric and intended for specific culture groups, so I won't be discussing them here.