我正在尝试一些多语言网站,我现在在Dictionary对象中有价值,现在我想根据我的语言选择设置这些文本。下面是 是我的代码。如何在控制内设置数值
的Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.aspx.cs" Inherits="MultiligualApplication.LoginForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
string className = "ClassEnLang";
if (Session["lang"] != null)
{
if (Session["lang"].ToString().Equals("fr-FR"))
{
className = "ClassFrLang";
}
}
if (className.Equals("ClassEnLang"))
dict = MultiligualApplication.ClassEnLang.dictionary;
else if (className.ToString().Equals("ClassFrLang"))
{
dict = MultiligualApplication.ClassFrLang.dictionary;
}
%>
<table style="width:100%;">
<tr>
<td colspan="3">
<asp:DropDownList ID="ddlLanguage" runat="server"
onselectedindexchanged="ddlLanguage_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="en-US">English</asp:ListItem>
<asp:ListItem Value="fr-FR">French</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="3"><%=dict["loginhere"] %></td>
</tr>
<tr>
<td><%=dict["userid"] %></td>
<td>
<input id="txtUserId" type="text" runat="server" placeholder="<%= dict['userid']%>"/></td>
<td>
</td>
</tr>
<tr>
<td><%=dict["password"]%></td>
<td>
<input id="txtPassword" type="text" runat="server" placeholder="<%= dict['password']%>"/></td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblErrorMsg" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input id="btnLogin" type="submit" value="<%= dict["submit"]%>" runat="server"/></td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MultiligualApplication
{
public partial class LoginForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["lang"] = "en-US";
}
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
Session["lang"] = ddlLanguage.SelectedItem.Value.ToString();
}
}
}
英语课堂的一个文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MultiligualApplication
{
public class ClassEnLang
{
public static Dictionary<string, string> dictionary = new Dictionary<string, string>();
static ClassEnLang()
{
dictionary.Add("loginhere", "Login Here");
dictionary.Add("userid", "User Id");
dictionary.Add("password", "Password");
dictionary.Add("submit", "Submit");
}
}
}
现在我得到的问题时<%=字典[”提交“]%>和文本框的占位符内的”字典“值设置。它没有显示出适当的价值。是我错过了一些东西,或者我正在使用一些错误的内联标签来获取值。请指导。
你为什么不使用内置的本地化机制?基于resx文件。示例可以在这里找到 - http://msdn.microsoft.com/en-us/library/c6zyy3s9(v=vs.98).aspx –
当然,我会尝试,但你能帮我设置值内的占位符使用当前代码? –
@SergeyLitvinov我将如何使用全球化来设置占位符值,因为我将仅使用此权限为文本字段设置值?如果我错了,请纠正我。 –