문제

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?

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top