问题
I have in my project mainMaster page were locates imageButtons:
<asp:ImageButton ID="RU" ImageUrl="/Images/RU.png" runat="server" onclick="RU_Click">
<asp:ImageButton ID="USA" ImageUrl="/Images/USA.png" runat="server" onclick="USA_Click" />
here's OnClick functions:
protected void RU_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("ru-RU");
Server.Transfer(Request.Url.LocalPath);
}
protected void USA_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-AU");
Server.Transfer(Request.Url.LocalPath);
}
also I have two .resx files but how cane I link press buttons with
<asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:Main, Name%>" />
回答1:
Your question is not clear, but let me clear one thing up for you that might be your problem.
You can set Culture only in the InitializeCulture
event:
protected override void InitializeCulture()
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-AU");
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-AU");
base.InitializeCulture();
}
回答2:
Use this code:
protected void RU_Click(object sender, EventArgs e)
{
SetCulture("ru-RU");
}
protected void USA_Click(object sender, EventArgs e)
{
SetCulture("en-UA");
}
public static void SetCulture(string culture)
{
CultureInfo cultureInfo = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
}
to set current culture, so localization could take effect.
来源:https://stackoverflow.com/questions/6097520/localize-application-on-button-click