1 / 30

Internationalization in ASP.NET 2.0

Internationalization in ASP.NET 2.0. SQL Server 2005 – Data Columns. Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations nchar nvarchar nvarchar(max) ntext Catalog view sys.types reports length in bytes, not characters

owena
Télécharger la présentation

Internationalization in ASP.NET 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internationalizationin ASP.NET 2.0

  2. SQL Server 2005 – Data Columns • Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations • nchar • nvarchar • nvarchar(max) • ntext • Catalog view sys.types reports length in bytes, not characters • Substitute: ASCII() and CHAR() with: UNICODE() and NCHAR() • Define variables and parameters of Stored Procedure and triggers in Unicode • Prefix Unicode string constants with the letter “N”

  3. Establish the User’s preferred “culture”

  4. Culture • Description of a language, and OPTIONALLY a region • System.Globalization.CultureInfo class • languagecode2[-country/regioncode2[-script]] • Examples: en represents “neutral English” en-GB represents UK English • CultureInfo object • Invariant: “” absence of culture (may be English) • Neutral “en” Language – NOT specific to a particular region • Specific “en-GB” Language in a specific region, and region info,

  5. Default International behavior Default internationalization behavior is determined by: CurrentCulture – must be a SPECIFIC culture:default culture for System.Globalization – affects culture specific formatting (date/time, number,currency, … AND display of Calendar control) CurrentUICulture – Can be neutral or specific culture:default culture used by ResourceManager

  6. Setting the Culture • Web.config <globalization enableClientBasedCulture=“true” culture=“en-GB” uiculture=“en” /> Or, <%@ Page … Culture=“auto:en-GB” UICulture=“auto:en” …. %> • Page • Automatic (browser’s first requested culture) with default: <%@ Page … Culture=“auto:en-GB” UICulture=“auto:en” …. %> • Programmatically: • Analyze complete list of browsers culture requests • Based upon setting in the User’s Profile

  7. Set Culture – reviewing all browser requests – CODE Sample ProtectedOverloadsOverridesSubInitializeCulture() If Request.UserLanguages IsNot Nothing AndAlso Request.UserLanguages.GetLength(0) > 0 Then Dim ct As New CultureInfo(Request.UserLanguages(0)) If ct IsNot Nothing Then Thread.CurrentThread.CurrentUICulture = ct Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ct.Name) End If End If EndSub

  8. Resource Code

  9. Resource code Managers • ASP.NET – Built-in: System.Resources.ResourceManager • Uses standard .resx format • Easily handled by localization firms • Directly supported in VS 2005 • Built-in tool to generate .resx filesfrom non-localized ASP.NET project • Cached – good performance • Custom resource managers • Directly use SQL Server • Slower performance

  10. Creating Resource Files – VS 2005 Tool Visual Studio 2005 tool: Generate Local Resources • Generates resource file for a particular page-- from Design view – Tools > Generate Local Resoruces • Automatically creates .resx file that includes all properties marked: Localizable(true) • By default, most Webcontrols’: Text, Title, ToolTip • NOT HTLM controls, except: HtmlAnchor, HtmlImage HtmlInputImage, HtmlTitle • Automatically modifies the page source • Does NOT work with raw HTML, e.g.: <p>text</p> can substitute Localize control

  11. Resource File Hierarchy The Generate Local Resource tool only creates the default page resource files.You must copy the default resource files and rename then to create the language resource hierarchy.

  12. Generate Local Resources Generate Local Resources – Page SourceExample code, before using tool: <%@ Page Language=“vb" AutoEventWireup="true“ CodeFile="Default.aspx.vb" Inherits="_Default" %> after using tool: <%@ Page Language=“vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>

  13. Generate Local Resources Generate Local Resources – Example code – BEFORE using tool: <%@ Page Language=“vb" AutoEventWireup="true“ CodeFile="Default.aspx.vb" Inherits="_Default" %> <table> <tr> <td style="width: 100px"> <asp:Label ID="Label1" runat="server" Text="User name"></asp:Label></td> <td style="width: 100px"> <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox></td> </tr> </table>

  14. Generate Local Resources Generate Local Resources – Example code, after using tool: <table> <tr> <td style="width: 100px"> <asp:Label ID="Label1" runat="server" Text="User Name" meta:resourcekey="Label1Resource1"> </asp:Label></td> <td style="width: 100px"> <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"> </asp:TextBox></td> </tr> </table>

  15. Resource Code GenerationDemo

  16. Add App_GlobalResources Folder& App_LocalResources Folder • Adding Global Resource File under App_GlobalResources FolderAdding Local Resource File under App_LocalResources FolderNote: While creating resource file, you should name resource file with filename.aspx.cultercode.resx

  17. Generate Local Resources Generate Local Resources – Example Resource file:

  18. Generate Local Resources Generate Local Resources – • Original TEXT attribute is retained • Not used at Runtime • Shown at design time • Property Windows - shown with pink icon:

  19. Preventing localization When using the Generate Local Resources tool • Can prevent a control from being included in Generate Local Resources:<asp:Label ID="Label1" runat="server" Meta:localize=“false”Text="User name"> </asp:Label>

  20. Localize control Generate Local Resource tool Does NOT work with static text, e.g.: <p>This is an example.</p> • Can substitute Localize control (derived from Literal), but must contain ONLY static text (no ASP.NET controls) <p> <asp:Localize ID=“locEx” runat=“Server”> This is an example. </asp:Localize> </p>Generate Local Resources will transform this to: <p> <asp:Localize ID=“locEx” runat=“Server”>meta:resourcekey=“locEx” Text=“This is an example.”> </asp:Localize> </p>

  21. Explicit Expressions Generate Local Resource tool– generates IMPLICIT expressions for controls with properties marked internally as “Localizable(true)” Explicit Expressions– useful for binding resources to ANY property … such as color, or ImageURL of ImageControl - use in HTML Attributes

  22. Explicit Expressions – cont’d Explicit Expressions • Syntax:<%$ Resources: resource-file, resource-key %>" • If resource-file is not specified, then looks for resource-key in local resource • Cannot combine: Explicit resource expression for a local resourceand Implicit resource expression within same control • Can combine: Explicit resource expression for a GLOBAL resourceand Implicit resource expression within same control

  23. Global Resources Global Resources • Reduces duplicationAll pages may share access to the same set of resources • Necessary for Explicit resources where in control that also uses Implicit resources • Compiled into a strongly typed class • MUST be in folder: App_GlobalResources • MUST use explicit resource references

  24. Programmatic Resource Access Global Resources • Can use compiled classname labMessage.Text = Resources.GlobalColors.WarningColor Local Resources • User the GetLocalResourceObject labMessage.Text = me.GetLocalResourceObject( “PageResources1.title”)

  25. Using Assemblies • The ResourceManager class allows the user to access and control resources stored in the main assembly or in resource satellite assemblies • The Resource File Generator (Resgen.exe) converts .txt files and .resx (XML-based resource format) files to common language runtime binary .resources files that can be embedded in a runtime binary executable or compiled into satellite assemblies.

  26. Using Text files • resource.fr-FR.txt - which contains the below French text.

  27. visual studio command prompt • Now open the visual studio command prompt and run the resgen.exe with .txt file as parameter as shown in image below.

  28. Code to Access Assemblies 'Read the rsources files Dim strResourcesPath As String = Server.MapPath("bin") Dim rm As ResourceManager = ResourceManager.CreateFileBasedResourceManager("resource.fr-FR", strResourcesPath, Nothing) lblwelcome.Text = rm.GetString("0006") + ", " + rm.GetString("0007") Button1.Text = rm.GetString("0008")

  29. Numbers/Currency Numberic .ToString • Culture formatted using NumberFormatInfo class • Can be created for Specific or Invariant culture, but NOT created for a neutral culture • Default set by CurrentCulture’s NumberFormat Property • Standard number format Specifiers • Uses NumberFormatInfo class: c, C Currency f, F Fixed Pointn, N Number p, P Percent • Does NOT use NumberFormatInfo class: d, D Decimal e, E Exponential x, X Hexadecimal g, G General r, R Round-trip • Can specify a particular culture Val.ToString(“N”, new CultureInfo(“fr-FR”))

  30. DateTime - Output DateTime .ToString method • Culture formatted using DateTimeFormatInfo class • Can be created for Specific or Invariant culture, but NOT created for a neutral culture • Default set by CurrentCulture’s DateTimeFormat Property • Uses CurrentCulture by default: ToString() ToShortDateString() ToLongDateString() ToShortTimeString() ToLongTimeString() • Specify particular culture: ToString(string) ToString(string, IFormatProvider)

More Related