在 ASP.NET 3.5 中为客户端验证实现多语言支持






4.50/5 (3投票s)
本文演示了如何在服务器端和客户端开发多语言页面。
引言
本文讨论了如何在 Visual Studio 2008 中开发多语言网页,并根据相应的语言启用客户端脚本公开的警告消息。
背景
几天前,我正在开发我的应用程序,使其支持多种语言(英语、法语和日语),以满足不同地区的用户需求。
为 aspx 页面启用多种语言非常容易,为此,我们需要为每种语言维护资源文件 (*.resx),并轻松生成资源文件。
要生成资源文件,请在设计模式下打开 aspx 页面,然后转到工具 --> 生成本地资源。

这将为资源文件中的所有 string
值创建一个元资源键,并在标记中插入相同的条目,例如
<asp:Label ID="lblcname" runat="server" Text="Customer Name: *"
meta:resourcekey="lblcnameResource1" />
现在我们需要在页面级别覆盖 InitializeCulture
protected override void InitializeCulture()
{
string culture = string.Empty;
culture = Request.Form["ddlLang"];
if (string.IsNullOrEmpty(culture)) culture = "Auto";
UICulture = culture;
Page.Culture = culture;
if (culture != "Auto")
{
CultureInfo ci = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
}
ddlLang
是一个下拉列表,其中列出了可用的语言。
但我的重点是为客户端开发多语言支持,因此如果 JavaScript 弹出警告,则该警告应使用相应的语言。
Using the Code
为了实现客户端功能,我在 App_globalResources ASP.NET 文件夹下创建了一个资源文件,并且在每个文件中我都维护了相应语言的 JavaScript 消息。
例如,在 Resource-fr-CA 中,messageID 和 Messages 是
CustIDMessage
Identifiant client doit être entier CustNameMessage
Nom du client ne peut pas être vide NoErrorMessage
Aucune erreur trouvée
根据所选的文化,我将这些 messageID
(CustIDMessage
, CustNameMessage
和 NoErrorMessage
) 注册为 JavaScript 变量。
public void GetResourceString()
{
XDocument resDoc = new XDocument();
StringBuilder sb = new StringBuilder();
string resfile = Server.MapPath("~/App_GlobalResources/Resource-" +
ddlLang.SelectedValue.ToString()+".resx");
resDoc = XDocument.Load(resfile);
string MessageKey = null;
string MessageString = null;
Int32 Iteration = 0;
foreach (var el in resDoc.Root.Descendants("data"))
{
MessageString = el.Value;
MessageKey = ((System.Xml.Linq.XAttribute)el.FirstAttribute).Value;
Iteration = Iteration + 1;
string ss = string.Format("var {0} = '{1}';",
MessageKey, MessageString);
sb.AppendLine(ss);
}
Page.ClientScript.RegisterStartupScript(this.GetType(),
"jsMessages", sb.ToString(), true);
}
在上面的代码中,我使用 Linq 的 XDocument
加载资源文件,并迭代 Xdocument
以获取 MessageKey
和 MessageString
。 在这里,我创建脚本以将 MessageKey
注册为变量名,并将 MessageString
作为值。
迭代完成后,我将生成的 string
注册为启动脚本。
在 aspx 页面上,我有一个按钮。 单击该按钮后,它会验证字段并弹出警告。
<asp:Button id="btnvalidate" runat="server"
OnClientClick="return validateEntry();" style="color: #008080;
font-weight: bold" Text="Validate" meta:resourcekey="btnvalidate" />
<script language="Javascript">
function validateEntry()
{
var msg='';
var custid=document.getElementById('custID').value;
var custname=document.getElementById('custName').value;
if(trim(custid)!='' && isNaN(trim(custid)))
{
msg+=CustIDMessage+'\n';
}
if(trim(custname)=='')
{
msg+=CustNameMessage;
}
if(msg=='')
{
alert(NoErrorMessage);
}
else
{
alert(msg);
}
return false;
}
</script>
如果选择的语言是日语,则会生成以下脚本
<script type="text/javascript">
//<![CDATA[
var CustIDMessage = '顧客IDの整数である必要があります';
var CustNameMessage = '顧客名を空白にすることはできません';
var NoErrorMessage = 'しないエラーが検出されました';
//]]>
</script>
当我在客户 ID 中输入 string
并且客户姓名留空时,单击验证按钮,我会收到日语的警告消息。

兴趣点
此实现非常简单,不需要在 JavaScript 中进行硬编码。 由于某些 Unicode 字符,可能会出现问题。