JQuery Validation Internationalization Problem

前端 未结 3 2133
悲&欢浪女
悲&欢浪女 2020-12-28 18:50

How can I change JQuery Validation plugin that gives error message at browser\'s language. I mean if I run it on a browser that has English language errors will be in Englis

相关标签:
3条回答
  • 2020-12-28 19:23

    You could have a look at the jquery's localisation plugin: http://keith-wood.name/localisation.html
    It allows loading javascript files based on the browser's defined language.

    The jquery validation plugin comes with localisation files available at this adress: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/localization/messages_##.js where ## is the loc code.

    What you can do is load the validation plugin with the default error messages and then use the localization plugin to load the language-specific error messages. You can even write your localized js file, syntax is pretty trivial:

    /*
     * Translated default messages for the jQuery validation plugin.
     * Locale: FR
     */
     jQuery.extend(jQuery.validator.messages, {
        required: "Ce champ est requis.",
        remote: "Veuillez remplir ce champ pour continuer.",
        email: "Veuillez entrer une adresse email valide.",
        url: "Veuillez entrer une URL valide.",
        date: "Veuillez entrer une date valide.",
        dateISO: "Veuillez entrer une date valide (ISO).",
        number: "Veuillez entrer un nombre valide.",
        digits: "Veuillez entrer (seulement) une valeur numérique.",
        creditcard: "Veuillez entrer un numéro de carte de crédit valide.",
        equalTo: "Veuillez entrer une nouvelle fois la même valeur.",
        accept: "Veuillez entrer une valeur avec une extension valide.",
        maxlength: jQuery.validator.format("Veuillez ne pas entrer plus de {0} caractères."),
        minlength: jQuery.validator.format("Veuillez entrer au moins {0} caractères."),
        rangelength: jQuery.validator.format("Veuillez entrer entre {0} et {1} caractères."),
        range: jQuery.validator.format("Veuillez entrer une valeur entre {0} et {1}."),
        max: jQuery.validator.format("Veuillez entrer une valeur inférieure ou égale à  {0}."),
        min: jQuery.validator.format("Veuillez entrer une valeur supérieure ou égale à  {0}.")
    });
    



    Hope this helps !

    0 讨论(0)
  • 2020-12-28 19:28

    Here's a suggestion for a simple way to set your custom validation message:

    1. Create a js file with XX_Messages.ee.js name with syntax:

     LANG = {
    
        PERSONAL_BIRTHDAY_CAN_T_BLANK : 'Birthday cannot be left blank',
        PERSONAL_COUNTRY_CAN_T_BLANK : 'Country cannot be left blank',
        PERSONAL_CITY_CAN_T_BLANK : 'City cannot be left blank',
    .......
    TEAM_EMAIL_NOT_VALID : 'E-mail is not a valid email address'
    };
    

    2.

    <head>
    ....
    <g:set var="locale" value="${session['language']}"/>
        <g:if test="${locale.equals("ee")}">
            <script src="${resource(dir: 'js',file:'/XX_Messages.ee.js')}"></script>
        </g:if>
    ....
    </head>
    

    Note: import your XX_Messages.ee.js in html head where you want validation

    3.

    messages: {
                gender: {
                    required: LANG.PERSONAL_BIRTHDAY_CAN_T_BLANK
                },
                firstName: {
                    required:  LANG.PERSONAL_CITY_CAN_T_BLANK
                },
                lastName: {
                    required: LANG.PERSONAL_COUNTRY_CAN_T_BLANK
                }
            }
    

    Note: XX_Messages.ee.js file must be import in head of your html before your validation js.

    0 讨论(0)
  • 2020-12-28 19:31

    Jquery validation plugin works with an array variable. I defined different array variables as like:

    locale_tr, locale_en, locale_sw etc.
    

    I get browser's language as two character as like

    tr en sw
    

    and I call the variable of :

    locale_ + browser's two character language
    

    so it works for me.

    0 讨论(0)
提交回复
热议问题