MVC 3,文化永远不变,Thread.CurrentThread.CurrentUICulture丢失了信息 [英] MVC 3, Culture never change, Thread.CurrentThread.CurrentUICulture lost info

查看:73
本文介绍了MVC 3,文化永远不变,Thread.CurrentThread.CurrentUICulture丢失了信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试在mvc3中建立自己的第一个网站.
我阅读了很多有关多语言网站的文章,并尝试了不同的方法,但未取得真正的成功.
我的最后一个策略是:在url中传递区域性信息,并使用routemanager设置良好的区域性:

Hi,

I try to do my first own website in mvc3.
I read a lot of article about multilanguage website and tried different approach without real success.
My last tactic is : to pass the culture info in url and use routemanager to set the good culture:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
             "Default", // Route name
             "{controller}/{action}/{id}", // URL with parameters
             new { controller = "Home", action = "Home",
              id = UrlParameter.Optional } // Parameter defaults
        );

        foreach (Route r in routes)
        {
            if (!(r.RouteHandler is SingleCultureMvcRouteHandler))
            {
                r.RouteHandler = new MultiCultureMvcRouteHandler();
                r.Url = "{culture}/" + r.Url;
                //Adding default culture
                if (r.Defaults == null)
                {
                    r.Defaults = new RouteValueDictionary();
                }
                r.Defaults.Add("culture", CultureEnum.en);

                //Adding constraint for culture param
                if (r.Constraints == null)
                {
                    r.Constraints = new RouteValueDictionary();
                }
                r.Constraints.Add("culture",
                new CultureConstraint(CultureEnum.en, CultureEnum.fr));
            }
        }
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        //CultureHelper.SetCulture(Thread.CurrentThread.CurrentCulture);
    }
}

#region Localisation Class
public class SingleCultureMvcRouteHandler : MvcRouteHandler { }

public class MultiCultureMvcRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(
         RequestContext requestContext)
    {
        var culture = requestContext.RouteData.Values["culture"].ToString();
        var ci = new CultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture(ci.Name);
        //CultureHelper.SetCulture(Thread.CurrentThread.CurrentCulture);

        return base.GetHttpHandler(requestContext);
    }
}

public class CultureConstraint : IRouteConstraint
{
    private string[] _values;
    public CultureConstraint(params string[] values)
    {
        this._values = values;
    }

    public bool Match(HttpContextBase httpContext,
    Route route, string parameterName,
    RouteValueDictionary values, RouteDirection routeDirection)
    {

        // Get the value called "parameterName" from the
        // RouteValueDictionary called "value"
        string value = values[parameterName].ToString();
        // Return true is the list of allowed values contains
        // this value.
        return _values.Contains(value);
    }
}

public class CultureEnum
{
    public static string fr = "fr-FR";
    public static string en = "en-US";
}
#endregion



路由管理器工作正常.每当我切换语言时,我都会看到url的变化.正确设置Thread.CurrentThread.CurrentUICulture和Thread.CurrentThread.CurrentCulture.但是:
-在页面中的所有时间,它都是显示的默认语言(中性resx).
-如果再次更改语言,我看到CurrentUICulture和CurrentCulture拥有我的用户语言(fr),但仍显示中性默认值.

我尝试将我的3个resx文件(toto.resx,toto.fr-FR.resx,toto.en-US.resx)放在多个位置(全局,本地,myproject.Resources等...),保持不变. br/> 这一直是显示默认resx的时间.

我尝试将resourcesgenerator更改为ResXFileCodeGenerator,PublicResXFileCodeGenerator等,但是结果相同.

经过漫长而艰难的一天后,我发现的唯一技巧是:



The routemanager work well. All time I switch the language, I saw the url change. The Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture are set propertly. But:
- All time in page, it''s the default language (neutral resx) who is displayed.
- If change my language again, I saw CurrentUICulture and CurrentCulture have my user language (fr) yet but still display the neutral default.

I try to put my 3 resx files (toto.resx, toto.fr-FR.resx, toto.en-US.resx)in multiple locations (global, local, myproject.Resources, etc...) nothing change.
It''s all time the Default resx displayed.

I try to change the resourcesgenerator as ResXFileCodeGenerator, PublicResXFileCodeGenerator, etc... but same results.

The only trick, I found after one long and hard day is:

public static void SetCulture(CultureInfo newCulture)
{
            if (newCulture == null)
                return;

            var classLi = from t in Assembly.GetExecutingAssembly().GetTypes()
                          where t.IsClass && t.Namespace == ResourcesNamespace
                          select t;

            foreach (Type t in classLi)
            {
                PropertyInfo pi = t.GetProperty("Culture");
                if (pi != null)
                {
                    pi.SetValue(t, newCulture, null);
                }
            }
        }
    }



很难看,但是可以用.但我担心此解决方案将因多个用户而失败.


因此,女士们,先生们,如果您愿意分享您的想法:)



It''s ugly but it works. But I a afraid this solution will fail with multiple users.


So ladies and gentlemen if you have the kindness to share your ideas :)

推荐答案



以下教程将逐步引导您逐步尝试完成操作.使用资源文件和文化信息来提供翻译.

使用ASP.NET开发多语言网站 [
Hi,

The following tutorial takes you step by step through exactly what you''re trying to do; Using resource files and culture information to provide translations.

Developing Multi Language Web Sites with ASP.NET[^]


这篇关于MVC 3,文化永远不变,Thread.CurrentThread.CurrentUICulture丢失了信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