Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Respect/Validation in another language

I want to use the Respect/Validation library in PHP. I know how to use it but currently I'm using it in a project in German language and of course, I also want the error messages in German.

For language translation, there is a section in the documentation but I don't really get it and I did not found any answer yet.

They're talking about a translator that should handle the translation of the messages. As a second parameter they're giving "gettext" but I don't know what this should be and how this should handle the translation.

Can anybody explain me how this works?

like image 331
deflox Avatar asked Oct 19 '22 11:10

deflox


1 Answers

Respect/Validation won't do the translation for you, you should use a different project, library or function to do that. It won't leave you empty handed though, as the documentation states.

First, you should try to understand how translation libraries work (such as gettext()) and then read PHP documentation on Callables. Then it is a matter of choosing a library, creating the translations and calling setParam('translator', 'callable') method on the exception instance.

A quick introduction to your problem:

  • Translations are done based on a source: it can be a file, a database or something else, depending on which library you use.
  • Respect/Validation exception messages use the same pattern: {{name}} is invalid.. Where {{name}} will be replaced by the input given or the name if setName() was called on that rule.
  • You can see all messages you need to translate under the Respect\Validation\Exceptions namespace.
  • Usually, every library provide a single function/method to translate a given string. This is the method/function you want to set on the $exception->setParam() call.

If you ever translate all exception messages, we would love to make them available to everyone else.

PS: You could also make this question on the repository page, more people would help and we could also improve the way translations are handled by the library in the future.

like image 101
Augusto Pascutti Avatar answered Oct 21 '22 02:10

Augusto Pascutti