Localize application on button click

白昼怎懂夜的黑 提交于 2019-12-23 18:30:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!