Using MVC 3 with non-English Locales


If you normally work with a locale other than English, you need to include the correct locale-specific jQuery scripts, which you can download from the following URL:

http://plugins.jquery.com/node/8/release

For example, for German locales, you need to download the following file:

jquery.validate_17\jquery-validate\localization\methods_de.js

You then need to include the correct script reference in your .cshtml file,s as in the following example:

<script src="@Url.Content("~/Scripts/methods_de.js")" type="text/javascript"></script>

You don’t need to include the localized messages script (messages_de.js for German locales), because ASP.NET MVC and the DataAnnotations types use their own localized messages.

In addition to including the localized methods script, the current locale on the server must be set to the target locale so that any server-side messages (used for both client-side and server-side validation) will be used correctly.

If you use a non-English locale, you should also use the correct locale-specific character for the decimal delimiter in the price. (In German, for example, you would enter a price as "9,99".) On some non-English keyboards, the dot character (.) does not produce the English dot character used for decimal places; in those cases, if you use a dot character for the decimal point, you might see the following error:

The field Price must be a number.

Some of the locale specific methods scripts are not complete. schrijf_rob posted the following in the comments section of part 6 of my Intro to MVC 3 tutorial.

I live in the Netherlands and had the same problem inputing ‘9,99’. Originally I used methods_nl.js. But that did not work. So I compared it with methods_de.js and found that the number part was missing in methods_nl. I copied that part from methods_de and it all worked. We do have the same numbering system as in Germany so for other countries youe might need to tweek it a little. My methods_nl.js now looks like this:

jQuery.extend(jQuery.validator.methods, {
    date: function (value, element) {
        return this.optional(element) || /^\d\d?[\.\/-]\d\d?[\.\/-]\d\d\d?\d?$/.test(value);
    },
    number: function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});

The following MVC 3 tutorials use jQuery validation:

To set your locale:

  1. In Control Panel, open Regional and Language Options.
  2. Select the Location tab.
  3. In the drop down box, select your target locale.

5706.Region[1]


Comments (6)

  1. Radu Enuca says:

    This is a very useful information.

    Maybe add some hints for localized applications too?

  2. Hi Richard,

    The localization tool of the jquery validation toolkit is good for solving the translation of messages,

    but it doesn't solve well the globalization problem that is the problem of date and number formats for the following reason:

    1) it only provides number and date format for a few languages

    2) Also if it recognize well forrmatted dates and numbers it is not able to parse them into actual numbers and dates that can be used for defining validation attributes like the range attribute. I am using the globalization plugin here: github.com/…/jquery-global

    This way I have parsing and format functions that works with all languages and I  can use them to define validation attributes.

    Once I download it I redefine number and date correct format by using the localizez parsing functions, and then I have redefined the validation attributes such as the range attribute to use the localized parsing function instead of the culture neutral functions

  3. David says:

    Hi Ricka,

    Very useful article for "translation of messages with localization tools" is really helpful.The jquery validation toolkit for localization tool is seems to be very useful for justifying this concept. Anyways good blog post for all dotnet learners. Keep it up and post more. Your guidance is very helpful.

    gloriatech.com/microsoft-net-development-services.aspx

  4. Guilherme says:

    Very useful, but.. it's the same thing written here:

    msdn.microsoft.com/…/gg674880%28v=VS.98%29.aspx

  5. jman says:

    I can't get it to work for an reason unknown to me: Unable to get value of the property 'methods' is the error I get when trying to run the application, as if it doesn't recognize the already available jquery methods

  6. MrZer0 says:

    At the moment plugins.jquery.com/…/release is in development. Is there another place where plugins can be downloaded?