Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internationalization in ASP.NET 2.0. SQL Server 2005 – Data Columns Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations Use Unicode.

Similar presentations


Presentation on theme: "Internationalization in ASP.NET 2.0. SQL Server 2005 – Data Columns Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations Use Unicode."— Presentation transcript:

1 Internationalization in ASP.NET 2.0

2 SQL Server 2005 – Data Columns Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations nchar nchar nvarchar nvarchar nvarchar(max) nvarchar(max) ntext ntext Catalog view sys.types reports length in bytes, not characters Catalog view sys.types reports length in bytes, not characters Substitute: ASCII() and CHAR() with: UNICODE() and NCHAR() Substitute: ASCII() and CHAR() with: UNICODE() and NCHAR() Define variables and parameters of Stored Procedure and triggers in Unicode Define variables and parameters of Stored Procedure and triggers in Unicode Prefix Unicode string constants with the letter “N” 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 Description of a language, and OPTIONALLY a region System.Globalization.CultureInfo class System.Globalization.CultureInfo class languagecode2[-country/regioncode2[- script]] languagecode2[-country/regioncode2[- script]] Examples: en represents “neutral English” en-GB represents UK English Examples: en represents “neutral English” en-GB represents UK English CultureInfo object CultureInfo object Invariant:“”absence of culture (may be English) Invariant:“”absence of culture (may be English) Neutral“en”Language – NOT specific to a particular region Neutral“en”Language – NOT specific to a particular region Specific“en-GB” Language in a specific region, and region info, 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 Web.config Or, Or, Page Page Automatic (browser’s first requested culture) with default: Automatic (browser’s first requested culture) with default: Programmatically: Programmatically: Analyze complete list of browsers culture requests Analyze complete list of browsers culture requests Based upon setting in the User’s Profile Based upon setting in the User’s Profile

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

8 Resource Code

9 Resource code Managers ASP.NET – Built-in: System.Resources.ResourceManager ASP.NET – Built-in: System.Resources.ResourceManager Uses standard.resx format Uses standard.resx format Easily handled by localization firmsEasily handled by localization firms Directly supported in VS 2005 Directly supported in VS 2005 Built-in tool to generate.resx files from non-localized ASP.NET project Built-in tool to generate.resx files from non-localized ASP.NET project Cached – good performance Cached – good performance Custom resource managers Custom resource managers Directly use SQL Server Directly use SQL Server Slower performance 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 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) Automatically creates.resx file that includes all properties marked: Localizable(true) By default, most Webcontrols’: Text, Title, ToolTip By default, most Webcontrols’: Text, Title, ToolTip NOT HTLM controls, except: HtmlAnchor, HtmlImage HtmlInputImage, HtmlTitle NOT HTLM controls, except: HtmlAnchor, HtmlImage HtmlInputImage, HtmlTitle Automatically modifies the page source Automatically modifies the page source Does NOT work with raw HTML, e.g.: text can substitute Localize control Does NOT work with raw HTML, e.g.: text 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 Source Example code, before using tool : after using tool : 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 : Generate Local Resources – Example code – BEFORE using tool : <table><tr> <asp:Label ID="Label1" runat="server" Text="User name "> Text="User name "> </asp:TextBox></td></tr></table>

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

15 Resource Code Generation Demo

16 Add App_GlobalResources Folder & App_LocalResources Folder Adding Global Resource File under App_GlobalResources Folder Adding Local Resource File under App_LocalResources Folder Note: While creating resource file, you should name resource file with filename.aspx.cultercode.resx Adding Global Resource File under App_GlobalResources Folder Adding Local Resource File under App_LocalResources Folder Note: 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 retainedOriginal TEXT attribute is retained Not used at RuntimeNot used at Runtime Shown at design timeShown at design time Property Windows - shown with pink icon: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" Can prevent a control from being included in Generate Local Resources: <asp:Label ID="Label1" runat="server" Meta:localize=“false” Text="User name ">

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

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 - use in HTML Attributes

22 Explicit Expressions – cont’d Explicit Expressions Syntax: " Syntax: " If resource-file is not specified, then looks for resource-key in local resource If resource-file is not specified, then looks for resource-key in local resource Cannot combine: Explicit resource expression for a local resource and Implicit resource expression within same control Cannot combine: Explicit resource expression for a local resource and Implicit resource expression within same control Can combine: Explicit resource expression for a GLOBAL resource and Implicit resource expression within same control Can combine: Explicit resource expression for a GLOBAL resource and Implicit resource expression within same control

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

24 Programmatic Resource Access Global Resources Can use compiled classname labMessage.Text = Resources.GlobalColors.WarningColor Can use compiled classname labMessage.Text = Resources.GlobalColors.WarningColor Local Resources User the GetLocalResourceObject labMessage.Text = me.GetLocalResourceObject( “PageResources1.title”) 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 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. 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. 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. 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 'Read the rsources files Dim strResourcesPath As String = Server.MapPath("bin") Dim strResourcesPath As String = Server.MapPath("bin") Dim rm As ResourceManager = ResourceManager.CreateFileBasedResourc eManager("resource.fr-FR", strResourcesPath, Nothing) Dim rm As ResourceManager = ResourceManager.CreateFileBasedResourc eManager("resource.fr-FR", strResourcesPath, Nothing) lblwelcome.Text = rm.GetString("0006") + ", " + rm.GetString("0007") Button1.Text = rm.GetString("0008") Button1.Text = rm.GetString("0008")

29 Numbers/Currency Numberic.ToString Culture formatted using NumberFormatInfo class Culture formatted using NumberFormatInfo class Can be created for Specific or Invariant culture, but NOT created for a neutral culture Can be created for Specific or Invariant culture, but NOT created for a neutral culture Default set by CurrentCulture’s NumberFormat Property Default set by CurrentCulture’s NumberFormat Property Standard number format Specifiers Standard number format Specifiers Uses NumberFormatInfo class: Uses NumberFormatInfo class: c, CCurrencyf, FFixed Point n, NNumberp, P Percent Does NOT use NumberFormatInfo class: Does NOT use NumberFormatInfo class: d, DDecimale, EExponential x, XHexadecimal g, GGeneralr, RRound-trip Can specify a particular culture Can specify a particular culture Val.ToString(“N”, new CultureInfo(“fr-FR”)) Val.ToString(“N”, new CultureInfo(“fr-FR”))

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


Download ppt "Internationalization in ASP.NET 2.0. SQL Server 2005 – Data Columns Use Unicode datatypes in: Table columns, CONVERT() and CAST() operations Use Unicode."

Similar presentations


Ads by Google