2 internationalizing wpf and silverlight applications guy smith-ferrier technical architect, capella...

41

Post on 15-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com
Page 2: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

2

Internationalizing WPF And Silverlight ApplicationsGuy Smith-FerrierTechnical Architect, Capella Software Ltd.Email: [email protected]

Page 3: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

3

About…

Author of .NET Internationalization– Visit http://www.dotneti18n.com to download

the complete source code

The .NET Developer Network– http://www.dotnetdevnet.com– Free user group for .NET developers,

architects and IT Pros based in Bristol

Page 4: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

4

Agenda

Localizing WPF using .resx filesLocalizing WPF using LocBamlLocalizing WPF using LocBaml and a ResourceDictionaryLocalizing Silverlight using .resx files

Page 5: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

5

Localizing WPF Using .resx Files

Demo

Page 6: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

6

Localizing WPF Using .resx FilesUsing Visual Studio 2008 create a new WPF applicationAdd a button

Add a Resources File (In Solution Explorer, right click WpfApplication1, select Add | New Item, select Resources File) and call it Window1Resources.resx– Add a new resource entry called "Button_1" with a value of

"Hello World"– Set the Access Modifier to Public (using the combo box)

<Button Content="Hello World"/>

Page 7: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

7

In Window1.xaml add a "Resources" namespace and change the button's Content to use a static resource

<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Resources="clr-namespace:WpfApplication1" Title="Window1" Height="300" Width="300"> <Grid> <Button Content="{x:Static Resources:Window1Resources.Button_1}"/> </Grid></Window>

Localizing WPF Using .resx Files(continued)

Page 8: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

8

In Visual Studio copy Window1Resources.resx to Window1Resources.fr-FR.resx– Change the "Button_1" resource value to "Bonjour Le Monde"

In App.xaml.cs add a constructor to set the CurrentUICulture

public App(){ Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;}

Localizing WPF Using .resx Files(continued)

Page 9: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

9

Pros:-– Good tool support for .resx files– Resource granularity is at the resource entry level

Cons:-– Binding to properties requires XAML to be changed– Binding can only be applied to dependency properties (in

XAML)

Localizing WPF Using .resx FilesPros And Cons

Page 10: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

10

LocBamlLocBaml is a sample tool for localizing BAML– “The LocBaml tool is not a production-ready application. It is

presented as a sample that uses some of the localization APIs and illustrates how you might write a localization tool.”

Download from http://msdn.microsoft.com/en-us/library/ms771568.aspx– Unzip the download to a folder– Open Visual Studio and build the project

LocBaml is a wrapper around the BamlLocalizer class

Page 11: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

11

Localizing WPF Using LocBaml

Demo

Page 12: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

12

Localizing WPF Using LocBamlUsing Visual Studio 2008 create a new WPF applicationAdd a button

Open WpfApplication1.csproj using NotePad and add a UICulture element to the first PropertyGroup

<Button Content="Hello World"/>

<UICulture>en-GB</UICulture>

Page 13: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

13

Open AssemblyInfo.cs, uncomment the Neutral ResourcesLanguage attribute and set the culture

In App.xaml.cs add a constructor to set the CurrentUICulture

[assembly: NeutralResourcesLanguage("en-GB", UltimateResourceFallbackLocation.Satellite)]

Localizing WPF Using LocBaml(continued)

public App(){ Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;}

Page 14: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

14

Open a command prompt in the WpfApplication1 folder and execute this line:-

In Visual Studio answer "Yes To All" to reload the changed filesCompile the application

msbuild /t:updateuid WpfApplication1.csproj

Localizing WPF Using LocBaml(continued)

Page 15: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

15

In the command prompt change to the bin\Debug folderCopy LocBaml.exe from the LocBaml folder to the current folder and execute this line:-

In Windows Explorer double click on the new fr-FR.csv file to load Excel

locbaml /parse en-GB/WpfApplication1.resources.dll /out:fr-FR.csv

Localizing WPF Using LocBaml(continued)

Page 16: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

16

Change "Hello World" to "Bonjour Le Monde"File | Save As, set "Save as type" to "CSV (Comma delimited)"Make a directory beneath bin\Debug called "fr-FR"In the command prompt execute this line:-

