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

I am developing one MVC4 application in which there is an option to select the language in my application and, based on the selected lanaguage, all labels and messages are displayed. In this application, I placed one required check of the MVC field, and this will display the check message in English only. I created two different xml files for localization in my application folder, i.e. Resource.resx for English lanaguage and Resource.de-DE.resx for German langauge. I used the following code in the Model class:

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

So, I have a Resource.resx xml file, and in it I have "FromDateRequiredMessage", which is the same key as in the Resource.de-DE.resx file, but has a different meaning. I also made changes to my web.config file for globalization:

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

So, I want that when choosing German from my application, it should show me a verification error in German. But here he will show me confirmation only in English.

So what can I do for this? Please help me.

+4
source share
3 answers

Please use the Application_PreRequestHandlerExecute() method in your Global.asax application. This method will be executed before any page request or verification.

Also set the current culture value in this method.

+1
source

The simple answer> nothing works!

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

0
source

To do this, you need to establish a culture. Thus, it will provide the user with a choice in the drop-down list, etc. that they explicitly choose German, English, etc., then this will change YourSite to be YourSite / En or yoursight / Gr, etc. This article also explains and gives code examples. The http://www.codeproject.com/Articles/207602/Creating-a-Bilingual-ASP-NET-MVC3-Application-Part part of “Adding User Interface Support for Switching Languages” has what you need, I think.

0
source

Source: https://habr.com/ru/post/1500486/


All Articles