300 likes | 394 Views
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
E N D
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”
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,
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
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
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
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
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
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.
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" %>
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>
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>
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
Generate Local Resources Generate Local Resources – Example Resource file:
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:
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>
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>
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
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
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
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”)
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.
Using Text files • resource.fr-FR.txt - which contains the below French text.
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.
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")
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”))
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)