I'm currently integrating the Symfony Validator Component into a custom PHP Application. So far, everything has been working pretty nice and I can validate my User input.
Now I want to translate the validation messages to another locale and have integrated the Translation Component (it's required anyway due to a depnedency with TranslatorInterface in the DefaultTranslator).
The default Translator only supports the locale that is hard coded into the ValidationConstraints. As far as I've figured it out, I need to specify a custom Translator instance that loads the strings from the xliff files in the Validator component.
This is how far I got but the german translation would sadly not load:
$translator = new Translator('de_DE');
$translator->setFallbackLocale('en_GB');
$translator->addLoader('xliff', new XliffFileLoader());
$builder = new ValidatorBuilder();
$validator = $builder
->setTranslator($translator)
->getValidator();
$violations = $validator->validateValue($input, self::getValidationConstraints());
Any suggestions what I might be missing out here?
Translation Validation is a technique for ensuring that the target code produced by a translator is a correct translation of the source code. Rather than verifying the translator itself, translation validation validates the correctness of each translation, generating a formal proof that it is indeed a correct.
Found out myself. Of course the translation files need to be loaded... Also the Symfony config component needs to be added due to a dependencie to the FileLoader class.
$translator = new Translator('de');
$translator->addLoader('xliff', new XliffFileLoader());
$translator->addResource('xliff', '<path-to-compontnt>/Resources/translations/validators.de.xlf', 'de','validation');
$builder = new ValidatorBuilder();
$validator = $builder
->setTranslator($translator)
->setTranslationDomain('validation')
->getValidator();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With