Localizing a Windows Phone app Step by Step
published on: 11/15/2011 | Views: N/A | Tags: Localization
by WindowsPhoneGeek
In this article I am going to talk about how to localize a Windows Phone application.
Basically you need to separate localizable resources from the rest of the code by creating language-specific resource files. Visual Studio uses these files to create assemblies that allow your application to support many languages.
For reference take a look at the MSDN documentation.
Getting Started
Step1: Create a Windows Phone 7 application project.
Step2: Add a resource file for the default language of your application. In our case we will add a resource file named AppResources.resx
Step3: Open the resource file and enter the strings that you want to be localized in the resource file.
NOTE: It is important to consider the following requirements while editing strings in the resource editor:
-
The name must be unique. Make it as descriptive as possible.
-
The value is the string that will be displayed to the user in the application.
-
The comment is optional, but it is helpful for translators, especially in large resource files with many strings.
Step4: Copy the resource file once for each additional language that you want your app to support. Note that each resource file must contain the correct culture/language name, as described in Culture and Language Support for Windows Phone. In our case we will use the following name of the file AppResources.de-DE.resx (for the culture German (Germany)). The general resource file name format is :
<name of main resource>.<culture name>.resx
Step5: Translate the strings in the additionally added files.
Step6: Edit the project file to define the additional cultures / languages the application will support. To do this first unload the project file by pressing the mouse right button on the project and then select "Unload Project":
After that again press the mouse right button on the project and select "Edit" from the context menu:
Finally you will need to add the names of the additional cultures in the project file the following way: <SupportedCultures>de-DE;</SupportedCultures>
NOTE: If you have more than one additional culture you can include them in this way (note that semicolon is used to separate the culture names): <SupportedCultures>de-DE;es-ES;</SupportedCultures>
Step7: Make sure that the Access Modifier of all resource files(including the default one) is set to Public!
Step8: Create a new class that will have a property exposing the resources:
public class LocalizedStrings
{
public LocalizedStrings()
{
}
private static AppResources localizedResources = new AppResources();
public AppResources AppResources
{
get { return localizedResources; }
}
}
In the example above AppResources is the name of the class generated automatically for the resource files:
Step9: Open the App.xaml file and add the following code:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:WPLocalization" x:Key="LocalizedStrings" />
</Application.Resources>
Step10: Whenever you want to show a localized string in your app you will have to bind to a specific property of the global resource that we have just defined. The property names correspond to the resource names in the resource files. Example:
<TextBlock x:Name="PageTitle"
Text="{Binding Path=AppResources.Title, Source={StaticResource LocalizedStrings}}"
Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}"/>
To test the localization you will have to change the Display Language of the emulator in this way:
Here is the result:
That was all about how to localize a Windows Phone application. You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
:D
posted by: Alfah on 11/15/2011 3:38:43 PM
When you said you would be doin an article on localization, din expect it this fast :)
good goin.
Well done
posted by: Casper Skovgaard on 11/25/2011 12:59:22 PM
Only thing that didn't worked for me was step 6. I don't have the option to unload project. But I just followed the step on msdn and it worked. And now I have a localized app :-)
Now a couple of questions:
Would it be possible to publish an app with several names depending on the local marketplace. So in English marketplace name of app is "My local..." and on German marketplace it would be "Meine lo..."?
Is it possible to overwrite Display Language from settings, for example with language selected from a setting stored in the app?
Thanks.
/Casper
RE: @Casper Skovgaard
posted by: winphonegeek on 12/6/2011 11:50:17 AM
- You can localize the application title that appears in the application list and also on the application tile when the user pins your app to start. More information about localizing the app title you can find here: http://msdn.microsoft.com/en-us/library/ff967550(v=vs.92).aspx
Unfortunately I cannot tell if the marketplace will show your localized app title for the different countries.
- I am not aware of the existence of API that will allow you to change the Display Settings of the phone.
Cool touch for the article
posted by: jinek@msn.com on 12/27/2011 5:36:51 PM
Create the class: public partial class LocalizedBinding : Binding { public LocalizedBinding() { Source = Application.Current.Resources["LocalizedStrings"]; }
private string _lc;
public string LC
{
get { return _lc; }
set
{
_lc = value;
Path = new PropertyPath(string.Format(@"AppResources.{0}", value));
}
}
}
and you can use it in more simple way:
posted by: Kris on 5/8/2012 12:54:05 AM
Step8: Create a new class that will have a property exposing the resources:
Where?
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
