ASP.NET Tutorial/I18N/CultureInfo

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Choose culture control

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="ChooseCultureControl" %>
<div id="cultureStyle">   
   <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" />
</div>
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>


Create CultureInfo

<%@ 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 & "<br>"
      
      dim objCulture as new CultureInfo(strLanguage)
      lblMessage.Text += "Full name: " & objCulture.EnglishName & "<br>"
      lblMessage.Text += "Native name: " & objCulture.NativeName & "<br>"
      lblMessage.Text += "Abbreviation: " & objCulture.ThreeLetterISOLanguageName & "<br>"
      lblMessage.Text += "Current Time: " & DateTime.Now.ToString("D", objCulture) & "<br>"
      lblMessage.Text += "Parent: " & objCulture.Parent.EnglishName & "<br>"
      
   end sub   
</script>
<html><body>
   <b>Your user information:</b> 
   <asp:Label id="lblMessage" runat="server"/>
</body></html>


Select CultureAuto

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">
    <div>
    Today"s date is:
    <br />
    <asp:Label
        id="lblDate"
        Runat="server" />
    <br /><br />
    The price of the product is:
    <br />
    <asp:Label
        id="lblPrice"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Selecting a culture from a DropDownList control.

<%@ 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">
    <div>
    <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>
    <hr />
    Today"s date is:
    <br />
    <asp:Label
        id="lblDate"
        Runat="server" />
    <br /><br />
    The price of the product is:
    <br />
    <asp:Label
        id="lblPrice"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Use Profile object to store a user"s preferred culture

<%@ 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">
    <div>
    <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>
    <hr />
    Today"s date is:
    <br />
    <asp:Label
        id="lblDate"
        Runat="server" />
    <br /><br />
    The price of the product is:
    <br />
    <asp:Label
        id="lblPrice"
        Runat="server" />
    </div>
    </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>


Using the CultureInfo Class to Format String Values

<%@ 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">
    <div>
    <h1>German</h1>
    Today"s date is:
    <br />
    <asp:Label
        id="lblGermanDate"
        Runat="server" />
    <br /><br />
    The price of the product is:
    <br />
    <asp:Label
        id="lblGermanPrice"
        Runat="server" />
    <h1>Indonesian</h1>
    Today"s date is:
    <br />
    <asp:Label
        id="lblIndonesianDate"
        Runat="server" />
    <br /><br />
    The price of the product is:
    <br />
    <asp:Label
        id="lblIndonesianPrice"
        Runat="server" />
    </div>
    </form>
</body>
</html>