×
Namespaces

Variants
Actions
(Difference between revisions)

Localized XAML content using ResourceDictionary - Wp7 to Wp8 Migration

From Nokia Developer Wiki
Jump to: navigation, search
Aady (Talk | contribs)
(Aady -)
Aady (Talk | contribs)
(Aady -)
Line 1: Line 1:
[[Category:Draft]]
+
[[Category:Windows Phone 8]][[Category:Windows Phone 7.5]][[Category:Windows Phone]]
 
{{Abstract|This article explains how to use ResourceDictionary for text localization and then we will see how to convert the code to WP8. }}  
 
{{Abstract|This article explains how to use ResourceDictionary for text localization and then we will see how to convert the code to WP8. }}  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->

Revision as of 19:58, 22 January 2013

This article explains how to use ResourceDictionary for text localization and then we will see how to convert the code to WP8.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: Aady ()
Last edited: Aady (22 Jan 2013)

Contents

Introduction

Localization is the customization of applications for a given culture or locale. ResourceDictionary is a hash table / dictionary implementation that contains objects (resources) which can be used in XAML or codebehind.

You might want to make the app localized compatible and for which you can define the string text values as resources and load the required one instead of hardcoding the text to control. Example: for a textblock control you might want to load the localized string as text and this can be done dynamically using ResourceDictionary.

In this article we will see how to use ResourceDictionary for text localization and then we will see how to convert the code to WP8.

Implementing ResourceDictionary in WP7

To implement a ResourceDictionary in WP7, follow the below steps: Create a .cs file Change the file extension to .xaml (example: resourceDictionary.xaml for further reference) Add reference to it in App.xaml Ready to use in application. Add the below code:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 
<Style x:Key="tbStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="30"/>
</Style>
<sys:String x:Key="demoApp">Localized App</sys:String>
 
</ResourceDictionary>
<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="resourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<TextBlock x:Name="ApplicationTitle" Style="{StaticResource tbStyle}" 
Text=”{ StaticResource demoApp }”/>

Conversion to WP8

It’s very simple, WP8 has an additional element called myNamespace:LocalizedStrings. Just add the line: <myNamespace:LocalizedStrings x:Key="LocalizedStrings"/> , after ResourceDictionary.MergedDictionaries element.

<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="resourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<myNamespace:LocalizedStrings x:Key="LocalizedStrings"/>
</ResourceDictionary>
</Application.Resources>

Summary

Thus we see how easy is to make and app localized using ResourceDictionary and even more easy to make it WP8 compatible.

For additional info, please refer:
How to: Make XAML Content Localizable

173 page views in the last 30 days.
×