示例#1
0
 /**
  * {@inheritdoc}
  */
 public function translate($locale = null)
 {
     $locale = $locale ?: $this->currentLocale;
     if (null === $locale) {
         throw new \RuntimeException('No locale has been set and current locale is undefined.');
     }
     if ($this->currentTranslation && $locale === $this->currentTranslation->getLocale()) {
         return $this->currentTranslation;
     }
     if (!($translation = $this->translations->get($locale))) {
         if (null === $this->fallbackLocale) {
             throw new \RuntimeException('No fallback locale has been set.');
         }
         if (!($fallbackTranslation = $this->translations->get($this->getFallbackLocale()))) {
             $className = $this->getTranslationClass();
             $translation = new $className();
             $translation->setLocale($locale);
             $this->addTranslation($translation);
         } else {
             $translation = clone $fallbackTranslation;
         }
     }
     $this->currentTranslation = $translation;
     return $translation;
 }
 /**
  * Finds existing or creates new translation for specified property
  *
  * @param string $property object property name
  * @param string $locale   locale name
  *
  * @return Translation
  */
 private function findOrCreateTranslationForProperty($property, $locale)
 {
     foreach ($this->coll as $translation) {
         if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
             return $translation;
         }
     }
     /** @var TranslationInterface $translation */
     $translation = new $this->class();
     $translation->setTranslatable($this->translatable);
     $translation->setProperty($property);
     $translation->setLocale($locale);
     $this->coll->add($translation);
     return $translation;
 }
示例#3
0
 /**
  * @param TranslationInterface $translation
  */
 public function removeTranslation(TranslationInterface $translation)
 {
     if ($this->translations->removeElement($translation)) {
         $translation->setTranslatable(null);
     }
 }