1

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

我们目前有一个按钮来切换文化作为我们母版页的一部分,它调用一段代码来进行切换并设置一个 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");
    }
4

0 回答 0