Delay's Blog is the blog of David Anson, a Microsoft developer who works with C#, XAML, HTML, and Azure.
This blog has moved to a new location and comments have been disabled.
All old posts, new posts, and future comments can be found on The blog of dlaa.me.
See you there!
While it might be convenient if everybody spoke the same language (or communicated via telepathy), that's not the world we live in. :) Therefore, building applications that can be easily translated to other languages is an important consideration. Fortunately, it's easy - and it's covered in the MSDN article How to: Build a Localized Application for Windows Phone. But what's not covered is how to localize the controls in the Silverlight for Windows Phone Toolkit. As you might expect, it's fairly similar, but I've had a few people ask about this explicitly and I decided to do a quick post on the topic. As long as I was at it, I figured I'd show the entire process from start to finish just to make things a little easier...
Prepare the emulator/phone
In order to monitor our progress as the sample application gets localized, it's helpful to work in a non-default language which makes it easy to identify the un-localized parts. I run an English operating system, so I'll use Spanish for this example:
Open the Settings application
Choose region & language
Open the Display language picker
Change to "Español"
Tap the link to restart the emulator/phone:
Create a new application
We'll start with a brand new application using the default template provided by the Visual Studio 2010 development tools:
From the File menu, choose New, then Project...
Go to the "Silverlight for Windows Phone" section and create a "Windows Phone Application" with the name of your choice
From the Project menu, choose Add New Item...
Select "Resources File" and name it AppResources.resx
Add an entry for Name="Title" and Value="welcome"
Open MainPage.xaml.cs and add the following to the end of the constructor to set the text of the existing PageTitle element:
PageTitle.Text = AppResources.Title;
Run the application to see the custom title in English:
Localize the application
Now let's localize the sample application so it uses the proper language for the user's settings. In the steps below, we'll add support for Spanish (via the "es" language code), but adding other languages is a simple matter of repeating these steps using the other language's code and translations. It's a little bit of effort, but it's all quite simple:
In the Solution Explorer window, select AppResources.resx by clicking on it
Press Ctrl+C, then Ctrl+V to create a copy of AppResources.resx
Select Copy of AppResources.resx and press F2 to rename it to AppResources.es.resx
Copy of AppResources.resx
Staying in the Solution Explorer window, right-click the project root node and choose Unload Project
Right-click the project node again and choose Edit [ProjectName].csproj
Change the existing SupportedCultures element to be:
Back in the Solution Explorer window, right-click the project node and choose Reload Project
Open AppResources.es.resx and change "welcome" to "bienvenido"
Run the application and verify the custom title now shows up in Spanish (no code changes necessary!):
Add the Windows Phone Toolkit project
Now we'll modify the sample application to reference the Windows Phone Toolkit. (You can read more about the Windows Phone Toolkit in my introductory post.) Rather than adding a binary reference to the Toolkit, we'll add a project reference and build the Toolkit code as part of the sample application (for reasons that will become clear soon):
Go to http://silverlight.codeplex.com/ and download the "Silverlight for Windows Phone Toolkit Source & Sample" ZIP file
Unblock the ZIP file (see the notes at the end of this post for instructions)
Right-click the ZIP file and choose Extract All... to extract all files to the directory of your choice
From the File menu in Visual Studio, choose Add, then Existing Project...
Choose the Microsoft.Phone.Controls.Toolkit.csproj file found in the same-named directory of the extracted content
Click on the application project node in Solution Explorer to make it active again
From the Project menu, choose Add Reference...
Switch to the Projects tab and pick the Microsoft.Phone.Controls.Toolkit project
Press F6 and verify the solution builds both projects successfully:
------ Build started: Project: Microsoft.Phone.Controls.Toolkit, Configuration: Debug Any CPU ------
------ Build started: Project: LocalizedPhoneApplicationWithToolkit, Configuration: Debug Any CPU ------
========== Build: 2 succeeded or up-to-date, 0 failed, 0 skipped ==========
Add some Windows Phone Toolkit controls
Now it's time to add two of those handy-dandy Windows Phone Toolkit controls (both covered in the aforementioned blog post): DatePicker and ToggleSwitch. We'll follow the usual steps for referencing third-party controls which should be pretty familiar to everyone:
Open MainPage.xaml and switch to XAML view
Add the following to the top of the file along with the other xmlns definitions:
Paste the following inside the empty "ContentPanel" Grid:
Optional: Follow the steps in the notes of my Windows Phone Toolkit introduction to add the DatePicker Application Bar icons to the project
Run the application to see the text of the Toolkit controls in English:
Note: The day and month names are already in Spanish because they come from the operating system which knows to use the current language for dates. The Spanish-correct day/month/year formatting (vs. the United States default of month/day/year) is automatically provided by the DatePicker control.
Localize the Windows Phone Toolkit controls
Finally, we'll localize the Toolkit controls using the same process we did for the sample application itself:
In Solution Explorer, expand the "Properties" folder for the Toolkit and click Resources.resx
Press Ctrl+C, then Ctrl+V to create a copy of Resources.resx
Select Copy of Resources.resx and press F2 to rename it to Resources.es.resx
Copy of Resources.resx
Open Resources.es.resx and change "CHOOSE DATE" to "ELIGE UNA FECHA", "cancel" to "cancelar", "done" to "listo", "Off" to "Desactivado", and "On" to "Activado"
Run the application and verify the Toolkit control text is also in Spanish now:
Note: If you get the error Unable to copy file "obj\Debug\Microsoft.Phone.Controls.Toolkit.dll" to "..\Bin\Debug\Microsoft.Phone.Controls.Toolkit.dll". The process cannot access the file '..\Bin\Debug\Microsoft.Phone.Controls.Toolkit.dll' because it is being used by another process. when compiling, close all documents in Visual Studio via Window/Close All Documents, then restart Visual Studio itself.
Unable to copy file "obj\Debug\Microsoft.Phone.Controls.Toolkit.dll" to "..\Bin\Debug\Microsoft.Phone.Controls.Toolkit.dll". The process cannot access the file '..\Bin\Debug\Microsoft.Phone.Controls.Toolkit.dll' because it is being used by another process.
At the end of the day, translating an application isn't just a nice thing to do, it's good business! We operate in a global marketplace and that means localized products can have a big advantage over their single-language-only competitors. While good localization can't save a bad application from itself, it can make a good application stand out. So please think about localization in your next marketplace application - your customers will appreciate it!
People sometimes ask why the Windows Phone Toolkit (or the Silverlight Toolkit, for that matter) isn't already localized to the same set of languages the host platform supports. While I personally think it should be, doing so would take time and money and those two things can be in rather short supply at times. :) While I hope to see Toolkit localization become official some day, the good news for now is that things have been implemented such that they're easily localizable.
I've taken the quick/easy route of assigning the localized text resource directly to PageTitle.Text in the application's constructor, but that is not the recommended technique. Instead, what's generally preferred is what's described in the second half of the "Replacing hard-coded strings with strings in a resource file" section of the MSDN documentation: use a property Binding in XAML to reference the localized resources via a custom object as a StaticResource. It's a little unintuitive at first, but a very reasonable solution in practice. :)
After downloading the ZIP file for the Windows Phone Toolkit source code, you should "unblock" it before extracting its contents to avoid warnings from Visual Studio like Security Warning, You should only open projects from a trustworthy source. and The "ValidateXaml" task failed unexpectedly. System.IO.FileLoadException: Could not load file or assembly .... Unblocking is a simple matter of right-clicking on the ZIP file, choosing Properties from the context menu, clicking the Unblock button in the lower, right-hand corner of the resulting dialog, and hitting OK or Apply:
Security Warning, You should only open projects from a trustworthy source.
The "ValidateXaml" task failed unexpectedly. System.IO.FileLoadException: Could not load file or assembly ...