somnambulist-tech / validation

A re-write of rakit/validation, a standalone validation library inspired by Laravel Validation
MIT License
44 stars 13 forks source link

Language change doesn't work #22

Closed xJuvi closed 1 year ago

xJuvi commented 1 year ago

Hi there,

i tried to use the german language template, but i can't use it. Everytime i get the error

"No message was found for the language "de" and any of: "inputUserEmail:email", "inputUserEmail", "rule.email""

I tried both methods, set the language on factory or validator like the docu:

$factory = new Factory(); $factory->messages()->default('de');

and

$validation = $factory->validate($inputs, $rules); $validation->setLanguage('de')->validate();

Where is my mistake? Or is there a bug? I'm unsure. But the only way where language files were loaded is on initialisation of the factory. After that i can set other languages but there is no loading of the file?

Kind regards

dave-redfern commented 1 year ago

@xJuvi Hi, it looks like the docs are not clear enough and are missing the need to actually load the language file. It is not autoloaded when you set the language. You have to call Factory::registerLanguageMessages before using validation. The first arg is the 2 character ISO code, and the optional second is the path to the language file in case you want your own fully customised language file.

In the case of the provided German language file, it would be:

$factory = new Factory();
$factory->registerLanguageMessages('de');

$validation = $factory->make($inputs, $rules);

If you wanted your own language file, then provided the second argument:

$factory->registerLanguageMessages('de', '/path/to/file/with/array_of_de_strings.php');

I'll update the docs to add this. Thanks for reporting it.

dave-redfern commented 1 year ago

@xJuvi I've added notes to the README.md (https://github.com/somnambulist-tech/validation#loading-translation-messages). Please let me know if this is sufficient documentation.

dave-redfern commented 1 year ago

@xJuvi Did the documentation update help you? If so, can I close this issue now?