-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, defining translations is quite cumbersome, and the translator callback is passed to the constructor of multiple classes, which makes it quite ugly and could make translations inconsistent. This commit completely changes how translations are done in Validation. Instead of using a callback, it uses a specific class, and `Validator` will pass that object through the objects that render the messages.
- Loading branch information
1 parent
3833ad7
commit d356696
Showing
30 changed files
with
554 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,52 @@ | ||
# Message translation | ||
|
||
You're also able to translate your message to another language with Validation. | ||
The only thing one must do is to define the param `translator` as a callable that | ||
will handle the translation overwriting the default factory: | ||
You're able to translate message templates with Validation. | ||
|
||
```php | ||
Factory::setDefaultInstance( | ||
(new Factory())->withTranslator('gettext') | ||
); | ||
use Respect\Validation\Message\Translator\GettextTranslator; | ||
use Respect\Validation\ValidatorDefaults; | ||
|
||
ValidatorDefaults::setTranslator(new GettextTranslator()); | ||
``` | ||
|
||
After that, if you call the methods `getMessage()`, `getMessages()`, or `getFullMessage()` from the `ValidationException`, the messages will be translated. The example above will work if you have `gettext` properly configured. | ||
|
||
For non-static usage, pass the translator directly to the `Validator` constructor: | ||
|
||
```php | ||
use Respect\Validation\Factory; | ||
use Respect\Validation\Message\StandardFormatter; | ||
use Respect\Validation\Message\Translator\GettextTranslator; | ||
use Respect\Validation\Validator; | ||
|
||
$translator = new GettextTranslator(); | ||
|
||
$validator = new Validator(new Factory(), new StandardFormatter(), $translator); | ||
``` | ||
|
||
The example above uses `gettext()` but you can use any other callable value, like | ||
`[$translator, 'trans']` or `you_custom_function()`. | ||
## Supported translators | ||
|
||
After that, if you call `getMessage()`, `getMessages()`, or `getFullMessage()`, | ||
the message will be translated. | ||
- `ArrayTranslator`: Translates messages using an array of messages. | ||
- `GettextTranslator`: Translates messages using the `gettext` extension. | ||
|
||
## Custom translators | ||
|
||
You can implement custom translators by creating a class that implements the `Translator` interface. Here's an example using the `stichoza/google-translate-php` package: | ||
|
||
```php | ||
use Respect\Validation\Message\Translator; | ||
use Stichoza\GoogleTranslate\GoogleTranslate; | ||
|
||
final class MyTranslator implements Translator | ||
{ | ||
public function __construct( | ||
private readonly GoogleTranslate $googleTranslate | ||
) { | ||
} | ||
|
||
public function translate(string $message): string | ||
{ | ||
return $this->googleTranslate->translate($message); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.