To
see how you can go about preparing an application for the world, you'll
build a simple application that announces a new product, in this case a
Windows Phone. But first, you will learn how to use the CultureInfo
class to ensure dates, numbers, and text appear in the right form
regardless of the culture in which the announcement is made. Then you'll
see how, by using resource (.resx) files, you can easily add translated content to your app to reach new markets.
Let's jump into code that will set the stage for our discussion of internationalization of Windows Phone 7 applications.
Let's start by creating a new project inside Visual Studio and naming it InternationalizationSample.
By default, the MainPage.xaml file is created in the application, with the designer and XAML windows open and ready to program.
Double-click MainPage.xaml to bring up the XAML designer window. For convenience and simplicity, we will alter the content of textblocks in the TitleGridTitleGrid look identical to the XAML here. block. Make the XAML of the
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="Current Culture Setting" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="culture" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
Your project should now look like Figure 12-1. So far you've simply changed the text of the default title textblock in the Windows Phone 7 application.
Now double-click MainPage.xaml.cs to bring up the code view. Alternately, you can right-click the MainPage.xaml file and select "View Code."
Add the following statement to the top of the MainPage.xaml.cs file:
using System.Globalization;
Paste the following code in the MainPage() constructor:
PageTitle.Text = CultureInfo.CurrentCulture.ToString();
Press F5 to run the application. .
This caption represents two parts of the current Culture
setting on Windows Phone 7. The first part—"en"—states that the English
language is the current language on this Windows Phone 7 device (or
device emulator in our case) and it is a part of an ISO standard to
represent culture code associated with the language. The second
part—"US"—represents that the current locale is the United States, and
indicates that dates, currency, and other region-specific items should
be shown in the format that is native to people in the United States.
That part is an ISO standard as well, to represent a subculture code
associated with a country or region.
A concept of culture in .Net
Framework refers to a set of user preferences specific to the user,
such as dates, currency, and calendar format. Besides CurrentCulture, the CultureInfo
class contains many properties that may be of interest to you as you
internationalize your applications; you can find the full list here: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo_properties.aspx. For instance, we could have used the DisplayName
property to show a friendlier description of the current culture (in
our case, we would get "English (United States)." As has already been
mentioned, there is a lot of material to cover when it comes to
internationalization—a good place to refer to for more information is
MSDN documentation of the System.Global namespace, which can be found here: http://msdn.microsoft.com/en-us/library/abeh092z.aspx.
You might think that the
locale setting is of minor importance, yet it is extremely important to
properly localize your application: in England, for instance, people
speak English (certainly!), but the numeric date format is "dd/mm/yyyy,"
where the "dd" is the numeric representation of the day, "mm" is the
numeric representation of the month, and "yyyy" is the numeric
representation of the year. Compare this to the United States, where
people speak English as well, but for numerical date representations,
the month comes first! It would be very time-consuming to keep track of
all possible localization issues that may arise. In the end, we are also
very likely to make mistakes. It is much easier to stand on the
shoulders of giants who have thought through many internationalization
issues and have made standard libraries and functions available for our
use. Our main task is to make sure to use those libraries.