-
Notifications
You must be signed in to change notification settings - Fork 1
Localization
The package ships with English (en, the default) and Turkish (tr),
and lets you override individual messages or load your own language files.
setLocale() loads a language file and merges it over the active messages.
use InitPHP\Validation\Validation;
$v = new Validation(['age' => 'abc']);
$v->setLocale('tr');
$v->rule('age', 'integer');
$v->validation();
$v->getError(); // ["age bir tam sayı olmalıdır."]The loaded locale persists across validation() runs, so call setLocale()
once during setup.
setLocaleArray() merges a partial set of templates over the active locale —
handy for tweaking a few messages without a whole file.
$v->setLocaleArray([
'integer' => '{field} must be a whole number.',
'required' => 'Please fill in {field}.',
]);Message keys are matched case-insensitively against rule names, so a key of
creditCard or creditcard both apply to the creditCard rule. Placeholders
work exactly as in Error Messages.
setLocaleDir() points the loader at a directory of <locale>.php files; then
setLocale() loads from there.
$v->setLocaleDir(__DIR__ . '/lang');
$v->setLocale('de'); // loads __DIR__/lang/de.phpA language file returns an associative array of templates:
<?php
// lang/de.php
return [
'required' => '{field} darf nicht leer sein.',
'integer' => '{field} muss eine Ganzzahl sein.',
'mail' => '{field} muss eine E-Mail-Adresse sein.',
// ... one entry per rule you want to translate
];Any rule without a matching key falls back to the notValidDefault template
("The {field} value is not valid.").
Keys are rule names. The full set ships in
src/languages/en.php:
notValidDefault, callable, integer, float, numeric, string, boolean, array,
mail, mailHost, url, urlHost, empty, required, min, max, length, range, regex,
date, dateFormat, ip, ipv4, ipv6, again, equals, startWith, endWith, in, notIn,
alpha, alphaNum, alphanumeric, creditCard, only, strictOnly, contains, notContains
The special callable key is used when a callback rule
fails without its own message; notValidDefault is the catch-all fallback.
setLocale() and setLocaleArray() merge over what is already active.
Apply the base language first, then your overrides:
$v->setLocale('tr'); // base language
$v->setLocaleArray(['mail' => '{field} hatalı.']); // then tweak- Error Messages — placeholders, labels and custom messages.
- API Reference — the locale methods in full.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other