Overview
The Validator library supports internationalization (i18n) through theMessages interface. You can switch between built-in languages (English and Spanish) or create custom message implementations for any language.
Built-in Languages
The library includes two built-in message implementations:- MessagesEn - English messages (default)
- MessagesEs - Spanish messages
Changing the Default Language
Use the staticValidator.setMessages() method to change the language globally:
Switch to Spanish
Switch to English
The
setMessages() method affects all validators in your application. Call it early in your application’s initialization, typically in the main() method or application startup.Message Translations Reference
Here are some commonly used validation messages in both languages:| Rule | English (MessagesEn) | Spanish (MessagesEs) |
|---|---|---|
required | Required | Requerido |
email | Email invalid | Correo electrónico inválido |
minLength | %d or more characters are required | Se requiere %d o más caracteres |
maxLength | %d or less characters required | Se requiere %d o menos caracteres |
compare | Not match | No coinciden |
number | It is not a number | No es un número |
onlyNumbers | Only numbers | Solo números |
onlyLetters | Only letters | Solo letras |
name | Invalid personal name | Nombre personal inválido |
View all message translations
View all message translations
| Rule | English | Spanish |
|---|---|---|
compare | Not match | No coinciden |
date | The date does not match the format %s | La fecha no coincide con el formato %s |
email | Email invalid | Correo electrónico inválido |
expirationDate | Expired date | Fecha expirada |
httpLink | Invalid http link | Enlace http inválido |
httpsLink | Invalid https link | Enlace https inválido |
ip | Invalid IP | IP inválida |
ipv4 | Invalid IPv4 | IPv4 inválida |
ipv6 | Invalid IPv6 | IPv6 inválida |
length | It requires %d characters | Se requiere %d caracteres |
link | Invalid link | Enlace inválido |
maxLength | %d or less characters required | Se requiere %d o menos caracteres |
maxValue | The value cannot be greater than %1$.2f | El valor no puede ser mayor a %1$.2f |
minAge | You must be at least %d years old | Se debe tener al menos %d años |
minLength | %d or more characters are required | Se requiere %d o más caracteres |
minValue | The value cannot be less than %1$.2f | El valor no puede ser menor a %1$.2f |
mustContainMin | At least %d of the following characters are required: %s | Se requiere al menos %d de los siguientes caracteres: %s |
mustContainOne | At least one of the following characters is required: %s | Se requiere al menos uno de los siguientes caracteres: %s |
name | Invalid personal name | Nombre personal inválido |
notContain | The following characters aren’t admitted %s | No se admiten los siguientes caracteres %s |
number | It is not a number | No es un número |
numberPattern | Does not match pattern %s | No coincide con el patrón %s |
onlyAlphanumeric | Just alphanumeric characters | Solo caracteres alfanuméricos |
onlyLetters | Only letters | Solo letras |
onlyNumbers | Only numbers | Solo números |
rangeLength | The text must contain between %d to %d characters | El texto debe contener entre %d a %d caracteres |
rangeValue | The value must be between %1.2f | El valor debe estar entre %1.2f |
regExp | The value does not match the regular expression %s | El valor no coincide con la expresión regular %s |
required | Required | Requerido |
shouldOnlyContain | They are just admitted the following characters %s | Solo se admiten los siguientes caracteres %s |
time | Time invalid | Hora inválida |
time12 | Invalid 12 hour format | Formato 12 horas inválido |
time24 | Invalid 24 hour format | Formato 24 horas inválido |
wwwLink | Invalid www link | Enlace www inválido |
Creating Custom Messages
You can create custom message implementations for any language or to customize specific messages.Implementing the Messages Interface
Messages Interface Reference
TheMessages interface defines methods for all validation rules. Each method returns a String message:
Per-Validator Custom Messages
You can override default messages for individual validators without changing the global language:- More specific error messages for certain fields
- To mix languages in the same application
- User-friendly messages that differ from the default
Custom Messages with Annotations
Annotations also support custom messages:Runtime Language Switching
You can change languages at runtime based on user preferences:Extending Built-in Messages
You can extend existing message classes to customize only specific messages:Best Practices
Set language early
Call
Validator.setMessages() during application initialization before creating any validators.Use custom messages for clarity
Override default messages when you need more specific, user-friendly error text.
Extend, don't replace
Extend existing message classes to customize only what you need.
Test in all languages
Ensure your validation logic works correctly with all supported languages.
Next Steps
Error Handling
Learn how to handle validation errors effectively
Form Validation
Apply localization to complete form validation