Skip to content

Internationalization

Valicomb includes built-in support for 22 languages, making it easy to display validation error messages in your users’ preferred language.

CodeLanguage
arArabic
csCzech
daDanish
deGerman
enEnglish (default)
esSpanish
faPersian (Farsi)
fiFinnish
frFrench
huHungarian
idIndonesian
itItalian
jaJapanese
nlDutch
noNorwegian
plPolish
ptPortuguese
ruRussian
svSwedish
trTurkish
ukUkrainian
zhChinese

Set the language for all Validator instances:

use Frostybee\Valicomb\Validator;
// Set French as default
Validator::lang('fr');
// All new validators use French
$v = new Validator($data);
$v->rule('required', 'email');
// Error: "Email est requis"

Set language for a specific validator:

// Global default is English
Validator::lang('en');
// This instance uses Spanish
$v = new Validator($data, [], 'es');
$v->rule('required', 'email');
// Error: "Email es requerido"
$currentLang = Validator::lang(); // Returns 'en' by default

Use your own translation files:

// Set custom language directory
Validator::langDir('/path/to/my/translations');
// Then set language
Validator::lang('custom');
$v = new Validator($data, [], 'en', '/path/to/custom/lang');

Language files are PHP files returning an associative array:

// lang/en.php
return [
'required' => '{field} is required',
'email' => '{field} is not a valid email address',
'min' => '{field} must be at least {0}',
'max' => '{field} must be at most {0}',
'between' => '{field} must be between {0} and {1}',
// ... more rules
];
  1. Copy an existing language file as a template
  2. Translate each message
  3. Place in your custom language directory
  4. Use langDir() to point to your directory
// my-translations/pt-BR.php
return [
'required' => '{field} é obrigatório',
'email' => '{field} não é um endereço de e-mail válido',
'min' => '{field} deve ser pelo menos {0}',
// ...
];

// Get user's preferred language
$userLang = $_SESSION['language'] ?? 'en';
// Validate with user's language
$v = new Validator($data, [], $userLang);
// Parse Accept-Language header
$acceptLang = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en';
$lang = substr($acceptLang, 0, 2); // Get first 2 chars
// Fallback if not supported
$supported = ['en', 'es', 'fr', 'de', 'pt'];
if (!in_array($lang, $supported)) {
$lang = 'en';
}
$v = new Validator($data, [], $lang);

All languages support the same placeholders ({field}, {0}, {1}, etc.). These work consistently across all translations. For complete placeholder documentation, see Error Messages - Message Placeholders.


Labels work with any language:

Validator::lang('es');
$v = new Validator(['correo' => '']);
$v->labels(['correo' => 'Correo Electrónico']);
$v->rule('required', 'correo');
$v->validate();
// Error: "Correo Electrónico es requerido"

To contribute a new language or improve existing translations:

  1. Fork the repository
  2. Copy lang/en.php to lang/{code}.php
  3. Translate all messages
  4. Submit a pull request

Guidelines:

  • Keep placeholders exactly as they are: {field}, {0}, {1}, etc.
  • Match the tone and style of the English version
  • Test your translations with actual validation