Click here to Skip to main content
15,891,431 members
Articles / Mobile Apps / Windows Phone 7
Technical Blog

Localize Windows Phone 7 Styles

Rate me:
Please Sign up or sign in to vote.
4.93/5 (4 votes)
19 Jun 2013CPOL1 min read 4.6K   2   1
Localize Windows Phone 7 Styles.

Introduction

In the process of localizing a Silverlight application, usually, localized text is wider than the original English text. In most cases this is not a problem if you use a StackPanel or other wrapping controls, but sometimes width cannot be easily changed. In a Silverlight application you can use a satellite assembly and put localized styles, and connect to a page using MergedDictionary and ResourceDictionary.

Why cannot we use the same approach for Windows Phone applications? There are many reasons:

  1. Windows Phone doesn't support satellite assemblies.
  2. Windows Phone doesn't care about xml:lang.
  3. MergedDictionary degrades performance.

So try to use another approach. Define a control, a Button for example and apply the default style:

XML
<Button x:Name="MyStyledButton" Content="Button" Style="{StaticResource MyButton}"/>

Then put a general style in App.xaml:

XML
<Style x:Key="MyButton" TargetType="Button">  
    <Setter Property="Background" Value="#303030"></Setter>  
    <Setter Property="BorderThickness" Value="0"></Setter>  
</Style>

Another bug in Windows Phone 7 that attribute BasedOn not supported, so we have to copy whole style to derived. Next step change name, adding -[lang code] to x:Key of style. So 'MyButton' will be 'MyButton-de' for example.

XML
<Style x:Key="MyButton-ru" TargetType="Button">  
    <Setter Property="Background" Value="#FF8080"></Setter>  
    <Setter Property="BorderThickness" Value="1"></Setter>  
</Style>

Next we'll add runtime code for loading language dependent styles.

C#
string lang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;  
// load localized style  
Style style = (Style)App.Current.Resources["MyButton-" + lang];  
if (style != null)  
{  
    MyButton.Style = style;  
}

At runtime we determine the current UI language and try to load the altered style. If found, apply the new style, if not - the hardcoded style will be used.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA13-Jul-13 20:48
professionalȘtefan-Mihai MOGA13-Jul-13 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.