Localization of Required field validation message based on language selection from application in MVC

5.3k views Asked by At

I am developing one MVC4 application, in that there is option for language seletion in my application & based on that selected lanaguage, all label & messages are displayed.In this application i have put one required field validation of MVC, and it will display validation message in english only.I have created two different xml file for Localization in my application folder i.e Resource.resx for english lanaguage & Resource.de-DE.resx for German langauge. I have used the following code in my Model class :

    [Required(ErrorMessageResourceType = (typeof(MyFolder.MyResource.Resource)), ErrorMessageResourceName= "FromDateRequiredMessage")]
    [Display(Name = "FromDate", ResourceType = typeof(MyFolder.MyResource.Resource))]
    public DateTime FromDate { get; set; }

So here i have Resource.resx xml file and in that i have Key is 'FromDateRequiredMessage' which is same key as in Resource.de-DE.resx german file but different value.Also i have make the follwing changes in my in web.config file for globalization :

<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto"/>

So, i want that when i select the german language from my application, then it should show me validation error in german language.But here it will show me validation message in english only.

So, now what can i do for this? Please hel me.

3

There are 3 answers

0
Purnil Soni On BEST ANSWER

Please use method Application_PreRequestHandlerExecute() in your application's Global.asax file. This method will execute before any page request or its validation.

Also set the current culture value in this method.

0
Milad Hosseinpanahi On

Simple answer > nothing is wrong!

when you put culture=auto in web.config it works this way, your application will look at the header of the incoming request and look for accept languages and the order of them, so in one request in it you have something like En - Fa ... the application will select the first one and set culture an ui culture to that, so go to your browser language settings and change the order of the languages to test your application.

0
Somedeveloper On

In order to be able to do this you will need to set the culture. So will provide the user with selection in dropdown etc which they explicity select German,English etc, this will change yoursite to be yoursite/En or yoursight/Gr etc. This article explains in well and gives sample code. http://www.codeproject.com/Articles/207602/Creating-a-Bilingual-ASP-NET-MVC3-Application-Part the part "Adding UI support to switch languages" has what you need i believe.