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

类别:.NET开发 点击:0 评论:0 推荐:
Globalized Web Applications & ASP.NET

by Max Poliashenko and Chip Andrews


例子1

(a)
<INPUT type=text value= <%=GetString(unTranslatedString ) %>>

(b)

Response.Write("<SELECT>")

For Each row In myRecordset

Reponse.Write("<OPTION")

If myRecordset!ID.value = oldSelection Then

Response.Write(" SELECTED>")

Else

Response.Write(">")

End If

Response.Write(GetLocalizedString(myRecordset!MyField.value)&

"</OPTION>")

Next

Response.Write("</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)

<%@ Page language="c#" %>

<%@ Register TagPrefix="glb" NameSpace="GlobalControls&quo
t;

Assembly="GlobalComponents&quo
t;%>

<HTML>

<BODY>

<glb:GlobalizedLabel id=lblGlobal runat=server>Test Label

</glb:GlobalizedLabel>

</BODY>

</HTML>




例子3

/// <summary>

/// Validation messages are translated

/// </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

{

/// <summary>

/// This header will be translated

/// </summary>

public new string HeaderText

{

get{return base.HeaderText;}

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

}

}





例子4

<%@ Page language="c#" %>

<%@ Register TagPrefix="glb" NameSpace="AspNetDemo.Compone
nts"

Assembly="Components"
;%>

<HTML>

<HEAD>

<meta content="Microsoft Visual Studio 7.0" name="GENE
RATOR">

<meta content="C#" name="CODE_LANGUAGE">

</HEAD>

<body bgColor="#e4e4e0">

<h3 align="center">User Information</h3>

<form id="Form2" method="post" runat="server&q
uot;>

<glb:GlobalizedValidationSummary id="vSummary" runat="
server"

headertext="The following validation errors occurred:" />

<hr>

<table cellSpacing="0" cellPadding="0" width=&quo
t;100%" border="0">

<tr>

<td width="30%">Name</td>

<td>

<asp:textbox id="txName" runat="server">
</asp:textbox>

<glb:GlobalizedRequiredFieldValidator id="rfValidator1&quo
t;

runat="server" display=&
quot;dynamic"

controltovalidate="txName" ErrorMessage="Name is re
quired."

Text="required
" />

</td>

</tr>

<tr>

<td>Password</td>

<td>

<asp:textbox id="txPassword" TextMode="Password&
quot;

runat="server"></asp:textbox>

<glb:GlobalizedRequiredFieldValidator id="rfValidator2&quo
t;

controltovalidate="txPassword" runat=&
quot;server"

ErrorMessage="Password is required."

Text="minimum 5 characters" display=&q
uot;dynamic" />

</td>

</tr>

<tr>

<td>Confirm password</td>

<td>

<asp:textbox id="txPasswordConf"

TextMode="Password" runat="
server" />

<glb:GlobalizedCompareValidator id="cmpValidator1"

runat="server" display="Dyn
amic"

controltovalidate="txPasswordConf"

ErrorMessage="Passwords don't match. Please, re-
enter."

ControlToCompare="txPassword" operator="Equal"

type="String" Text="doesn't match"
; />

</td>

</tr>

<tr>

<td>Email</td>

<td>

<asp:textbox id="txEmail" runat="server">
;</asp:textbox>

<glb:GlobalizedRequiredFieldValidator id="rfValidator3&quo
t;

runat="server" display="d
ynamic"

controltovalidate="txEmail" ErrorMessage="Email is
required."

Text="required" />

<glb:GlobalizedRegularExpressionValidator id="EmailRegVali
dator"

validationexpression="\w+@\w+\.\w+" runat="server&q
uot;

ErrorMessage="Email format is invalid.

" controltovalidate="txEmail" di
splay="static">

[email protected]</glb:GlobalizedRegularExpressionValidator>

</td>

</tr>

<tr>

<td>Age</td>

<td>

<asp:textbox id="txAge" runat="server">&
lt;/asp:textbox>

<glb:GlobalizedRangeValidator id="rngValidator1"

runat="server" controltovalidate
="txAge"

MinimumValue="1" MaximumValue="199" Type="
;Integer"

ErrorMessage="Age is invalid."

display="static" Text="0 < age < 200" /&
gt;

</td>

</tr>

<tr><td>&nbsp;</td></tr>

<tr>

<td align="middle" colSpan="2">

<glb:GlobalizedLinkButton id="btUpdate"

runat="server" Text="Update
" />

&nbsp;

<glb:GlobalizedLinkButton id="btCancel"

runat="server" Text="Cancel
">

Cancel</glb:GlobalizedLinkButton>

</td>

</tr>

</table>

</form>

</body>

</HTML>






例子5

/// <summary>

/// Button that pops client-side dialog to confirm the action. It exposes

/// ConfirmMessage property to set its dialog message with "Are you sur
e?"

/// being a default. It relies on GlobalizedButton class to translate

/// its Text and ToolTip properties.

/// </summary>

public class GlobalizedConfirmButton : Button

{

private string m_ConfirmMsg = "Are you sure?";

/// <summary>

/// Message will be translated and displayed in confirmation dialog box.

/// </summary>

public string ConfirmMessage

{

set{m_ConfirmMsg = value;}

}

/// <summary>

/// Renders HTML and client javascript for confirmation box

/// </summary>

/// <param name="output"></param>

protected override void Render(HtmlTextWriter output)

{

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

{

output.WriteLine(@"<script id='clientConfirmation'

language='javascript'><!--

function RUSure()

{if (confirm('" + Translator.GetLocalizedString(m_ConfirmMsg)

+ @"')) return true;else return false;}

//--></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

<configuration>

<system.web>

<globalization

responseEncoding="utf-8"

requestEncoding="utf-8"

fileEncoding="utf-8"

culture="en-CA"

uiCulture="de-DE"

/>



</system.web>

</configuration>






例子8

<%@ Page language="c#" Culture="ru-RU" ResponseEncodi
ng="utf-8" %>






例子9

/// <summary>

/// Page sets its Culture according to the user profile

/// to affect all the formatting on this page

/// </summary>

public class LocalizedPage: Page

{

public LocalizedPage()

{

this.Culture = Utility.GetUserContextCulture();

}

}



例子10

System.Threading.Thread.CurrentThread.CurrentUICulture =

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

本文地址:http://com.8s8s.com/it/it46045.htm