Title: Internationalization in ASP.NET 2.0
1Internationalizationin ASP.NET 2.0
2SQL 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
3Establish the Users preferred culture
4Culture
- 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,
5Default International behavior
- Default internationalization behavior is
determined by - CurrentCulture must be a SPECIFIC
culturedefault culture for System.Globalization
affects culture specific formatting (date/time,
number,currency, AND display of Calendar
control) - CurrentUICulture Can be neutral or specific
culturedefault culture used by ResourceManager
6Setting the Culture
- Web.config
- ltglobalization enableClientBasedCulturetrue
cultureen-GB
uicultureen /gt - Or, lt_at_ Page Cultureautoen-GB
UICultureautoen . gt - Page
- Automatic (browsers first requested culture)
with default lt_at_ Page Cultureautoen-GB
UICultureautoen .
gt - Programmatically
- Analyze complete list of browsers culture
requests - Based upon setting in the Users Profile
7Set Culture reviewing all browser requests
CODE Sample
- Protected Overloads Overrides Sub
InitializeCulture() - Â Â Â Â Â If Request.UserLanguages IsNot Nothing
AndAlso Request.UserLanguages.GetLength(0) gt 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
- End Sub
8Resource Code
9Resource code Managers
- ASP.NET Built-in System.Resources.ResourceM
anager - 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
10Creating Resource Files VS 2005 Tool
- Visual Studio 2005 tool Generate
Local Resources - Generates resource file for a particular page
-- from Design view Tools gt 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. ltpgttextlt/pgt
can substitute Localize control
11Resource 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.
12Generate Local Resources
- Generate Local Resources Page SourceExample
code, before using tool - lt_at_ Page Languagevb" AutoEventWireup"true
CodeFile"Default.aspx.vb" Inherits"_Default" gt - after using tool
- lt_at_ Page Languagevb" AutoEventWireup"true"
- CodeFile"Default.aspx.vb" Inherits"_Default"
- Culture"auto" metaresourcekey"PageResource1"
UICulture"auto" gt
13Generate Local Resources
- Generate Local Resources Example code BEFORE
using tool lt_at_ Page Languagevb"
AutoEventWireup"true CodeFile"Default.aspx.vb"
Inherits"_Default" gt - lttablegt
- lttrgt
- lttd style"width 100px"gt
- ltaspLabel ID"Label1" runat"server"
- Text"User name"gtlt/aspLabelgtlt/tdgt
- lttd style"width 100px"gt
- ltaspTextBox ID"TextBox1" runat"server"gt
- lt/aspTextBoxgtlt/tdgt
- lt/trgt
- lt/tablegt
14Generate Local Resources
- Generate Local Resources Example code, after
using tool - lttablegt lttrgt
- lttd style"width 100px"gt
- ltaspLabel ID"Label1" runat"server"
- Text"User Name"
- metaresourcekey"Label1Resource1"gt
- lt/aspLabelgtlt/tdgt
- lttd style"width 100px"gt
- ltaspTextBox ID"TextBox1" runat"server"
- metaresourcekey"TextBox1Resource1"gt
- lt/aspTextBoxgtlt/tdgt
- lt/trgt lt/tablegt
15Resource Code GenerationDemo
16Add App_GlobalResources Folder
App_LocalResources Folder
- Adding Global Resource File under
App_GlobalResources Folder Adding Local Resource
File under App_LocalResources FolderNote While
creating resource file, you should name resource
file with filename.aspx.cultercode.resx -
17Generate Local Resources
- Generate Local Resources Example Resource file
18Generate Local Resources
- Generate Local Resources
- Original TEXT attribute is retained
- Not used at Runtime
- Shown at design time
- Property Windows - shown with pink icon
19Preventing localization
- When using the Generate Local Resources tool
- Can prevent a control from being included in
Generate Local ResourcesltaspLabel ID"Label1"
runat"server" - Metalocalizefalse Text"User name"gt
- lt/aspLabelgt
20Localize control
- Generate Local Resource tool Does NOT work with
static text, e.g. ltpgtThis is an example.lt/pgt - Can substitute Localize control (derived from
Literal), but must contain ONLY static text (no
ASP.NET controls) ltpgt ltaspLocalize IDlocEx
runatServergt This is an example.
lt/aspLocalizegt lt/pgtGenerate Local Resources
will transform this to ltpgt ltaspLocalize
IDlocEx runatServergt metaresourcekeylo
cEx TextThis is an example.gt
lt/aspLocalizegt lt/pgt
21Explicit 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
22Explicit Expressions contd
- Explicit Expressions
- Syntax lt Resources resource-file,
resource-key gt" - 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 - Can combine Explicit resource
expression for a GLOBAL resource
and Implicit resource expression within
same control
23Global Resources
- Global 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 - Compiled into a strongly typed class
- MUST be in folder App_GlobalResources
- MUST use explicit resource references
24Programmatic Resource Access
- Global Resources
- Can use compiled classname labMessage.Text
Resources.GlobalColors.WarningColor - Local Resources
- User the GetLocalResourceObject labMessage.Text
me.GetLocalResourceObject(
PageResources1.title)
25Using 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.
26Using Text files
- resource.fr-FR.txt - which contains the below
French text.
27visual 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.
28Code to Access Assemblies
- 'Read the rsources files
- Dim strResourcesPath As String
Server.MapPath("bin") - Dim rm As ResourceManager
ResourceManager.CreateFileBasedResourceManager("re
source.fr-FR", strResourcesPath, Nothing) - lblwelcome.Text rm.GetString("0006") ", "
rm.GetString("0007") - Button1.Text rm.GetString("0008")
29Numbers/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 CurrentCultures 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))
30DateTime - 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 CurrentCultures DateTimeFormat
Property - Uses CurrentCulture by default
- ToString() ToShortDateString() ToLongDateString()
ToShortTimeString() ToLongTimeString() - Specify particular culture
- ToString(string)
- ToString(string, IFormatProvider)