有没有人知道你是否可以控制ModelState对象的文化,我在我的多语言应用程序中面临一个问题,其中语言基于你从哪个子域访问网站ex:
italia.domain.com - “将文化变为意大利语”
german.domain.com - “将文化改为德语”
问题是提交表单时ModelState对象上的语言似乎是由客户端浏览器而不是当前线程文化控制的。
所以即时搜索我可以修改此行为或覆盖它的解决方案,以便我的意大利子域上的语言始终是意大利语而不是客户端浏览器的语言。
修改
我已经根据子域名做了改变语言的部分:
var HttpHost = HttpContext.Request.ServerVariables["HTTP_HOST"];
var _hostname = (HttpHost.Split(':').Length > 1) ? HttpHost.Substring(0, HttpHost.IndexOf(':')) : HttpHost;
var allowedHostnames = "italiansubdomain.domain.com|it,frenchsubdomain.domain.com|fr,germansubdomain.domain.com|de,englishsubdomain.domain.com|en".Split(',');
foreach (var hostname in allowedHostnames)
{
if (hostname.StartsWith(_hostname.ToLower()))
{
var lang = hostname.Split('|').Last();
if (lang == "en") lang = "uk";
// Updates the cultures for the dynamic language
var ci = new CultureInfo(lang);
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}
}
所以我的问题是,当我像这样使用Modelstate验证时:
public class Email {
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), StringLength(50, MinimumLength = 2, ErrorMessageResourceName = "validation_string_length_2_50", ErrorMessageResourceType = typeof(Resources.Master))]
public string SenderName { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), Email(ErrorMessageResourceName = "validation_email_invalid", ErrorMessageResourceType = typeof(Resources.Master))]
public string SenderEmail { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), StringLength(50, MinimumLength = 2, ErrorMessageResourceName = "validation_string_length_2_50", ErrorMessageResourceType = typeof(Resources.Master))]
public string ReceiverName { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), Email(ErrorMessageResourceName = "validation_email_invalid", ErrorMessageResourceType = typeof(Resources.Master))]
public string ReceiverEmail { get; set; }
public string Comment { get; set; }
}
检查模型状态验证部分:
if (!ModelState.IsValid) {
var keys = ModelState.Keys.ToList();
var values = ModelState.Values.ToList();
for (var i = 0; i < keys.Count; i++)
{
var value = values[i];
if (value.Errors.Count > 0)
{
response.AddError(keys[i], value.Errors[0].ErrorMessage);
}
}
}
当我然后通过Ajax响应结果访问错误或仅通过调试我接收基于浏览器语言设置的错误消息时,我想将其更改为当前活动的语言。
修改
提前致谢,bsthomsen
答案 0 :(得分:3)
我通过将文化被更改的代码从Controller.OnActionExecuting移动到Controller.ExecuteCore来解决这个问题
答案 1 :(得分:0)
此libk应该有助How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
将全球化部分添加到Web.config文件
<globalization uiCulture="es" culture="es-MX" />
或programmaticaly,覆盖页面的InitializeCulture
方法(WebForms)
protected override void InitializeCulture()
{
UICulture = selectedLanguage ;
Culture = selectedLanguage ;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
base.InitializeCulture();
}
答案 2 :(得分:0)
我曾经处理的情况并不完全相同但很相似。您应该能够将当前线程的UICulture设置为特定语言,并且您应该按照预期的方式工作。因此,请确定请求所针对的子域,并相应地设置文化。查看更多:http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx
答案 3 :(得分:0)
您可以轻松覆盖Global.asax.cs
在您的情况下,您可以检查传入的URL并相应地设置文化。
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
//Create culture info object
CultureInfo ci = new CultureInfo("en");
if(Request.Url.Host.Equals("italia.domain.com",
StringComparison.InvariantCultureIgnoreCase))
{
ci = new CultureInfo("it");
}
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(ci.Name);
}