Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validation Internationalization Problem

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 English or if the browser's language is Turkish error messages will be Turkish too(of course I will define messages at that languages, also I should define a default language if I didn't define an error message list according to browser's language)

like image 725
kamaci Avatar asked Aug 17 '11 10:08

kamaci


2 Answers

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 !

like image 51
Didier Ghys Avatar answered Sep 20 '22 14:09

Didier Ghys


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.

like image 37
kamaci Avatar answered Sep 19 '22 14:09

kamaci