多语种网站代码无效

时间:2013-03-07 09:27:02

标签: c# asp.net multilingual globalization global-asax

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

    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");
    }
}

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

Resource Files

然而,当你点击法国国旗时没有任何反应。我已经完成了代码并且正在设置cookie并运行Thread.CultureInfo行。

关于还有哪些方法可以检查?

感谢。

1 个答案:

答案 0 :(得分:0)

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

using System;

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

/// ///翻译的摘要说明 /// 公共类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

{

}

解决了这个问题。