ketchup-plugin icon indicating copy to clipboard operation
ketchup-plugin copied to clipboard

Localization feature

Open winebloom opened this issue 13 years ago • 1 comments

The validation messages may depend on the client language. A way to do this is to replace the current contents of the "jquery.ketchup.validations.js" file with the below (working) code. Would you add this into the next builds?

// Retrieve value from var pageLanguage = document.documentElement.lang

if ( pageLanguage=='fr' ) {

jQuery.ketchup .validation('required', 'Ce champ est requis.', function(form, el, value) { var type = el.attr('type').toLowerCase(); if(type == 'checkbox' || type == 'radio') { return (el.attr('checked') == true); } else { return (value.length != 0); } }) .validation('minlength', 'Ce champ doit avoir une longueur minimale de {arg1}.', function(form, el, value, min) { return (value.length >= +min); }) .validation('maxlength', 'Ce champ doit avoir une longueur maximale de {arg1}.', function(form, el, value, max) { return (value.length <= +max); }) .validation('rangelength', 'Ce champ doit avoir une longueur entre {arg1} et {arg2}.', function(form, el, value, min, max) { return (value.length >= min && value.length <= max); }) .validation('min', 'Valeur minimale de {arg1}.', function(form, el, value, min) { return (this.isNumber(value) && +value >= +min); }) .validation('max', 'Valeur maximale de {arg1}.', function(form, el, value, max) { return (this.isNumber(value) && +value <= +max); }) .validation('range', 'Valeur entre {arg1} et {arg2}.', function(form, el, value, min, max) { return (this.isNumber(value) && +value >= +min && +value <= +max); }) .validation('number', 'Doit être un nombre.', function(form, el, value) { return this.isNumber(value); }) .validation('digits', 'Doivent être des chiffres.', function(form, el, value) { return /^\d+$/.test(value); }) .validation('email', 'Doit avoir un format d'adresse mél.', function(form, el, value) { return this.isEmail(value); }) .validation('url', 'Doit être une adresse web.', function(form, el, value) { return this.isUrl(value); }) .validation('username', 'Doit être un nom utilisateur en usage.', function(form, el, value) { return this.isUsername(value); }) .validation('match', 'Doit être {arg1}.', function(form, el, value, word) { return (el.val() == word); }) .validation('contain', 'Doit contenir {arg1}', function(form, el, value, word) { return this.contains(value, word); }) .validation('date', 'Doit avoir un format de date.', function(form, el, value) { return this.isDate(value); }) .validation('minselect', 'Cochez au moins {arg1} cases.', function(form, el, value, min) { return (min <= this.inputsWithName(form, el).filter(':checked').length); }, function(form, el) { this.bindBrothers(form, el); }) .validation('maxselect', 'Ne cochez pas plus que {arg1} cases.', function(form, el, value, max) { return (max >= this.inputsWithName(form, el).filter(':checked').length); }, function(form, el) { this.bindBrothers(form, el); }) .validation('rangeselect', 'Cochez entre {arg1} et {arg2} cases.', function(form, el, value, min, max) { var checked = this.inputsWithName(form, el).filter(':checked').length; return (min <= checked && max >= checked); }, function(form, el) { this.bindBrothers(form, el); });

// If pageLanguage is unidentified or if HTML lang is English } else {

jQuery.ketchup

.validation('required', 'This field is required.', function(form, el, value) { var type = el.attr('type').toLowerCase(); if(type == 'checkbox' || type == 'radio') { return (el.attr('checked') == true); } else { return (value.length != 0); } }) .validation('minlength', 'This field must have a minimal length of {arg1}.', function(form, el, value, min) { return (value.length >= +min); }) .validation('maxlength', 'This field must have a maximal length of {arg1}.', function(form, el, value, max) { return (value.length <= +max); }) .validation('rangelength', 'This field must have a length between {arg1} and {arg2}.', function(form, el, value, min, max) { return (value.length >= min && value.length <= max); }) .validation('min', 'Must be at least {arg1}.', function(form, el, value, min) { return (this.isNumber(value) && +value >= +min); }) .validation('max', 'Can not be greater than {arg1}.', function(form, el, value, max) { return (this.isNumber(value) && +value <= +max); }) .validation('range', 'Must be between {arg1} and {arg2}.', function(form, el, value, min, max) { return (this.isNumber(value) && +value >= +min && +value <= +max); }) .validation('number', 'Must be a number.', function(form, el, value) { return this.isNumber(value); }) .validation('digits', 'Must be digits.', function(form, el, value) { return /^\d+$/.test(value); }) .validation('email', 'Must have the format of an e-mail.', function(form, el, value) { return this.isEmail(value); }) .validation('url', 'Must be a web address.', function(form, el, value) { return this.isUrl(value); }) .validation('username', 'Must be a used username.', function(form, el, value) { return this.isUsername(value); }) .validation('match', 'Must be {arg1}.', function(form, el, value, word) { return (el.val() == word); }) .validation('contain', 'Must contain {arg1}', function(form, el, value, word) { return this.contains(value, word); }) .validation('date', 'Must have the format of a date.', function(form, el, value) { return this.isDate(value); }) .validation('minselect', 'Select at least {arg1} checkboxes.', function(form, el, value, min) { return (min <= this.inputsWithName(form, el).filter(':checked').length); }, function(form, el) { this.bindBrothers(form, el); }) .validation('maxselect', 'Select not more than {arg1} checkboxes.', function(form, el, value, max) { return (max >= this.inputsWithName(form, el).filter(':checked').length); }, function(form, el) { this.bindBrothers(form, el); }) .validation('rangeselect', 'Select between {arg1} and {arg2} checkboxes.', function(form, el, value, min, max) { var checked = this.inputsWithName(form, el).filter(':checked').length; return (min <= checked && max >= checked); }, function(form, el) { this.bindBrothers(form, el); });

}

winebloom avatar Mar 17 '11 10:03 winebloom

In hope that it is more readable here is the same code framed with

 tags:

// Retrieve value from 
var pageLanguage = document.documentElement.lang

if ( pageLanguage=='fr' ) {

jQuery.ketchup
.validation('required', 'Ce champ est requis.', function(form, el, value) {
  var type = el.attr('type').toLowerCase();
  if(type == 'checkbox' || type == 'radio') {
    return (el.attr('checked') == true);
  } else {
    return (value.length != 0);
  }
})
.validation('minlength', 'Ce champ doit avoir une longueur minimale de {arg1}.', function(form, el, value, min) {
  return (value.length >= +min);
})
.validation('maxlength', 'Ce champ doit avoir une longueur maximale de {arg1}.', function(form, el, value, max) {
  return (value.length = min && value.length = +min);
})
.validation('max', 'Valeur maximale de {arg1}.', function(form, el, value, max) {
  return (this.isNumber(value) && +value = +min && +value = this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
  this.bindBrothers(form, el);
})
.validation('rangeselect', 'Cochez entre {arg1} et {arg2} cases.', function(form, el, value, min, max) {
  var checked = this.inputsWithName(form, el).filter(':checked').length;
  return (min = checked);
}, function(form, el) {
  this.bindBrothers(form, el);
});

// If pageLanguage is unidentified or if HTML lang is English
} else {

jQuery.ketchup

.validation('required', 'This field is required.', function(form, el, value) {
  var type = el.attr('type').toLowerCase();
  if(type == 'checkbox' || type == 'radio') {
    return (el.attr('checked') == true);
  } else {
    return (value.length != 0);
  }
})
.validation('minlength', 'This field must have a minimal length of {arg1}.', function(form, el, value, min) {
  return (value.length >= +min);
})
.validation('maxlength', 'This field must have a maximal length of {arg1}.', function(form, el, value, max) {
  return (value.length = min && value.length = +min);
})
.validation('max', 'Can not be greater than {arg1}.', function(form, el, value, max) {
  return (this.isNumber(value) && +value = +min && +value = this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
  this.bindBrothers(form, el);
})
.validation('rangeselect', 'Select between {arg1} and {arg2} checkboxes.', function(form, el, value, min, max) {
  var checked = this.inputsWithName(form, el).filter(':checked').length;
  return (min = checked);
}, function(form, el) {
  this.bindBrothers(form, el);
});

}

winebloom avatar Mar 17 '11 10:03 winebloom