Internationalization
Valicomb includes built-in support for 33 languages, making it easy to display validation error messages in your users’ preferred language.
Available Languages
Section titled “Available Languages”| Code | Language |
|---|---|
ar |
Arabic |
az |
Azerbaijani |
bg |
Bulgarian |
cs |
Czech |
da |
Danish |
de |
German |
el |
Greek |
en |
English (default) |
es |
Spanish |
fa |
Persian (Farsi) |
fi |
Finnish |
fr |
French |
hu |
Hungarian |
id |
Indonesian |
it |
Italian |
ja |
Japanese |
ko |
Korean |
lt |
Lithuanian |
lv |
Latvian |
nb |
Norwegian Bokmål |
nl |
Dutch |
nn |
Norwegian Nynorsk |
no |
Norwegian |
pl |
Polish |
pt |
Portuguese |
pt-br |
Portuguese (Brazil) |
ro |
Romanian |
ru |
Russian |
sk |
Slovak |
sl |
Slovenian |
sv |
Swedish |
th |
Thai |
tr |
Turkish |
uk |
Ukrainian |
vi |
Vietnamese |
zh-cn |
Chinese (Simplified) |
zh-tw |
Chinese (Traditional) |
Setting the Language
Section titled “Setting the Language”Global Setting
Section titled “Global Setting”Set the language for all Validator instances:
use Frostybee\Valicomb\Validator;
// Set French as defaultValidator::lang('fr');
// All new validators use French$v = new Validator($data);$v->rule('required', 'email');// Error: "Email est requis"Per-Instance Setting
Section titled “Per-Instance Setting”Set language for a specific validator:
// Global default is EnglishValidator::lang('en');
// This instance uses Spanish$v = new Validator($data, [], 'es');$v->rule('required', 'email');// Error: "Email es requerido"Getting Current Language
Section titled “Getting Current Language”$currentLang = Validator::lang(); // Returns 'en' by defaultCustom Language Directory
Section titled “Custom Language Directory”Use your own translation files:
// Set custom language directoryValidator::langDir('/path/to/my/translations');
// Then set languageValidator::lang('custom');Per-Instance Custom Directory
Section titled “Per-Instance Custom Directory”$v = new Validator($data, [], 'en', '/path/to/custom/lang');Language File Format
Section titled “Language File Format”Language files are PHP files returning an associative array:
// lang/en.phpreturn [ '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];Creating Custom Translations
Section titled “Creating Custom Translations”- Copy an existing language file as a template
- Translate each message
- Place in your custom language directory
- Use
langDir()to point to your directory
// my-translations/pt-BR.phpreturn [ 'required' => '{field} é obrigatório', 'email' => '{field} não é um endereço de e-mail válido', 'min' => '{field} deve ser pelo menos {0}', // ...];Dynamic Language Selection
Section titled “Dynamic Language Selection”Based on User Preference
Section titled “Based on User Preference”// Get user's preferred language$userLang = $_SESSION['language'] ?? 'en';
// Validate with user's language$v = new Validator($data, [], $userLang);Based on HTTP Header
Section titled “Based on HTTP Header”// 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);Message Placeholders
Section titled “Message Placeholders”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.
Combining with Field Labels
Section titled “Combining with Field Labels”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"Contributing Translations
Section titled “Contributing Translations”To contribute a new language or improve existing translations:
- Fork the repository
- Copy
lang/en.phptolang/{code}.php - Translate all messages
- 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