Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 236
Herin Avatar asked Nov 11 '22 22:11

Herin


1 Answers

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.

like image 59
Purnil Soni Avatar answered Nov 15 '22 12:11

Purnil Soni