1

我有一个网站一直运行良好,直到我不得不删除一些不正确的资源文件。但是,这可能不是它不起作用的原因。基本上,像大多数多语言网站一样,用户可以通过单击标志来更改语言:

    protected void imbEnglish_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("en-GB");
}
protected void imbFrench_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("fr-FR");
}
protected void imbGerman_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("de-DE");
}
protected void imbSpanish_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("es-ES");
}
protected void imbItalian_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("it-IT");
}
protected void imbPolish_Click(object sender, ImageClickEventArgs e)
{
    SetCultureStoreCookie("fr-FR");
}

protected void SetCultureStoreCookie(string culture)
{
    //Sets the cookie that is to be used by Global.asax
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = culture;
    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(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

    //Response.Redirect(Request.Path);
    Server.Transfer(Request.Path);
}

正如评论中所说,文化的后续设置正在 Global.asax 中处理:

    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("en-GB");
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
    }
}

所有页面,包括母版页都有英文(默认)资源文件,我已经开始创建法文资源文件:

资源文件

但是,当您单击法国国旗时,没有任何反应。我已经单步执行了代码,正在设置 cookie 并运行 Thread.CultureInfo 行。

关于在哪里检查的任何想法?

谢谢。

4

1 回答 1

0

我通过改变它的工作方式解决了这个问题。显然,MasterPage 类没有一个名为 InitializeCulture 的方法,使用我在其他帖子中看到的一些技巧,我创建了一个 BaseClass 页面:

using System;

使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Threading;使用 System.Globalization;

/// /// 翻译摘要说明 /// public class BasePage : System.Web.UI.Page { public BasePage() { // // TODO: 在此处添加构造函数逻辑 // }

protected override void InitializeCulture()
{
    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);

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Value);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
    }
    else
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
        //Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
    }

    base.InitializeCulture();
}

protected void Page_Load(object sender, EventArgs e)
{
    InitializeCulture();
}

}

然后将每个页面更改为从 BaseClass 页面继承:

public partial class AboutUs : BasePage

{

}

这解决了问题。

于 2013-03-08T10:05:47.830 回答