Asp Mvc Resources Global不起作用

时间:2017-02-10 07:45:21

标签: c# asp.net-mvc translation

这是我的方案:我有一个多语言的Asp.Net Mvc应用程序。 这是我的本地化默认路线:

routes.MapRoute(
    name: "DefaultLocalized",
    url: "{lang}/{controller}/{action}/{id}",
    constraints: new { lang = @"(\w{2})|(\w{2}-\w{2})" },   // en or en-US
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

我的ControllerActivator:

public class LocalizedControllerActivator : IControllerActivator
{
    private string _DefaultLanguage = "it";

    public IController Create(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        //Get the {language} parameter in the RouteData
        string lang = (string)requestContext.RouteData.Values["lang"] ?? _DefaultLanguage;

        if (lang != _DefaultLanguage)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            }
            catch (Exception e)
            {
                throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
            }
        }
        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}

我在Global.asax中初始化:

ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new App_Start.LocalizedControllerActivator()));

这是重定向本地化路线的方法:

public ActionResult Language(string languageCode)
{
    //ViewBag.lang = languageCode;
    string basePath = string.Format("{0}://{1}{2}", Request.UrlReferrer.Scheme, Request.UrlReferrer.Authority, Url.Content("~"));
    object lang;
    if (!RouteData.Values.TryGetValue("lang", out lang))
        return Redirect(Request.UrlReferrer.ToString().Replace(basePath, string.Format("{0}{1}/", basePath, languageCode)));
    else
    {
        string basePathWithLang = string.Format("{0}{1}", basePath, lang.ToString());
        return Redirect(Request.UrlReferrer.ToString().Replace(basePathWithLang, string.Format("{0}{1}", basePath, languageCode)));
    }
}

问题:

在我的机器上工作就像一个魅力,但当我加入我使用的外部托管时,我在全局资源文件中使用的单词不起作用。 在我的主页上,我有语言选择:

<li><a href="@Url.Action("Language","Home",new { languageCode="it" })">Italiano</a></li>
<li><a href="@Url.Action("Language","Home",new { languageCode="en" })">English</a></li>

包含在:     @ Resources.Global.contacts 如果我改变语言不翻译。也许这会有所帮助:当我在abcxxx.it打开我的网站时,起始语言是英语,而不是我在代码中设置为默认的意大利语。

1 个答案:

答案 0 :(得分:1)

你知道吗,代码没有问题。这是逻辑问题。

您将_DefaultLanguage设为it

现在当你从lang的路线值中获得it时。所以它不会进入块if (lang != _DefaultLanguage),因为它们都是相同的,并且不会更改CurrentThread的CurrentCultureCurrentUICulture。它将保持不变并设置为en-US

所以您需要做的就是删除if子句,并在任何情况下将lang分配给CurrentThread的CurrentCultureCurrentUICulture

public class LocalizedControllerActivator : IControllerActivator
{
    private string _DefaultLanguage = "it";

    public IController Create(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        //Get the {language} parameter in the RouteData
        string lang = (string)requestContext.RouteData.Values["lang"] ?? _DefaultLanguage;

        try
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
        }
        catch (Exception e)
        {
            throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
        }
        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}

这应解决问题。