580 likes | 746 Views
WEB326 ASP.NET 2.0: Going Global Gets Easier! New Localization Features in ASP.NET 2.0 . Michele Leroux Bustamante Architect, IDesign Microsoft Regional Director. Michele Leroux Bustamante. IDesign Inc., www.idesign.net Microsoft Regional Director Microsoft MVP – XML Web Services
E N D
WEB326ASP.NET 2.0: Going Global Gets Easier! New Localization Features in ASP.NET 2.0 Michele Leroux Bustamante Architect, IDesign Microsoft Regional Director
Michele Leroux Bustamante • IDesign Inc., www.idesign.net • Microsoft Regional Director • Microsoft MVP – XML Web Services • Published in MSDN, CoDe, more… • International Speaker, INETA • BEA Technical Director • Events Director, IASA • Program Advisor, UCSD • Web Services Track Chair, SD Expo • Blogs: www.dasblonde.net, www.interopwarriors.com
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
What Is Globalization? • Designing applications to be adapted to the global marketplace • Display, input, formats & conventions • Localizability • Separate culture dependencies from code • Localization • Adapting applications for specific markets • 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, es) • Country Code: ISO 3166 (i.e., CA, EC) • Together, they identify culture (i.e., es-EC) • See RFC 3066 – Tags for identifying languages • Custom cultures are sometimes necessary
What To Localize? • Static (hard-coded) application content • Menu/control captions, Web content, XML • Dynamic application content • Database, XML, other • Graphical content • Filenames, connection strings, queries
Storing Localized Content • Deployment architecture is affected by location of localized application content • Resources, file system, database,external applications • Choices influenced by application type • Microsoft Windows versus Web • And by business requirements • Frequency of culture translations • Ease of translation process • Frequency of application maintenance
Duplicate Site Content Web Application Resources GlobalWeb \bin GlobalWeb.dll Global.asax Web.config page and user control content Database Content logo.gif Products
Duplicate Site 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
Single Content Base Web Application Resources Satellite Assemblies GlobalWeb \bin \en GlobalWeb.en.dll GlobalWeb.dll Global.asax 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 • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
Localization Features in 2.0 • Web Forms resource generator • Localization expressions • Strongly-typed resources • Improved Resource Editor • ResourceManager lifetime management • Automatic culture selection • New Microsoft ASP.NET deployment options • New resource provider model
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
ASP.NET 2.0 and Resources • XML Resources (*.resx) • Generated for master pages, content pages, and user controls • Accessible through page or control properties, declarative expressions, or other APIs • Type-safe global resources • Set multiple localized control properties in a single command
ASP.NET 2.0 and Resources (cont’d) • App_LocalResources • Generated automatically or by hand • Per page, control • App_GlobalResources • Manual generation • Shared, strongly-typed • Pre-compile or deploy source files and let the resource build provider handle the rest
ASP.NET 2.0 and Resources (cont’d) • ResourceManager lifecycle is handled automatically • Resource providers are cached along with specific resource manager instances per local or global resource • Default resource and satellite assemblies are loaded into application domain, like providers, and readily accessible • ResourceManager is still thread-safe
Generating Resources • From Design View, select Generate Local Resource from the Tools menu • Localizable properties for server controls are stuffed into resources • Non-localizable properties can be manually inserted • Localization expressions are generated for each server control • Page parser turns expressions into code to set properties from resources
Localizing Static Content • The <asp:Localize> control can isolate blocks of static text for localization • Participates in resource generation • Based on the <asp:Literal> control, but can directly edit text in Design View <asp:Localize id=”welcomeContent” runat="server">Welcome!</asp:Localize>
Generating Local Resources Michele Leroux Bustamante Architect, IDesign Microsoft Regional Director
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
Localization Expressions • ASP.NET supports new declarative binding expressions for connection strings, application settings and resources • Extensible for custom bindings • Automatically applied when resources are generated for a page • Can hand-edit to control where resources are drawn from
Implicit Expressions • After generating resources, meta:resourcekey indicates the resource prefix for this control • Resources are generated with this prefix for all localizable properties <asp:LinkButton id=lnkSelectCulture PostBackUrl="selectculture.aspx" runat="server" meta:resourcekey="LinkButtonResource1">Change Culture Settings</asp:LinkButton>’
Explicit Expressions • Set a specific property to a specific resource entry • Can come from local page resources: • Or, from a global resource: <asp:ImageButton id="btnIDesign" Runat="server" ImageUrl="~/Images/idesignlogo.jpg" AlternateText='<%$ Resources: MissionStatement %>‘ PostBackUrl="http://www.idesign.net" meta:resourcekey="ImageButtonResource1" /> <asp:ImageButton id="btnIDesign" Runat="server" ImageUrl="~/Images/idesignlogo.jpg" AlternateText='<%$ Resources: Glossary, MissionStatement %>‘ PostBackUrl="http://www.idesign.net" meta:resourcekey="ImageButtonResource1" />
Binding Expression Editor • Bindings can be set through the Properties window via Expressions editor dialog • ClassKey specifies a global resource if applicable, omit for local resources • ResourceKey specifies the resource entry to apply to the control property
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
Creating Global Resources • Add a Resources file to the application • Will be placed in a special App_GlobalResources directory
Creating Global Resources • Add resources through the integrated Resource Editor • Apply them to server controls using explicit expressions • Or, write custom code to retrieve them at runtime <asp:ImageButton id="btnIDesign" Runat="server" ImageUrl="~/Images/idesignlogo.jpg" AlternateText='<%$ Resources: Glossary, MissionStatement %>‘ PostBackUrl="http://www.idesign.net" meta:resourcekey="ImageButtonResource1" />
Improved Resource Editor • Integrated with Visual Studio • Visually categorize resources by their data type • More friendly interface • Create new images/icons in place
Strongly-Typed Resources • Global resources generate a type-safe class accessible via global Properties object
class Resources { private static System.Resources.ResourceManager _resMgr; private static System.Globalization.CultureInfo _resCulture; /*FamANDAssem*/ internal Resources() {} [System.ComponentModel.EditorBrowsableAttribute(…)] public static System.Resources.ResourceManager ResourceManager {…} [System.ComponentModel.EditorBrowsableAttribute(…)] public static System.Globalization.CultureInfo Culture {…} public static System.Drawing.Bitmap flag { get { return ((System.Drawing.Bitmap)(ResourceManager.GetObject("flag", _resCulture))); } } }
Global Resources, Expressions and Runtime Behavior Michele Leroux Bustamante Architect, IDesign Microsoft Regional Director
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
Runtime Culture Selection • System.Threading.Thread.CurrentThread • CurrentUICulture • Affects resource selection • Defaults to system UI language, or: • CurrentCulture • Affects formatting and conventions • Defaults to user locale, can be changed: Thread.CurrentThread.CurrentUICulture = new CultureInfo("es"); Thread.CurrentThread.CurrentCulture = new CultureInfo("es"); DateTime dt = DateTime.Now; String s = dt.ToLongDateString();
CultureInfo • System.Globalization.CultureInfo • Encapsulates language and region
Resources and Assemblies • Satellite assemblies • Generated from localized resources • Hub and spoke model • Pre-compiled or JIT-compiled for ASP.NET • Resource build provider • ResourceManager handles resource fallback to retrieve culture-specific entries at runtime
(default) en en-CA en-US Resource Fallback • Best match is current thread’sUI culture • I.e., en-CA, en-US • Neutral parent culture is next best fit • I.e., en • Last resort is default frommain assembly • (default)
(default) en en-CA en-US Resource Fallback • Identify the default resource culture [assembly: NeutralResourcesLanguageAttribute("en")] • Default resources will be used instead of searching for satellite assembly
Culture-Specific Formatting • System.Globalization • Utility classes support formatting, sorting and conversion by culture • Formatting Classes • Sorting: SortKey, CompareInfo • String comparison: CompareInfo • Date and time: DateTimeFormatInfo • Numbers: NumberFormatInfo • Calendar data: Calendar
Automatic Culture Selection • Based on browser language settings • Application or page scope • Can specify default culture • I.e., “auto:en-US” <globalizationrequestEncoding=“utf-8" responseEncoding=“utf-8" fileEncoding=“utf-8" culture=“auto:en-US" uiCulture=“auto" /> <%@ Page UICulture="auto" Culture="auto">
Automatic Culture Selection Michele Leroux Bustamante Architect, IDesign Microsoft Regional Director
Agenda • Localization and the Web • New Localization Features in 2.0 • Generating Local Resources • Localization Expressions • Global Resources and Type Safety • Runtime Culture Selection • ASP.NET Architecture
ASP.NET Architecture • Single code/page base is easy with 2.0 • No duplicate Web content per culture • Generated page resources • Make consuming resources per page easy • Type-safe shared resources • Make consuming common resources easy • ResourceManager lifecycle • Less code required, runtime handles it
Single Code/Page Base • Low impact translations • Improved productivity • Fewer errors • Less overall development effort • Issues • Managing iterations with translators • Giving translators access to data sources • Working with Web designers
Deployment Options • Deploy application source • Resources (.resx) are dynamically compiled into satellite assemblies • Updates are not version controlled • Source is not protected • Deploy pre-compiled application • Assemblies pre-generated, including a local and global resource assembly and its associated satellite assemblies • Better version control and IP protection
Managing User Preferences • Automatic culture selection is inadequate for enterprise applications • Culture selection options: • Auto (for default only) • UI selection • User registration • Preferences should be persisted: • Transient solutions include cookies or user session • Persistent solutions should leverage profiles or data storage
Caching and Localization • Caching is critical to site performance • Caching localized pages requires custom caching per page • @OutputCache, VaryByCustom <%@ OutputCache Duration=86400 VaryByParam=None VaryByCustom="uiCulture" %>
Caching and Localization • GetVaryByCustomString must have access to the user’s preferred culture • Initialize the thread earlier in page cycle • Note: Session and Profile not available public override string GetVaryByCustomString(HttpContext context, string custom) { if (string.Compare(custom, "uiCulture", true, System.Globalization.CultureInfo.InvariantCulture) == 0 && System.Threading.Thread.CurrentThread.CurrentUICulture != null) { return System.Threading.Thread.CurrentThread.CurrentUICulture.Name; } else return base.GetVaryByCustomString(context, custom); }