Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translation in Validator Component

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?

like image 978
room13 Avatar asked Apr 17 '13 15:04

room13


People also ask

What is translation validation?

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.


1 Answers

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();
like image 141
room13 Avatar answered Sep 17 '22 05:09

room13