public function translate($locale = null, $create = false)
    {
        $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 ($create) {
                $className = $this->getTranslationClass();

                /** @var TranslationInterface $translation */
                $translation = new $className();
                $translation->setLocale($locale);

                $this->addTranslation($translation);
            } else {
                if (null === $this->fallbackLocale) {
                    throw new \RuntimeException('No fallback locale has been set.');
                }

                if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) {
                    $className = $this->getTranslationClass();

                    /** @var TranslationInterface $translation */
                    $translation = new $className();
                    $translation->setLocale($locale);

                    $this->addTranslation($translation);
                } else {
                    $translation = clone $fallbackTranslation;
                }
            }
        }

        $this->currentTranslation = $translation;
        $this->currentLocale      = $locale;

        return $translation;
    }
public function translate($locale = null, $create = false)
    {
        $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 ($create) {
                $className = $this->getTranslationClass();

                /** @var TranslationInterface $translation */
                $translation = new $className();
                $translation->setLocale($locale);

                $this->addTranslation($translation);
            } else {
                if (null === $this->fallbackLocale) {
                    throw new \RuntimeException('No fallback locale has been set.');
                }

                if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) {
                    $className = $this->getTranslationClass();

                    /** @var TranslationInterface $translation */
                    $translation = new $className();
                    $translation->setLocale($locale);

                    $this->addTranslation($translation);
                } else {
                    $translation = clone $fallbackTranslation;
                }
            }
        }

        $this->currentTranslation = $translation;
        $this->currentLocale      = $locale;

        return $translation;
    }
public function translate ( $ locale = null , $ create = false ) { $ 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 ( $ create ) { $ className = $ this -> getTranslationClass ( ) ; /** @var TranslationInterface $translation */ $ translation = new $ className ( ) ; $ translation -> setLocale ( $ locale ) ; $ this -> addTranslation ( $ translation ) ; } else { if ( null === $ this -> fallbackLocale ) { throw new \ RuntimeException ( 'No fallback locale has been set.' ) ; } if ( ! $ fallbackTranslation = $ this -> translations -> get ( $ this -> getFallbackLocale ( ) ) ) { $ className = $ this -> getTranslationClass ( ) ; /** @var TranslationInterface $translation */ $ translation = new $ className ( ) ; $ translation -> setLocale ( $ locale ) ; $ this -> addTranslation ( $ translation ) ; } else { $ translation = clone $ fallbackTranslation ; } } } $ this -> currentTranslation = $ translation ; $ this -> currentLocale = $ locale ; return $ translation ; }
Returns the translation regarding to the current or fallback locale.

@param string $locale
@param bool   $create
@return TranslationInterface
@throws \RuntimeException
Returns the translation regarding to the current or fallback locale .