public function validate($value, $minLength = null, $maxLength = null, $allowLinebreaks = false)
 {
     // make sure there are no untranslated parts at the beginning, unless the string is entirely empty
     if ($value !== "") {
         $this->regexValidator->validate($value, "|^\\[:[a-z]{2}\\]|");
     }
     $availableLanguages = array_map(function ($locale) {
         return substr($locale, 0, 2);
     }, $this->localeService->getAvailableLocales());
     $parts = Multilang::multilangStringToArray($value);
     if (!count($parts) && $minLength) {
         throw new InvalidValueException(Translate::t("The field must be translated into at least one language."));
     }
     foreach ($parts as $lang => $string) {
         if (!in_array($lang, $availableLanguages)) {
             throw new InvalidValueException(sprintf(Translate::t("`%s` is not a valid language."), $lang));
         }
         if ($string || $minLength) {
             $this->stringValidator->validate($string, $minLength, $maxLength, $allowLinebreaks);
         }
     }
 }
Пример #2
0
 /**
  * @dataProvider providerTestU
  */
 public function testU($string, $locale, $expected)
 {
     $this->assertSame($expected, Multilang::u($string, $locale));
 }
 public function u($string, $locale = null)
 {
     return Multilang::u($string, $locale);
 }
 protected function getEntityName($entity)
 {
     return is_callable([$entity, "getName"]) ? Multilang::u($entity->getName()) : $entity->getId();
 }