370 likes | 592 Views
WEB307: How To Make your Silverlight 4 Application World Ready. Guy Smith-Ferrier Technical Architect Email: guy@guysmithferrier.com Twitter: @ GuySmithFerrier Blog: http://www.guysmithferrier.com. Agenda. Localizing Silverlight U sing . resx F iles
E N D
WEB307: How To Make your Silverlight 4 Application World Ready Guy Smith-Ferrier Technical Architect Email: guy@guysmithferrier.com Twitter: @GuySmithFerrier Blog: http://www.guysmithferrier.com
Agenda • Localizing Silverlight Using .resxFiles • Downloading Localized Resources On Demand • Silverlight Installation User Experience • Silverlight UI Localization • Silverlight Fonts and Font Management • Silverlight Globalization
Localizing Silverlight Using .resx Files SilverlightApplication1.sln SilverlightApplication1.xap SilverlightApplication1.csproj AppManifest.xaml MainPage.xaml SilverlightApplication1.dll MainPageResources.resx fr-FR/SilverlightApplication1. resources.dll MainPageResources.fr-FR.resx
Solutions To The Internal Constructor • 1. Use a different Strongly Typed Resource Class code generator • PublicResourceCodeGenerator • http://www.dotneti18n.com/Downloads/ResourceProviderCodeGenerators.zip • ResXFileCodeGeneratorEx • http://dmytro.kryvko.googlepages.com • Visual Studio 2005 & 2008 only (Source code available)
Solutions To The Internal Constructor (continued) • 2. Write a wrapper for the Strongly Typed Resource Class and bind to the wrapper public class PublicMainPageResources { private static MainPageResources resources = new MainPageResources(); public MainPageResourcesMainPageResources { get { return resources; } } } <UserControl.Resources> <Resources:PublicMainPageResources x:Name="MainPageResources"/> </UserControl.Resources> <Button Content="{Binding MainPageResources.Button_1, Source={StaticResourceMainPageResources}}"/>
Download On Demand SilverlightApplication1.sln SilverlightApplication1.xap SilverlightApplication1.csproj AppManifest.xaml MainPage.xaml SilverlightApplication1.dll MainPageResources.resx SilverlightApplication1.fr-FR.xap AppManifest.xaml MainPageResources.fr-FR.resx fr-FR/SilverlightApplication1. resources.dll
Download On DemandCreating The Resource XAP File Manually • Copy bin\debug\AppManifest.xaml to bin\debug\fr-FR • Ensure that the only AssemblyPart is:- • Create a ZIP file, SilverlightApplication1.fr-FR.zip, containing:- • AppManifest.xaml with no path information • SilverlightApplication1.resources.dll with fr-FR path • Rename SilverlightApplication1.fr-FR.zip to SilverlightApplication1.fr-FR.xap • Copy SilverlightApplication1.fr-FR.xap to SilverlightApplication1.Web\ClientBin <AssemblyPart Source= "fr-FR/SilverlightApplication1.resources.dll" />
Download On DemandCreating The Resource Xap File Automatically • Download http://www.guysmithferrier.com/ Downloads/Silverlight.Build.Tasks.zip • Add the following lines to the bottom of SilverlightApplication1.csproj:- • In SilverlightApplication1.csproj add the following line beneath </SupportedCultures>:- <Import Project="$(ProgramFiles)\ MSBuild\I18N\Silverlight\v4.0\Silverlight.Build.Tasks.targets" /> <Target Name="AfterBuild" DependsOnTargets="XapResourcePackager"> </Target> <PackageCultures>fr-FR</PackageCultures>
Download On DemandChanging The Silverlight Application • Empty the SupportedCultures in the .csproj file • Add the following reference to the Silverlight application:- • System.ComponentModel.Composition • System.ComponentModel.Composition.Initialization <SupportedCultures></SupportedCultures>
Download On DemandChanging The Silverlight Application • Change the Application_Startup event to:- private void Application_Startup(object sender, StartupEventArgs e) { string xapResourceFilename = String.Format("{0}.{1}.xap", "SilverlightApplication1", Thread.CurrentThread.CurrentUICulture.Name); DeploymentCatalogcatalog = new DeploymentCatalog( new Uri(xapResourceFilename, UriKind.Relative)); CompositionHost.Initialize(catalog); catalog.DownloadCompleted += (s, args) => { this.RootVisual = new MainPage(); }; catalog.DownloadAsync(); }
Download On Demand demo
Silverlight Installation User Experience • The Silverlight install image is retrieved from http://go.microsoft.com/fwlink/?LinkId=161376 • This reads the browser's HTTP accept language setting and returns an image for that culture
Silverlight Installation User Experience (continued) • The Silverlight add-in is installed using a Windows application • The Windows application uses the operating system's culture (e.g. en-US) to determine the localized resources to display • If you set your browser's language to a different language to your operating you will see a schizophrenic installation experience
Silverlight Runtime Culture Support • Silverlight is a single runtime that includes all 40 supported languages:-
Silverlight Configuration Dialog • The Config dialog's UI is dictated by the operating system (only) • Defaults to English
Silverlight Message & File Dialogs(MessageBox, OpenFileDialog, SaveFileDialog) • These dialogs' UIs are dictated by the operating system (only)
Silverlight Globalized Controls • The DatePicker and Calendar controls respect the Thread.CurrentCulture setting en-GB fr-FR nb-NO ja-JP
Silverlight 'Web Safe' Fonts • Silverlight supports 10 'web-safe' fonts on all platforms • (assuming the fonts have not been uninstalled)
Font Linking and Font Fallback • Silverlight has no 'built-in' fonts • All fonts are supplied by the operating system • The OS uses Font Linking and Font Fallback to find the best match for unknown glyphs • Lucida Sans Unicode on Windows Vista Ultimate • Lucida Sans Unicode on Windows XP SP2 (East Asian support not installed)
Silverlight Default Font • Silverlight uses the "Portable User Interface" font by default • The "Portable User Interface" font is an alias for Lucida Grande
Using Embedded Fonts demo
Using Streamed Fonts demo
Font Subsetting • Open a Silverlight project in Blend • Add a font • Tools | Font Manager • Select Mongolian Baiti (349K) • Check the "Auto fill" checkbox • Add a TextBlock, set the Font Family to the new font and set the Content to "Hello World" • Build the project
Font Subsetting (continued) • Open the subsetted font (obj\Debug\Fonts\monbaiti0.subset.ttf, 101K)
Font SubsettingMSBuild Tasks • When you add a Font to your project Blend adds the following line to the .csproj file:- • This .targets file includes the font subsetting task:- <Import Project="$(MSBuildExtensionsPath)\Microsoft\Expression\Blend\Silverlight\v4.0\Microsoft.Expression.Blend.Silverlight.targets" /> <UsingTaskAssemblyFile="SubsetFontTask.dll" TaskName="Microsoft.Expression.SubsetFontTask.SubsetFontsSilverlight" />
Silverlight Globalization Namespace • Comparison between the .NET Framework and the Silverlight Framework:- • http://www.guysmithferrier.com/downloads/SilverlightGlobalizationClassComparison.pdf • No support for locale IDs (LCIDs) • No lunisolar calendars • No Windows-specific features • CultureInfo.ThreeLetterWindowsLanguageName • RegionInfo.GeoId • No code page support (Unicode only)
Custom Cultures • The CultureAndRegionInfoBuilder class is not supported in Silverlight • Silverlightdoes read custom cultures if they are present on the client's operating system
TextInfo.IsRightToLeft • There is no TextInfo.IsRightToLeft in Silverlight 4 namespace System.Globalization { static class TextInfoExtensions { public static bool IsRightToLeft(this TextInfotextInfo) { CultureInfocultureInfo = new CultureInfo(textInfo.CultureName); while (! (cultureInfo.IsNeutralCulture || cultureInfo.Parent == null)) { cultureInfo = cultureInfo.Parent; } return ( cultureInfo.Name == "ar" || // "Arabic" cultureInfo.Name == "dv" || // "Divehi" cultureInfo.Name == "fa" || // "Persian" cultureInfo.Name == "he" || // "Hebrew" cultureInfo.Name == "prs" || // "Dari" cultureInfo.Name == "ps" || // "Pashto" cultureInfo.Name == "syr" || // "Syriac" cultureInfo.Name == "ug" || // "Uyghur" cultureInfo.Name == "ur" // "Urdu" ); } } }
Right To Left Support • Right 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 MainPage() { InitializeComponent(); if ( Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft()) { this.FlowDirection = FlowDirection.RightToLeft; } }
Other WPF I18N Features Not Available In Silverlight • Static markup extension is not supported • Silverlight'sMarkupExtension is not public • See "Differences Between WPF and Silverlight" • http://wpfslguidance.codeplex.com
Silverlight On Windows vs. Silverlight On Mac OS X • Culture data varies across operating systems • Sort, casing and comparison results vary across operating systems
Summary • Localization is possible through .resx files • Bind to the strongly typed resource classes • If you have more than one culture you should consider "download on demand" • Avoiding a schizophrenic user interface can be a challenge • Font support is dependent upon the operating system • Globalization support is dependent upon the operating system
Session Evaluations Tell us what you think, and you could win! All evaluations submitted are automatically entered into a daily prize draw* Sign-in to the Schedule Builder at http://europe.msteched.com/topic/list/ * Details of prize draw rules can be obtained from the Information Desk.
© 2010 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.