ASP.NET Tutorial/I18N/CultureInfo
Содержание
Choose culture control
<source lang="csharp">
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="ChooseCultureControl" %>
<asp:Localize ID="locCountry" runat="server" meta:resourcekey="locCountry1" Text="Country :" ></asp:Localize> <asp:ImageButton ID="imgUS" runat="server" CssClass="cultureFlag" BorderWidth="1" CommandName="US" OnCommand="imgCommand" ImageUrl="images/flag_us.gif" meta:resourcekey="imgUSResource1" /> <asp:ImageButton ID="imgFR" runat="server" CssClass="cultureFlag" BorderWidth="1" CommandName="France" OnCommand="imgCommand" ImageUrl="images/flag_fr.gif" meta:resourcekey="imgFRResource1" />
File: Control.ascx.cs
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ChooseCultureControl : System.Web.UI.UserControl {
protected void Page_Load(object sender, EventArgs e) { if (Profile.Culture == "fr-FR") { imgFR.BorderStyle = BorderStyle.Dotted; imgUS.BorderStyle = BorderStyle.None; } else { imgUS.BorderStyle = BorderStyle.Dotted; imgFR.BorderStyle = BorderStyle.None; } } protected void imgCommand(object sender, CommandEventArgs e) { if (e.rumandName == "France") Profile.Culture = "fr-FR"; else Profile.Culture = "en-US";
Response.Redirect(Request.Url.AbsolutePath); }
}
File: Web.config <?xml version="1.0"?> <configuration>
<system.web> <anonymousIdentification enabled="true"/> <profile> <properties> <add name="Culture" type="string" allowAnonymous="true" defaultValue="auto"/> </properties> </profile> <compilation debug="true"/> </system.web>
</configuration></source>
Create CultureInfo
<source lang="csharp">
<%@ Page Language="VB" %> <%@ Import Namespace="System.Globalization" %> <script runat="server">
sub Page_Load(Sender as Object, e as EventArgs) dim strLanguage as string = Request.UserLanguages(0).ToString lblMessage.Text = "Primary language: " & strLanguage & "
" dim objCulture as new CultureInfo(strLanguage) lblMessage.Text += "Full name: " & objCulture.EnglishName & "
" lblMessage.Text += "Native name: " & objCulture.NativeName & "
" lblMessage.Text += "Abbreviation: " & objCulture.ThreeLetterISOLanguageName & "
" lblMessage.Text += "Current Time: " & DateTime.Now.ToString("D", objCulture) & "
" lblMessage.Text += "Parent: " & objCulture.Parent.EnglishName & "
" end sub
</script> <html><body>
Your user information: <asp:Label id="lblMessage" runat="server"/>
</body></html></source>
Select CultureAuto
<source lang="csharp">
In the <%@ Page %> directive, both the Culture and UICulture attributes are set to the value auto:en-US. The culture name that appears after the colon enables you to specify a default culture when a language preference cannot be detected from the browser.
<%@ Page Language="C#" Culture="auto:en-US" UICulture="auto:en-US"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">
void Page_PreRender() { lblDate.Text = DateTime.Now.ToString("D"); lblPrice.Text = (512.33m).ToString("c"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Select Culture Auto</title>
</head> <body>
<form id="form1" runat="server">
Today"s date is:
<asp:Label id="lblDate" Runat="server" />
The price of the product is:
<asp:Label id="lblPrice" Runat="server" />
</form>
</body> </html></source>
Selecting a culture from a DropDownList control.
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">
protected void btnSelect_Click(object sender, EventArgs e) { Culture = ddlCulture.SelectedValue; } void Page_PreRender() { lblDate.Text = DateTime.Now.ToString("D"); lblPrice.Text = (512.33m).ToString("c"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Select Culture</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblCulture" Text="Culture:" AssociatedControlID="ddlCulture" Runat="server" /> <asp:DropDownList id="ddlCulture" DataTextField="DisplayName" DataValueField="Name" DataSourceID="srcCultures" Runat="server" /> <asp:Button id="btnSelect" Text="Select" Runat="server" OnClick="btnSelect_Click" />
<asp:ObjectDataSource id="srcCultures" TypeName="System.Globalization.CultureInfo" SelectMethod="GetCultures" Runat="server"> <SelectParameters> <asp:Parameter Name="types" DefaultValue="SpecificCultures" /> </SelectParameters> </asp:ObjectDataSource>
Today"s date is:
<asp:Label id="lblDate" Runat="server" />
The price of the product is:
<asp:Label id="lblPrice" Runat="server" />
</form>
</body> </html></source>
Use Profile object to store a user"s preferred culture
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">
protected override void InitializeCulture() { Culture = Profile.UserCulture; UICulture = Profile.UserUICulture; } protected void btnSelect_Click(object sender, EventArgs e) { Profile.UserCulture = ddlCulture.SelectedValue; Profile.UserUICulture = ddlCulture.SelectedValue; Response.Redirect(Request.Path); } void Page_PreRender() { lblDate.Text = DateTime.Now.ToString("D"); lblPrice.Text = (512.33m).ToString("c"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Select Culture Profile</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblCulture" Text="Culture:" AssociatedControlID="ddlCulture" Runat="server" /> <asp:DropDownList id="ddlCulture" DataTextField="DisplayName" DataValueField="Name" DataSourceID="srcCultures" Runat="server" /> <asp:Button id="btnSelect" Text="Select" Runat="server" OnClick="btnSelect_Click" />
<asp:ObjectDataSource id="srcCultures" TypeName="System.Globalization.CultureInfo" SelectMethod="GetCultures" Runat="server"> <SelectParameters> <asp:Parameter Name="types" DefaultValue="SpecificCultures" /> </SelectParameters> </asp:ObjectDataSource>
Today"s date is:
<asp:Label id="lblDate" Runat="server" />
The price of the product is:
<asp:Label id="lblPrice" Runat="server" />
</form>
</body> </html> File: Web.Config <configuration xmlns="http://schemas.microsoft.ru/.NetConfiguration/v2.0">
<system.web> <anonymousIdentification enabled="true"/> <profile> <properties> <add name="UserCulture" defaultValue="en-US" /> <add name="UserUICulture" defaultValue="en"/> </properties> </profile> </system.web>
</configuration></source>
Using the CultureInfo Class to Format String Values
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Globalization" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">
void Page_Load() { CultureInfo gCulture = new CultureInfo("de-DE"); lblGermanDate.Text = DateTime.Now.ToString("D", gCulture); lblGermanPrice.Text = (512.33m).ToString("c", gCulture); CultureInfo iCulture = new CultureInfo("id-ID"); lblIndonesianDate.Text = DateTime.Now.ToString("D", iCulture); lblIndonesianPrice.Text = (512.33m).ToString("c", iCulture); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>ToString Culture</title>
</head> <body>
<form id="form1" runat="server">
German
Today"s date is:
<asp:Label id="lblGermanDate" Runat="server" />
The price of the product is:
<asp:Label id="lblGermanPrice" Runat="server" />
Indonesian
Today"s date is:
<asp:Label id="lblIndonesianDate" Runat="server" />
The price of the product is:
<asp:Label id="lblIndonesianPrice" Runat="server" />
</form>
</body> </html></source>