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 

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.

<system.web>

  ….

  <globalization culture=auto uiCulture=auto/>

  ….

</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

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.

public class DemoFoolProofModel

{

    public int Id { get; set; }

    //[Required]

    [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName=“NameRequired”)]

    public string Name { get; set; }

    //[Required]

    [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName=“AddressRequired”)]

    public string Address { get; set; }

}

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

And here is the output in the browser.

4

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

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

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

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

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

7

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.