LocBaml.exe /generate en-GB/WpfLocBaml1.resources.dll /trans:fr-FR.csv /out:fr-FR /culture:fr-FR

Localizing WPF Using LocBaml(continued)

Page 17: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

17

LocBaml CSV ColumnsColumn Description

BAML Name The name of the BAML resource stream

Resource Key The resource entry identifier

Localization Category The category

Readable Should the resource be visible to the localizer

Modifiable Should the resource be modifiable by the localizer

Localization Comments Comments from the developer to the localizer

Value The value of the resource entry

Page 18: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

18

Localization CommentsLocalization comments are added to the XAML

<Button x:Uid="Button_1" Content="Hello World" ToolTip="Click to see the world" Localization.Comments="$Content (The text shown in the button) $ToolTip (The text shown over the button)"/>

Page 19: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

19

Localization comments can be extracted/included by adding a LocalizationDirectivesToLocFile to the .csproj

Comments can be extracted to a separate file

<LocalizationDirectivesToLocFile>All</LocalizationDirectivesToLocFile>

Localization Comments(continued)

<LocalizableAssembly><LocalizableFile Name="Window1"> <LocalizationDirectives Uid="Button_1" Comments="$Content (The text shown in the button) $ToolTip (The text shown over the button)" /></LocalizableFile></LocalizableAssembly>

Page 20: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

20

LocBaml Equivalents For ASP.NET Developers

ASP.NET LocBaml

Generate tags in source code Tools | Generate Local Resources msbuild /t:updateuidlocbaml /parse

Tag Identifier meta:resourcekey x:Uid

Source Of Localized Resources .resx file .csv file

Build Satellite Assemblies Build (in Visual Studio) locbaml /generate

'Prevent' Localization meta:localize Localization.Attributes

Page 21: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

21

Pros:-– Localization can be performed without the original source– Localization does not need to be part of the development process– Binding to properties requires no change to the XAML– Bind to any property

Cons:-– Poor tool support– Resource granularity is at the BAML resource level

– No effective resource fallback– LocBaml overwrites existing CSV files– LocBaml does not sign generated assemblies– LocBaml fails to generate correct CSV files when resources contain commas

Localizing WPF Using LocBamlPros And Cons

Page 22: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

22

In Visual Studio select Tools | External Tools…, click Add– Set Title to "msbuild /t:updateuid"– Set Command to"C:\Windows\Microsoft.NET\Framework\

v3.5\MSBuild.exe"– Set Arguments to "/t:updateuid $(ProjectFileName)"– Set Initial directory to "$(ProjectDir)"– Check "Use Output window"– Click OK

Integrating LocBaml With Visual StudioIntegrating "msbuild /t:updateuid"

Page 23: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

23

In Visual Studio select Tools | External Tools…, click Add– Set Title to "locbaml /parse"– Set Command to"c:\CS35Tests\WpfApplication1\bin\debug\

locbaml.exe"– Set Arguments to "/parse en-GB/$

(TargetName).resources.dll /out:fr-FR.csv"– Set Initial directory to "$(BinDir)"– Check "Use Output window"– Click OK

Integrating LocBaml With Visual StudioIntegrating "locbaml /parse"

Page 24: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

24

In Visual Studio select Tools | External Tools…, click Add– Set Title to "locbaml /generate"– Set Command to"c:\CS35Tests\WpfApplication1\bin\debug\

locbaml.exe"– Set Arguments to " /generate en-GB/$

(TargetName).resources.dll /trans:fr-FR.csv /culture:fr-FR /out:fr-FR "

– Set Initial directory to "$(BinDir)"– Check "Use Output window"– Click OK

Integrating LocBaml With Visual StudioIntegrating "locbaml /generate"

Page 25: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

25

Localizing WPF Using LocBaml And Resource Dictionaries

Demo

Page 26: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

26

Localizing Using LocBaml And DictionariesAdd a resource dictionary called Window1Resources

Add the dictionary to Window1.xaml:-

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"> <system:String x:Key="Button_1Message">Hello</system:String></ResourceDictionary>

