asp.net开发国际化的程序十个例子

Globalized Web Applications & ASP.NET

by Max Poliashenko and Chip Andrews

例子1

(a)

1<input )="" =getstring(untranslatedstring="" ```="" type="text" value="```"/>

(b)

Response.Write("

 1<select>")   
 2  
 3For Each row In myRecordset   
 4  
 5Reponse.Write("<option") if="" myrecordset!id.value="oldSelection" response.write("="" selected="" then="">")   
 6  
 7Else   
 8  
 9Response.Write("&gt;")   
10  
11End If   
12  
13Response.Write(GetLocalizedString(myRecordset!MyField.value)&amp;   
14  
15"")   
16  
17Next   
18  
19Response.Write("</option")></select>

")

例子2

(a)

public class GlobalizedLabel : System.Web.UI.WebControls.Label

{

protected override void Render(HtmlTextWriter output)

{

output.Write(Translator.GetLocalizedString(Text));

}

}

(b)

Translator.GetLocalizedString("The amount of ")) + price.ToString(
) +

Translator.GetLocalizedString(" will be charged to your account.&quo
t;));

(c)

(new StringBuilder()).AppendFormat("The amount of {0:C} will be

charged to your account.", price);

(d)

1@ Page language="c#" 
1@ Register TagPrefix="glb" NameSpace="GlobalControls&quo   
2t;   
3  
4Assembly="GlobalComponents&quo   
5t;
1<html>
2<body>
3<glb:globalizedlabel id="lblGlobal" runat="server">Test Label   
4  
5</glb:globalizedlabel>
6</body>
7</html>

例子3

///

1<summary>   
2  
3/// Validation messages are translated   
4  
5/// </summary>

public class GlobalizedRequiredFieldValidator : RequiredFieldValidator

{

protected override void Render(HtmlTextWriter output)

{

Text = Translator.GetLocalizedString(Text);

ErrorMessage = Translator.GetLocalizedString(ErrorMessage);

base.Render(output);

}

}

... etc.

public class GlobalizedValidationSummary : ValidationSummary

{

///

1<summary>   
2  
3/// This header will be translated   
4  
5/// </summary>

public new string HeaderText

{

get{return base.HeaderText;}

set{base.HeaderText = Translator.GetLocalizedString(value);}

}

}

例子4

1@ Page language="c#" 
1@ Register TagPrefix="glb" NameSpace="AspNetDemo.Compone   
2nts"   
3  
4Assembly="Components"   
5;
 1<html>
 2<head>
 3<meta content="Microsoft Visual Studio 7.0" name="GENE   
 4RATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6</head>
 7<body bgcolor="#e4e4e0">
 8<h3 align="center">User Information</h3>
 9<form headertext="The following validation errors occurred:" id="Form2" method="post" runat="   
10server" vsummary"=""></form>
11<hr/>
12<table border="0" cellpadding="0" cellspacing="0" t;100%"="" width="&amp;quo">
13<tr>
14<td width="30%">Name</td>
15<td>
16<asp:textbox id="txName" runat="server">
17</asp:textbox>
18<glb:globalizedrequiredfieldvalidator controltovalidate="txName" display="&amp;" errormessage="Name is re   
19quired." id="rfValidator1&amp;quo   
20t;   
21  
22runat=" quot;dynamic"="" server"="" text="required   
23"></glb:globalizedrequiredfieldvalidator>
24</td>
25</tr>
26<tr>
27<td>Password</td>
28<td>
29<asp:textbox id="txPassword" server"="" textmode="Password&amp;   
30quot;   
31  
32runat="></asp:textbox>
33<glb:globalizedrequiredfieldvalidator display="&amp;q" errormessage="Password is required." id="rfValidator2&amp;quo   
34t;   
35  
36controltovalidate=" quot;server"="" runat="&amp;" text="minimum 5 characters" txpassword"="" uot;dynamic"=""></glb:globalizedrequiredfieldvalidator>
37</td>
38</tr>
39<tr>
40<td>Confirm password</td>
41<td>
42<asp:textbox id="txPasswordConf" runat="   
43server" textmode="Password"></asp:textbox>
44<glb:globalizedcomparevalidator ;="" controltocompare="txPassword" controltovalidate="txPasswordConf" display="Dyn   
45amic" errormessage="Passwords don't match. Please, re-   
46enter." id="cmpValidator1" operator="Equal" runat="server" text="doesn't match" type="String"></glb:globalizedcomparevalidator>
47</td>
48</tr>
49<tr>
50<td>Email</td>
51<td>
52<asp:textbox id="txEmail" runat="server">   
53;</asp:textbox>
54<glb:globalizedrequiredfieldvalidator controltovalidate="txEmail" display="d   
55ynamic" errormessage="Email is   
56required." id="rfValidator3&amp;quo   
57t;   
58  
59runat=" server"="" text="required"></glb:globalizedrequiredfieldvalidator>
60<glb:globalizedregularexpressionvalidator "="" controltovalidate="txEmail" di="" email="" format="" id="EmailRegVali   
61dator" invalid.="" is="" runat="server&amp;q   
62uot;   
63  
64ErrorMessage=" splay="static" validationexpression="\w+@\w+\\.\w+">   
65  
66[email protected]</glb:globalizedregularexpressionvalidator>
67</td>
68</tr>
69<tr>
70<td>Age</td>
71<td>
72<asp:textbox id="txAge" runat="server">&amp;   
73lt;/asp:textbox&gt;   
74  
75<glb:globalizedrangevalidator &="" <="" controltovalidate="txAge" display="static" errormessage="Age is invalid." gt;="" id="rngValidator1" maximumvalue="199" minimumvalue="1" runat="server" td="" text="0 &lt; age &lt; 200" type="   
76;Integer">
77</glb:globalizedrangevalidator></asp:textbox></td></tr>
78<tr><td> </td></tr>
79<tr>
80<td align="middle" colspan="2">
81<glb:globalizedlinkbutton id="btUpdate" runat="server" text="Update   
82"></glb:globalizedlinkbutton>   
83  
84    
85  
86<glb:globalizedlinkbutton id="btCancel" runat="server" text="Cancel   
87">   
88  
89Cancel</glb:globalizedlinkbutton>
90</td>
91</tr>
92</table>
93
94</body>
95</html>

例子5

///

 1<summary>   
 2  
 3/// Button that pops client-side dialog to confirm the action. It exposes   
 4  
 5/// ConfirmMessage property to set its dialog message with "Are you sur   
 6e?"   
 7  
 8/// being a default. It relies on GlobalizedButton class to translate   
 9  
10/// its Text and ToolTip properties.   
11  
12/// </summary>

public class GlobalizedConfirmButton : Button

{

private string m_ConfirmMsg = "Are you sure?";

///

1<summary>   
2  
3/// Message will be translated and displayed in confirmation dialog box.   
4  
5/// </summary>

public string ConfirmMessage

{

set{m_ConfirmMsg = value;}

}

///

1<summary>   
2  
3/// Renders HTML and client javascript for confirmation box   
4  
5/// </summary>

///

1<param name="output"/>

protected override void Render(HtmlTextWriter output)

{

if (Page.Request.Browser.JavaScript == true)

{

output.WriteLine(@"

1<script id="clientConfirmation" language="javascript"><!--   
2  
3function RUSure()   
4  
5{if (confirm('" + Translator.GetLocalizedString(m_ConfirmMsg)   
6  
7\+ @"')) return true;else return false;}   
8  
9//--></script>

");

Attributes.Add("onclick","return RUSure();");

}

Text = Translator.GetLocalizedString(Text);

base.Render(output);

}

}

例子6

decimal d = 1234.556M;

d.ToString("C",new System.Globalization.CultureInfo("nl-NL&qu
ot;));

例子7

1<configuration>
2<system.web>
3<globalization culture="en-CA" fileencoding="utf-8" requestencoding="utf-8" responseencoding="utf-8" uiculture="de-DE"></globalization>
4</system.web>
5</configuration>

例子8

1@ Page language="c#" Culture="ru-RU" ResponseEncodi   
2ng="utf-8" 

例子9

///

1<summary>   
2  
3/// Page sets its Culture according to the user profile   
4  
5/// to affect all the formatting on this page   
6  
7/// </summary>

public class LocalizedPage: Page

{

public LocalizedPage()

{

this.Culture = Utility.GetUserContextCulture();

}

}

例子10

System.Threading.Thread.CurrentThread.CurrentUICulture =

new System.Globalization.CultureInfo("de-DE");

Published At
Categories with Web编程
Tagged with
comments powered by Disqus