protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array())
 {
     $translator = new IdentityTranslator();
     $translator->setLocale('en');
     $contextFactory = new LegacyExecutionContextFactory($metadataFactory, $translator);
     $validatorFactory = new ConstraintValidatorFactory();
     return new LegacyValidator($contextFactory, $metadataFactory, $validatorFactory, $objectInitializers);
 }
 public function testGetLocaleReturnsDefaultLocaleIfNotSet()
 {
     $translator = new IdentityTranslator(new MessageSelector());
     \Locale::setDefault('en');
     $this->assertEquals('en', $translator->getLocale());
     \Locale::setDefault('pt_BR');
     $this->assertEquals('pt_BR', $translator->getLocale());
 }
 public function provideMockData()
 {
     $violation = new ConstraintViolation('Damn error!', 'Damn error!', [], null, 'property', 'blah');
     $violation2 = new ConstraintViolation('Another error!', 'Another error!', [], null, 'property', 'foobar');
     $translator = new IdentityTranslator();
     $translator->setLocale('de');
     return ['Only default language, with sort' => [new ConstraintViolationList([$violation]), $translator, true, false, ['property' => ['Damn error!']]], 'Only default language, no sort' => [new ConstraintViolationList([$violation]), $translator, false, false, ['Damn error!']], 'All languages, no sort' => [new ConstraintViolationList([$violation]), $translator, false, true, ['de' => ['Damn error!'], 'en' => ['Damn error!']], ['de', 'en']], 'All languages, with sort' => [new ConstraintViolationList([$violation, $violation2]), $translator, true, true, ['property' => ['de' => ['Damn error!', 'Another error!'], 'en' => ['Damn error!', 'Another error!']]], ['de', 'en']]];
 }
 public function testGetLocaleReturnsDefaultLocaleIfNotSet()
 {
     // in order to test with "pt_BR"
     IntlTestHelper::requireFullIntl($this);
     $translator = new IdentityTranslator();
     \Locale::setDefault('en');
     $this->assertEquals('en', $translator->getLocale());
     \Locale::setDefault('pt_BR');
     $this->assertEquals('pt_BR', $translator->getLocale());
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function getValidator()
 {
     $metadataFactory = $this->metadataFactory;
     if (!$metadataFactory) {
         $loaders = array();
         if (count($this->xmlMappings) > 1) {
             $loaders[] = new XmlFilesLoader($this->xmlMappings);
         } elseif (1 === count($this->xmlMappings)) {
             $loaders[] = new XmlFileLoader($this->xmlMappings[0]);
         }
         if (count($this->yamlMappings) > 1) {
             $loaders[] = new YamlFilesLoader($this->yamlMappings);
         } elseif (1 === count($this->yamlMappings)) {
             $loaders[] = new YamlFileLoader($this->yamlMappings[0]);
         }
         foreach ($this->methodMappings as $methodName) {
             $loaders[] = new StaticMethodLoader($methodName);
         }
         if ($this->annotationReader) {
             $loaders[] = new AnnotationLoader($this->annotationReader);
         }
         $loader = null;
         if (count($loaders) > 1) {
             $loader = new LoaderChain($loaders);
         } elseif (1 === count($loaders)) {
             $loader = $loaders[0];
         }
         $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
     }
     $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
     $translator = $this->translator;
     if (null === $translator) {
         $translator = new IdentityTranslator();
         // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale
         // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
         // validation messages are pluralized properly even when the default locale gets changed because they are in
         // English.
         $translator->setLocale('en');
     }
     $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
     return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
 }
 public function testGetSetLocale()
 {
     $translator = new IdentityTranslator(new MessageSelector());
     $translator->setLocale('en');
     $translator->getLocale();
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function trans($id, array $parameters = [], $domain = null, $locale = null)
 {
     $possibleActions = ['lbl', 'err', 'msg'];
     if (self::get() === FrontendLanguage::class) {
         $possibleActions[] = 'act';
     }
     if (!preg_match('/(' . implode('|', $possibleActions) . ')./', $id)) {
         return parent::trans($id, $parameters, $domain, $locale);
     }
     if (!strpos($id, '.')) {
         return parent::trans($id, $parameters, $domain, $locale);
     }
     list($action, $string) = explode('.', $id, 2);
     if (!in_array($action, $possibleActions)) {
         return parent::trans($id, $parameters, $domain, $locale);
     }
     $translatedString = self::$action($string);
     // we couldn't translate it, let the parent have a go
     if (preg_match('/\\{\\$' . $action . '.*' . $string . '\\}/', $translatedString)) {
         $parentTranslatedString = parent::trans($id, $parameters, $domain, $locale);
         // If the parent couldn't translate return our default
         if ($id === $parentTranslatedString) {
             return $translatedString;
         }
         return $parentTranslatedString;
     }
     return $translatedString;
 }