Asp Mvc资源全球无法正常工作 [英] Asp Mvc Resources Global doesn't work
问题描述
这是我的情况:我有一个多语言的Asp.Net Mvc应用程序.这是我的本地化默认路由:
this is my scenario: I have a multilanguage Asp.Net Mvc Application. Here is my localized default route:
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:
My 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中初始化的
That I initialize in Global.asax:
ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new App_Start.LocalizedControllerActivator()));
这是重定向本地路由的方法:
And this is the method that redirects localized routes:
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)));
}
}
问题:
在我的机器上工作时就像一个咒语,但是当我放置要使用的外部主机时,在全局资源文件中使用的单词将不起作用.在我的主页中,可以选择语言:
On my machine works like a charm, but when I put on the external hosting that I work with, words that I use in global resources files, doesn't work. In my home page I have language selection:
<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上打开网站时,其初始语言为英语,而不是我在代码中设置为默认语言的意大利语.
The word contained in: @Resources.Global.contacts if I change language is not translated. Maybe this can help: when I open my website at abcxxx.it the start language is english, instead of italian that I set as default in my code.
推荐答案
您知道吗,代码没有问题.这是逻辑上的问题.
You know what, there is no problem with the code. It's the problem with logic.
您正在将 _DefaultLanguage
设置为 it
.
现在,当您从也以 it
出现的路由值中获取 lang
时.因此,它不会进入块 if(lang!= _DefaultLanguage)
内,因为它们都相同,并且CurrentThread的 CurrentCulture
和 CurrentUICulture
不会相同改变了.它将保持不变,并设置为 zh-CN
.
And now when you are getting lang
from the routevalues that also come as it
. So it won't go inside block if (lang != _DefaultLanguage)
because they both are same and CurrentThread's CurrentCulture
and CurrentUICulture
won't be changed. It will remain the unchanged and set to en-US
.
所以您需要做的是删除 if
子句,并将' lang
分配给CurrentThread的 CurrentCulture
和 CurrentUICulture
无论如何.
So what you need to do is remove if
clause and assign 'lang
to the CurrentThread's CurrentCulture
and CurrentUICulture
in any case.
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;
}
}
这应该可以解决问题.
这篇关于Asp Mvc资源全球无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!