Globalization and Localization of Model Validation Messages in MVC

In this quick article, you will learn how to globalize and localize the model validation messages in MVC Applications. At the end of this article, you will also learn how to display the localized message (greeting) on the page.

Here is what we will build.

1.jpg

(Click here to view bigger image)

I will use ASP.NET resource files to define the globalized (default) and localized strings and this string message will be displayed to the user based on the request header language that the client browser sends.

Now, follow the steps from here.

Step 1

To do ASP.NET automatically we need to set the globalization settings in web.config, as in the following.

  1. <system.web>  
  2.   ....  
  3.   <globalization culture="auto" uiCulture="auto"/>  
  4.   ....  
  5. </system.web>

 

Step 2

To localize the text we need to rely on the resource files that are nothing but a XML file with .resx extension that contains globalized and localized string messages, name and its value pair.

These resource files can be added anywhere in the application but I will prefer to add it with views.

2.png

Error.resx File

In this file I have defined two string names AddressRequired and NameRequired. This file can be called a default/base resource and will be displayed every time the browser requests a language that we don't support. Remember to change the access modifier to Public, because this file turns into an assembly that requires public access.

Error.hi.resx File

This file will have the localized name and value pair, which is Hindi here. This string message will be displayed to the user based on the request header language that the client browser sends.

Step 3

To display these localized validation messages, we need to make a quick change in the model validation attribute, here we go.

  1. public class DemoFoolProofModel  
  2. {  
  3.     public int Id { getset; }  
  4.     //[Required]  
  5.     [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName="NameRequired")]  
  6.     public string Name { getset; }  
  7.     //[Required]  
  8.     [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName="AddressRequired")]  
  9.     public string Address { getset; }  
  10. } 

I am overriding the validation message displayed by [Required]. ErrorMessageResourceType will have the name of resource file and ErrorMessageResourceName will have the name of the name-value pair. That's it.

Step 4

Now we are all set to run the application, hit F5, at the same time open Fiddler to inspect the request header. I selected English (United States) as a primary language and you can see Fiddler has "en-US" in the request header. In this case the Error.resx file information will be rendered.

3.png

And here is the output in the browser.

4.png

Step 5

Let's select Hindi as a primary language and you will see Fiddler has "hi" in the request header. In this case the Error.hi.resx file information will be rendered.

5.png

Step 6

In the same way if you want to display some greeting on your website then you can do this by adding another two resource files, as in the following.

6.png

Now, to display this localized message on the view page use the following code:

  1. <h2>@MvcApplication1.Views.Demo.Greeting.GreetingMsg</h2>  

 

Now, select Hindi as a primary language, you will see it working.

7.png

Hope this helps.


Similar Articles