<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Window1Resources.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary></Window.Resources>

Page 27: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

27

Add a click event to the button to show the resource

Follow the previous steps to add uids, parse out the resources, modify the CSV and rebuild the assembly

MessageBox.Show(this.FindResource("Button_1Message").ToString());

Localizing Using LocBaml And Dictionaries(continued)

Page 28: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

28

Right To Left SupportRight to left behaviour is supported through the FrameworkElement.FlowDirection property– Values can be LeftToRight or RightToLeft– Elements inherit their FlowDirection from their container– Images never inherit their FlowDirection

To set the FlowDirection programmatically:-public Window1(){ InitializeComponent(); if (Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft) { Window.SetFlowDirection(this, FlowDirection.RightToLeft); }}

Page 29: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

29

Localizing Silverlight Using .resx Files

Demo

Page 30: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

30

Localizing Silverlight Using .resx FilesUsing Visual Studio 2008 create a new Silverlight appAdd a button

Add a Resources File (In Solution Explorer, right click WpfApplication1, select Add | New Item, select Resources File) and call it PageResources.resx– Add a new resource entry called "Button_1" with a value of

"Hello World"– Set the Access Modifier to Public (using the combo box)– Change the constructor to public in PageResources.Designer.cs

<Button Content="Hello World"/>

Page 31: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

31

In Page.xaml add a "Resources" namespace, a static resource and change the button to use a static resource

<UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Resources="clr-namespace:SilverlightApplication1" Width="400" Height="300"> <UserControl.Resources> <Resources:PageResources x:Name="PageResources"/> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <Button Content="{Binding Button_1, Source={StaticResource PageResources}}"/> </Grid></UserControl>

Localizing Silverlight Using .resx Files(continued)

Page 32: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

32

In Visual Studio copy PageResources.resx to PageResources.fr-FR.resx– Change the "Button_1" resource value to "Bonjour Le Monde"

Open SilverlightApplication1.csproj using NotePad, locate the SupportedCultures element and set it to fr-FR

<SupportedCultures>fr-FR</SupportedCultures>

Localizing Silverlight Using .resx Files(continued)

Page 33: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

33

Open SilverlightApplication1TestPage.aspx and add InitParameters to the Silverlight control

<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApplication1.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" InitParameters="UICulture=fr-FR" />

Localizing Silverlight Using .resx Files(continued)

Page 34: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

34

In App.xaml.cs change the Application_Startup method

Run the application

Localizing Silverlight Using .resx Files(continued)

private void Application_Startup(object sender, StartupEventArgs e){ string cultureName = e.InitParams["UICulture"].ToString(); if (! String.IsNullOrEmpty(cultureName)) Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);

this.RootVisual = new Page();}

Page 35: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

35

WPF I18N Features Not Available In Silverlight

Static markup extension is not supportedFrameworkElement.FlowDirection is not supportedCustom cultures and Windows-only cultures are supported– The cultures must exist on the client (not the server)– CultureAndRegionInfoBuilder not supported

Numerous properties and methods missing from System.Globalization classes (CultureInfo, RegionInfo, TextInfo etc.)

Page 36: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

36

SummaryThere is no 'one true path' for localizing WPF/SilverlightThe choices are:-– .resx files– XAML (LocBaml)– custom solution

Tool support for post-build localization (i.e. LocBaml) will improveThere is no resource provider model in WPF/SilverlightGlobalization support in WPF is excellent

Page 37: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

37

Q&A

Page 38: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

38

Resources

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/expression

??? ???

http://msdn.microsoft.com

Developer’s Kit, Licenses, and MORE!

Page 39: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

Breakout Sessions (session codes and titles)

Interactive Theater Sessions (session codes and titles)

Hands-on Labs (session codes and titles)

Hands-on Labs (session codes and titles)

Related Content

Page 40: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

Track ResourcesResource 1

Resource 2

Resource 3

Resource 4

Page 41: 2 Internationalizing WPF And Silverlight Applications Guy Smith-Ferrier Technical Architect, Capella Software Ltd. Email: guy@guysmithferrier.com

41

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED

OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.