我正在创建一个多语言网站,我有一些文件:
- 登录页面。
- 主页。
- 子页面使用母版页。
在login
页面中,我使用链接控件由用户选择语言:
<table class="borderlang" style="position: absolute; top: 5px; left: 5px; width: auto; height: auto;">
<tr>
<td style="vertical-align: middle;">
<asp:Label ID="Label1" runat="server" Text="Language :" meta:resourcekey="Label1Resource1"></asp:Label>
</td>
<td>
<a href="?lang=vi" runat="server" id="linkVietnameseLang">
<img src="Content/Images/VNlogo.jpg" style="height: 20px; width: auto; padding-left: 10px;" /></a>
</td>
<td>
<a href="?lang=en" runat="server" id="linkEnglishLang">
<img src="Content/Images/USALogo.png" style="height: 20px; width: auto; padding-left: 10px;" /></a>
</td>
</tr>
</table>
我使用BasePage.cs
代码:
public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
if (!string.IsNullOrEmpty(Request["lang"]))
{
Session["lang"] = Request["lang"];
}
string lang = Convert.ToString(Session["lang"]);
string culture = string.Empty;
if (lang.ToLower().CompareTo("en") == 0)
{
culture = "en-US";
}
if (lang.ToLower().CompareTo("vi") == 0 || string.IsNullOrEmpty(culture))
{
culture = "vi-VN";
}
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
base.InitializeCulture();
}
}
所以在登录页面我使用:
public partial class Login : BasePage
使用BasePage。
在这个表格中有一些项目我不能使用本地资源,所以我使用了2个资源:global resource
和local resource
。但是我不在乎,因为当我在link ->中选择语言时,语言会改变,全局和局部资源也会改变。
现在,当我登录,网站将重定向到一个页面使用母版页。在主页上,我为用户设置了一个下拉菜单,以防他们再次更改语言:
<asp:DropDownList ID="ddlLanguage" runat="server"
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged"
AutoPostBack="True" meta:resourcekey="ddlLanguageResource1">
<asp:ListItem Text="Tiếng ViệT" Value="vi-VN" meta:resourcekey="ListItemResource1" />
<asp:ListItem Text="EnglisH" Value="en-US" meta:resourcekey="ListItemResource2" />
</asp:DropDownList>
在母版页我添加了:
if (!Page.IsPostBack)
{
ddlLanguage.SelectedValue = Thread.CurrentThread.CurrentCulture.Name;
}
:
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = ddlLanguage.SelectedValue;
Response.Cookies.Add(cookie);
//Set the culture and reload the page for immediate effect.
//Future effects are handled by Global.asax
Thread.CurrentThread.CurrentCulture =
new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(ddlLanguage.SelectedValue);
Server.Transfer(Request.Path);
}
在全球。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["CultureInfo"];
if (cookie != null && cookie.Value != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
Thread.CurrentThread.CurrentCulture = new CultureInfo(cookie.Value);
}
else
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
}
}
问题1:如何使用session或cookie的链接值(在登录页)到母版页:ddlanguage value = session或cookie的lang ??
现在,在子页中使用母版页。我必须使用本地资源,因为如果使用全局资源,我必须使用<%$ Resources:nameresource, keysource%> ->
太慢。而且我有一个问题:在具有本地资源的子页面中,文化和文化值不取决于母版页中的ddlanguage值。
问题2:如何在子页中设置culture和uiculture值=母版页中的ddlanguage值
和结果是:lang value in login page = ddlanguage
下拉值在master page = culture
和uiculture in child page
使用该母版。但是可以改变母版和文化的ddlanguage值,and
在子页的文化也会改变。
.
我是一个业余的编码,所以我希望有解决我的情况。非常非常感谢(很抱歉我的英语不好)。
In program.cs
user name can set Globals variable
namespace GlobalVariables
{
public static class Globals
{
static Globals()
{
userName="";
}
public static void setusername(int[] newInt)
{
setusername = newInt;
}
public static string userName { get; private set; }
}
you can set or get every child or parent form
inherit namespace like in another child or parent form
using GlobalVariables;
to set Globals session or cookiee
Globals.setusername(userName.ToString()));
you can check and restrict page
username=Globals.userName;
Response.Cookies("userInfo")("userName") = "rangarajakrishnan"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Dim aCookie As New HttpCookie("userInfo")
aCookie.Values("userName") = "mike"
aCookie.Values("lastVisit") = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)