Translate an entity. All stored translation data will be applied to this object. It should be called only inside the
doctrine postLoad hook, because it doesn't handle recursive connections.

@param $entity
@param $locale
@return null
@throws \Exception
970379
public function translate($entity, $locale)
    {
        $metadata = $this->metadataCollection->getMetadata($entity);
        if($metadata === null) {
            return null;
        }

        //translation data is stored inside the object
        if($locale === $this->defaultLocale) {
            return;
        }

        $accessor = PropertyAccess::createPropertyAccessor();   
        if($metadata !== null) {
            foreach($metadata->getProperties() as $property) {
                $strategy = $this->strategyResolver->getStrategy($property->getStrategy());
                $value = $strategy->getTranslation($entity, $property, $locale, $metadata);
                $accessor->setValue($entity, $property->getName(), $value);
            }
        }
    }
403422
protected function getTranslation(string $key, string $locale)
    {
        return $this->translations()
            ->where('key', $key)
            ->where('locale', $locale)
            ->value('translation');
    }
1232036
public function translate(string $locale = null, bool $fallback = true): ?Model
    {
        $locale = $locale ?: $this->getLocale();

        $translation = $this->getTranslation($locale);

        if (!$translation && $fallback) {
            $translation = $this->getTranslation($this->getFallback());
        }

        if (!$translation && !$fallback) {
            $translation = $this->getEmptyTranslation($locale);
        }

        return $translation;
    }
683336
protected function initLocale($locale)
    {
        $file = $this->localeDir . '/' . $locale . '.php';
        if (file_exists($file)) {
            $data = include $file;

            if (!is_array($data)) {
                $data = [];
            }
        } else {
            $data = [];
        }

        if (!isset($data['pluralForm']) || !is_callable($data['pluralForm'])) {
            $data['pluralForm'] = null;
        }
        if (!isset($data['texts'])) {
            $data['texts'] = [];
        }

        $this->data[$locale] = $data;
    }
513160
public function translate($key, $translation, $locale = null) {
        $this->translations[$this->locale($locale)][$key] = $translation;

        return $translation;
    }
1281820
private function translate($key, $locale = null)
    {
        if (is_null($locale))
            $locale = $this->translator->getLocale();

        $translation = $this->translator->trans($key, [], $locale);

        // @codeCoverageIgnoreStart
        if ( ! is_string($translation))
            throw new InvalidTranslationException(
                "The translation key [{$key}] for locale [{$locale}] should return a string value."
            );
        // @codeCoverageIgnoreEnd

        return (string) $translation;
    }
729693
public function hasTranslation($locale = null)
    {
        $locale = $locale ?: \localizer\locale()->id();

        foreach ($this->cachedTranslations() as $translation) {
            if ((int) $translation->getAttribute($this->getLocaleKey()) === (int) $locale) {
                return true;
            }
        }

        return false;
    }
1006623
protected function getTranslationOrNew($locale)
    {
        if (($translation = $this->getTranslation($locale)) === null) {
            $translation = $this->createNewTranslation($locale);
        }

        return $translation;
    }
597828
public function translateOrFirst($locale = null)
    {
        $translation = $this->translate($locale, true);

        if ( ! $translation)
        {
            $translation = $this->translations->first();
        }

        return $translation;
    }
463509
public function setLocale($locale = null)
    {
        if (class_exists('\Locale')) {
            $this->locale = \Locale::lookup(
                $this->getAvailableLocales(),
                $locale,
                true,
                $this->getSettings()->locale->default
            );
        }

        if (empty($this->locale)) {
            $this->locale = $this->getSettings()->locale->default;
        }

        // Set up locale environment for gettext.
        bindtextdomain(self::TEXT_DOMAIN_NAME, self::TEXT_DOMAIN_LOCATION);
        bind_textdomain_codeset(self::TEXT_DOMAIN_NAME, 'UTF-8');
        textdomain(self::TEXT_DOMAIN_NAME);
        putenv('LANG=' . $this->locale);
        putenv('LC_MESSAGES=' . $this->locale);
        $locale = setlocale(LC_MESSAGES, $this->locale);

        Logger::get()->info(sprintf('Locale is now %s [%s] (domain "%s" at %s)',
            $locale,
            $this->locale,
            self::TEXT_DOMAIN_NAME,
            realpath(self::TEXT_DOMAIN_LOCATION)
        ));
    }
396330
public function getLocalization($locale)
	{
		$localization = parent::getLocalization($locale);
		
		// Create fake localization if not persisted
		if (is_null($localization)) {
			$localization = $this->createLocalization($locale);
		}
		
		return $localization;
	}
237285
protected function getCellContent($entity)
    {
        $content = $this->getValue($entity);
        if ($content && isset($this->options['translate_content']) && $this->options['translate_content']) {
            $translator = isset($this->options['translator']) ? $this->options['translator'] : null;
            if ($translator) {
                $content = isset($this->options['translation_content_prefix'])
                    ? $this->options['translation_content_prefix'].$content
                    : $content;

                $translationOptions = isset($this->options['translation_options'])
                    ? $this->options['translation_options']
                    : array();

                $translationDomain = isset($this->options['translation_domain'])
                    ? $this->options['translation_domain']
                    : null;

                $content = $translator->trans($content, $translationOptions, $translationDomain);
            }
        }
        return $content;
    }
205167
public function getTranslations(Locale $locale)
    {
        $localeID = $locale->getID();
        $translations = isset($this->translations[$localeID]) ? $this->translations[$localeID][1] : null;
        if ($translations === null) {
            $translations = clone $this->getSourceStrings(true);
            $translations->setLanguage($locale->getID());
            $this->setTranslations($locale, $translations);
        }

        return $translations;
    }
397690
public function findAllByLocale($locale)
    {
        try {
            $qb = $this->getUnitQuery($locale)
                ->orderBy('unitTranslations.name', 'ASC');

            return $qb->getQuery()->getResult();
        } catch (NoResultException $exc) {
            return null;
        }
    }
1278168
public function setLocale($locale)
	{
		if(is_string($locale))
			$locale=CLocale::getInstance($locale);
		$this->sizeFormat['decimalSeparator']=$locale->getNumberSymbol('decimal');
		$this->_locale=$locale;
	}
670360
public function toJSONI18N(Entity $entity, array $locales=[], $depth=0) {
		if(!$locales)
			$locales = $entity->getLocales();
		return json_encode($entity->toArrayI18N($locales, $depth));
	}
788624
public function getTranslation(
        $entityType,
        $entityId,
        $entityField,
        $locale
    ) {
        $translation = $this
            ->entityTranslationRepository
            ->findOneBy([
                'entityType' => $entityType,
                'entityId' => $entityId,
                'entityField' => $entityField,
                'locale' => $locale,
            ]);

        return $translation instanceof EntityTranslationInterface
            ? $translation->getTranslation()
            : '';
    }