错误信息:型号不拾取特定语言的资源文件 [英] Error Message: Model not picking language-specific resource file
问题描述
我要显示在西班牙模式错误消息,我已经定义在文件国土资源的字符串。我也做了其他字符串同样使用剃刀语法在页面上而是从视图模型标注的那些不被采摘。
I want to display model error message in spanish and I have defined those string in resourse files. I have done the same for other string on the page using razor syntax but the ones from ViewModel annotation are not being picked.
它实际上是捡的默认值 - 英语。所以我的猜测是,可能是语言/文化没有被检测到,但页面上的其他字符串都显示在西班牙
It is actually picking the default values - English. So my guess is that may be the language/culture was not detected, but other string on the page are displayed in spanish
//Spanish: El campo {0} se requiere
//English: The {0} field is required <--- this comes out always irrespective of set language
[Required(ErrorMessageResourceName = "ErrorMessage_Required",
ErrorMessageResourceType = typeof(GlobalResources.Resources))]
[Display(Name = "CardNumber", ResourceType = typeof(GlobalResources.Resources) )]
public string CardNumber { get; set; }
我设置的语言在我的控制器
I set the language in my Controller
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
如何将文化设置延伸到的ViewModels?
How do I extend the culture settings to ViewModels?
更新
类似的帖子:<一href=\"http://stackoverflow.com/questions/7202607/mvc3-globalization-need-global-filter-before-model-binding\">MVC3全球化:全球需要过滤模型前结合
更新
改变使它工作我的浏览器设置preferred语言。这意味着正在使用的不受 System.Threading.Thread.CurrentThread.CurrentUICulture =新System.Globalization.CultureInfo(郎),这个设置模型属性;
。的有没有办法做到这一点? - 仍在寻找.... 的
更新:移动code到 Application_AcquireRequestState
似乎解决它。
Update: Moving the code to Application_AcquireRequestState
seems to solve it.
protected void Application_AcquireRequestState()
{
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
<一个href=\"http://stackoverflow.com/questions/16218382/thread-currentthread-currentuiculture-vs-accept-language-in-viewmodels/16218779?noredirect=1#16218779\">The解释我,还张贴在这个问题上的链接发现,这是为时已晚,该模型利用文化的控制器重写方法被设置为调用该方法之前就已经发生了绑定。 这种联系是有益的
The explanation I got, also found in the link posted in this question, that it was too late for the model to make use of the culture being set in controller overridden method as the binding had already occurred before the method is called. This link was helpful
推荐答案
请确保您设置的两个 的CurrentCulture
和的CurrentUICulture
。我怀疑的CurrentCulture
其实就是资源文件正在寻找:
Make sure you set both CurrentCulture
and CurrentUICulture
. I suspect CurrentCulture
is actually what the resource files are looking at:
//I've taken off System.Threading, add a using to it (aids readability)
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
这篇关于错误信息:型号不拾取特定语言的资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!