660 likes | 777 Views
DEV392 .NET Framework: Building Applications With Globalization In Mind. Michele Leroux Bustamante Principal Software Architect IDesign Inc. Speaker BIO. Principal Software Architect, IDesign, www.idesign.net Microsoft Regional Director, www.microsoft.com/rd
E N D
DEV392 .NET Framework:Building ApplicationsWith Globalization In Mind Michele Leroux Bustamante Principal Software Architect IDesign Inc.
Speaker BIO • Principal Software Architect, IDesign, www.idesign.net • Microsoft Regional Director, www.microsoft.com/rd • Microsoft MVP – XML Web Services • INETA Speaker’s Bureau • BEA Technical Director • Web Services Program Advisor, UCSD • Blog: www.dasblonde.net
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
What Is Globalization? • Designing applications to be adapted to the global marketplace • Affects display, input, formats & conventions • Localizability • Separate culture dependencies from code • Localization • Adapting applications for a particular market • Content translation, feature customization • Changes independent of compiled source
Locale/Culture • Language and cultural environment • International Standards Organization (ISO) Conventions • Language Code - ISO 639-1 • I.e., en, fr, de, es • Country Code - ISO 3166 • I.e., CA, US, EC, ES • Together, they identify culture • I.e., en-CA, en-US, es-EC, es-ES • See RFC 3066 – Tags for identifying languages
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
What To Localize? • Static Application Content • Usually “hard-coded” for presentation • For example • Menu captions • Control captions • Labels, buttons, selection options, list options • Web page content • *.aspx, *.ascx, *.html, *.xml
What To Localize? • Dynamic Application Content • Content populated at runtime from XML files, databases, and other data sources • Need location of content to be organized by culture • Need filenames, connection strings and queries to be configurable by culture
Storing Localized Content • Deployment architecture is affected by location of localized application content • Resources, file system, database,external applications • Choices influenced by application type • Windows versus Web • And by business requirements • Frequency of culture translations • Ease of translation process • Frequency of application maintenance
Windows Forms Resources Main Assembly Application Directory Satellite Assemblies Culture-Specific Subdirectories GlobalApp.Resources.dll \en-CA GlobalApp.exe Form1 Common Internal Resources Code Form1 Common GlobalApp.Resources.dll Internal Types \es Internal Form1 Common GlobalApp.Resources.dll \es-EC Form1 Common Internal
Windows Forms Resources and data Main Assembly Application Directory Satellite Assemblies Culture-Specific Subdirectories \en-CA GlobalApp.Resources.dll resources GlobalApp.exe \es GlobalApp.Resources.dll resources resources \es-EC GlobalApp.Resources.dll resources Database Content Products Products_en-CA Products_es
ASP.NET Duplicate Content Web Application Resources GlobalWeb \bin GlobalWeb.dll Global.asax Web.config page and user control content Database Content logo.gif Products
ASP.NET Duplicate Content Web Application Resources GlobalWeb \bin GlobalWeb.dll Global.asax Web.config translated page and user control content page and user control content \en-CA Database Content logo.gif logo.en-CA.gif \es logo.es.gif Products Products_en-CA Products_es
ASP.NET Single Content Base Web Application Resources Satellite Assemblies GlobalWeb \bin \en GlobalWeb.en.dll Global.asax GlobalWeb.dll resources Web.config \en-CA resources GlobalWeb.en-CA.dll resources \es GlobalWeb.es.dll resources logo.gif Database Content logo.en-CA.gif logo.es.gif translated file content list.en-CA.xml Products list.es.xml Products_en-CA Products_es
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
What Are .NET Resources? • XML Resources (*.resx) • Automatically generated by VS.NET for each Windows Form and Web Form • By default, compiled and embedded in project assembly • Can add custom resources tothe project • Can build assemblies that contain shared resources
How Are Resources Created? • Localizable = false this.button1.Location = new System.Drawing.Point(88, 112); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1";
How Are Resources Created? • Localizable = true this.button1.Location = ((System.Drawing.Point) (resources.GetObject("button1.Location"))); this.button1.Name = "button1"; this.button1.Size = ((System.Drawing.Size) (resources.GetObject("button1.Size"))); this.button1.TabIndex = ((int)(resources.GetObject("button1.TabIndex"))); this.button1.Text = resources.GetString("button1.Text"); this.button1.TextAlign = ((System.Drawing.ContentAlignment) (resources.GetObject("button1.TextAlign"))); this.button1.Visible = ((bool)(resources.GetObject("button1.Visible")));
How Are Resources Created? • (Default) Culture • Main assembly • Neutral Culture • Spanish (es) • Specific Culture • Spanish (Ecuador) (es-EC) • Spanish (Mexico) (es-MX) • Triggers satelliteassemblygeneration
Satellite Assemblies • Defined for each language and culture • Hub and spoke model • Contain only resources - No code! • Individual version updates • No impact to main executable • Directory structure andnaming conventions • Runtime follows a resource fallback process to find closest match
Satellite AssembliesResource Deployment Main Assembly Application Directory Satellite Assemblies Culture-Specific Subdirectories GlobalApp.Resources.dll \en-CA GlobalApp.exe Form1 Common Internal Resources Code Form1 Common GlobalApp.Resources.dll Internal Types \es Internal Form1 Common GlobalApp.Resources.dll \es-EC Form1 Common Internal
Compiling Resources ColorPicker.exe ColorPicker.Resources.dll \en Resources Code Form1 Form1 Strings Strings Types CommonRes.Resources.dll \en-CA CommonRes.Resources.dll Errors Errors ColorPicker.Resources.dll Form1 Strings \es CommonRes.Resources.dll Errors
Resource Source • Resource source files • Text (*.txt) – string resources • XML (*.resx) – string or binary resources • Default XML resources provided for Windows and Web Forms • Can manually add custom resources • VS.NET automatically compiles satellite assemblies • [ResourceType].[Culture].resx
Locating Resources • Resource files share the same name [assemblyname].resources.dll • Manifest specifies culture [assemblyname].[resourcetype].[culture].resources • Deployed to culture-specific subdirectories • \[culture] for Windows Forms deployments • \bin\[culture] for ASP.NET deployments
Locating Resources • Best match is current thread’sUI culture • en-CA, en-US • Neutral parent culture is next best fit • en • Last resort is default frommain assembly • (default) (default) en en-CA en-US
Locating Resources • Identify the default resource culture [assembly: NeutralResourcesLanguageAttribute("en")] • Default resources will be used instead of searching for matching satellite assembly (default) en en en-CA en-US
ResourceManager • System.Resources.ResourceManager • Access specific resource type: • Form1, Form2, Strings, Errors, Common • Request resource entries by key name System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1)); button1.Text = rm.GetString(“button1.Text”); int n = (int)rm.GetObject(“count”); string s = rm.GetString(“message”);
ResourceManager • Use helper methods to retrieve resources • GetObject(), GetString() • Access specific resources by name • GetResourceSet() • Access resources for a particular culture • No resource fallback • ReleaseAllResources() • Free resources from memory
ResourceManager • Direct access to satellite assemblies • Returns resources for the current thread’s UI culture setting: • To customize set Thread.CurrentThread.CurrentUICulture • If resource entry not found in satellite assembly for the current culture, ResourceManager performs resource fallback to find a valid entry
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
Runtime Culture Selection • The current thread’s culture settings dictate resource selection • CurrentUICulture Property • Controls resource selection • CurrentCulture Property • Controls culture-aware data formatting
Runtime Culture Selection • System.Globalization.CultureInfo • Encapsulates language and region
CurrentUICulture • System.Threading • Thread.CurrentThread.CurrentUICulture • Defaults to system UI language, or: • Or, request specific ResourceSet • But, you may lose resource fallback feature Thread.CurrentThread.CurrentUICulture = new CultureInfo("es"); ResourceManager rm = new ResourceManager(typeof(Form1)); ResourceSet rset = rm.GetResourceSet(new CultureInfo("es"), true, true);
CurrentCulture • System.Threading • Thread.CurrentThread.CurrentCulture • Defaults to user locale, can be changed Thread.CurrentThread.CurrentCulture = new CultureInfo("es"); DateTime dt = DateTime.Now; String s = dt.ToLongDateString(); // formatted for the current culture
Culture-Specific Formatting • System.Globalization • Utility classes support formatting and conversion based on culture • Formatting Classes • Sorting: SortKey, CompareInfo • String comparison: CompareInfo • Date and time: DateTimeFormatInfo • Numbers: NumberFormatInfo • Calendar data: Calendar
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
Windows Forms Resource Allocation • Control properties automatically stored in embedded resources • Allows translators to edit individual form layout without touching your code • Other content stored inshared resources • Shared between forms, separate resource • Shared between application or assemblies, separate resource assembly
Windows Forms GlobalApp.Resources.dll Main Assembly Application Directory Form1.en-CA.resources \en-CA Strings.en-CA.resources Common.Resources.dll Common.Resources.dll Common.en-CA.resources Common.en-CA.resources GlobalApp.exe Form1.en-CA.resources GlobalApp.Resources.dll Strings.en-CA.resources \es Form1.es.resources Strings.es.resources Common.Resources.dll Common.es.resources GlobalApp.Resources.dll \es-EC Strings.en-CA.resources Products Products_en-CA Products_es
Agenda • What is Globalization? • Localization and Application Architecture • .NET Resources • Runtime Culture Selection • Windows Forms Architecture • ASP.NET Architecture
ASP.NET Duplicate Content • Goals • Facilitate rapid 1.0 release • Deal with translation later • Translate content for each culture • I.e., ASPX, ASCX, HTML, XML • Other source files selected by culture • I.e., GIF, JPG • Can redirect to localized set of pages • Translators edit content source • Error prone
ASP.NET Duplicate Content Web Application Resources GlobalWeb \bin GlobalWeb.dll Global.asax Web.config translated page and user control content page and user control content \en-CA Database Content logo.gif logo.en-CA.gif \es logo.es.gif Products Products_en-CA Products_es
Culture-Specific Directories • Subdirectories named by culture • Filenames and navigation are untouched • Content duplicated per sub-directory • Filenames include culture • Useful for shared directories • I.e., \Images\logo.gif, \Images\logo.es-EC.gif • Use resources to store filename!
Culture-Specific Directories /Articles subdirectory is relative to application path Once redirected to culture-specific directory, navigation shouldbe relative /default.aspx represents default page forneutral culture CurrentUICulture=“es”