页面在Master Page中切换语言,但在刷新之前不在PageContent中切换

时间:2013-06-28 17:27:27

标签: c# asp.net webforms multilingual page-lifecycle

我目前正在开发一个需要在法语和英语之间切换的网站,我们正在使用.resx字符串文件和CurrentCulture + CurrentUICulture

我们目前有一个Button可以将文化切换为我们母版页的一部分,该母版页调用一段代码来进行切换并设置cookie以记住访问之间的语言

我们遇到的问题是MasterPage按预期切换语言,但页面内容保留旧语言,直到下一页加载

BaseMaster类(所有主页继承自此)

    public class BaseMaster : MasterPage
    {
        protected void btn_Language_Click(object sender, EventArgs e)
        {
            Tools.ToggleLanguage();
        }
    }

BasePage类(站点中的所有页面都从此继承)

    public class BasePage : Page {



    protected override void InitializeCulture()
    {
        base.InitializeCulture();
        Tools.CheckLanguageCookie();

    }

}

语言切换

    public static void ToggleLanguage()
    {
        var lang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en" ? "fr-CA" : "en-CA";

        SetLanguage(lang);
    }

    public static void SetLanguage(string language)
    {
        if (language != "en-CA" && language != "fr-CA")
            language = "en-CA";

        HttpCookie cookie = new HttpCookie("Lang"){Value = language, Expires = DateTime.Now.AddYears(1)};
        HttpContext.Current.Response.Cookies.Add(cookie);


        Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

    }

    public static void CheckLanguageCookie()
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["Lang"];

        SetLanguage(cookie != null ? cookie.Value : "en-CA");
    }

0 个答案:

没有答案