Does anyone know if you can control the culture of the ModelState Object, im facing a problem in my multilingual application where the language is based on which subdomain from which you visit the site ex:
italia.domain.com - "Changes the culture to Italian"
german.domain.com - "Changes the culture to German"
The issue is that the language on the ModelState object when submitting a form seems to be controlled by the Clients browser and not the current threads culture.
So im searching for a solution where i can modify this behavior or override it so that the language on my italian subdomain always is italian and not the language of the clients browser.
EDIT
I already made the part where i change language based on subdomain:
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;
}
}
So my problem is that when i use Modelstate validation like this:
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; }
}
And the check for modelstate validation part:
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);
}
}
}
When i then access the Errors through a Ajax Response result or just by debugging im receiving the error messages based on the browsers language setting, this is where i want to change it to be the language that is currently active instead.
EDIT
Thanks in advance, bsthomsen