From 2081c83d7d50c338ecce0324b3cd097b17e07660 Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Fri, 4 Aug 2017 12:33:22 +0300 Subject: [PATCH] Add internal API Method - getTranslation --- src/CurrentTranslationLoader.php | 64 +++++++++++++++++++------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/CurrentTranslationLoader.php b/src/CurrentTranslationLoader.php index 86aa518..946e3af 100644 --- a/src/CurrentTranslationLoader.php +++ b/src/CurrentTranslationLoader.php @@ -19,10 +19,6 @@ class CurrentTranslationLoader */ private $propertyAccessor; - /** - * @var bool - */ - private $fallback = true; /** * @var array */ @@ -34,13 +30,6 @@ public function __construct(Container $container) $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); } - /** - * @param $trueFalse - */ - public function doFallback($trueFalse) - { - $this->fallback = $trueFalse; - } /** * Reinitialize all current translations of all managed entities @@ -57,13 +46,19 @@ public function flush() */ public function initializeCurrentTranslation(TranslatableInterface $entity) { + /** @var TranslationService $translationService */ $translationService = $this->container->get('object_bg.translation.service.translation'); - $CurrentLanguage = $translationService->getCurrentLanguage(); - $success = $this->initializeTranslation($entity, $CurrentLanguage); + /** @var Language $currentLanguage */ + $currentLanguage = $translationService->getCurrentLanguage(); + $success = $this->initializeTranslation($entity, $currentLanguage); + + $locale = $currentLanguage->getLocale(); - if ($success == false && $this->fallback === true) { - $this->initializeFallbackTranslation($entity); + if ($success == false) { + $locale = $this->initializeFallbackTranslation($entity); } + + return $locale; } /** @@ -76,7 +71,7 @@ private function initializeFallbackTranslation(TranslatableInterface $entity) foreach ($fallbackLocales as $fallbackLocale) { if ($this->initializeTranslation($entity, $fallbackLocale)) { - break; + return $fallbackLocale; } } } @@ -88,16 +83,32 @@ private function initializeFallbackTranslation(TranslatableInterface $entity) */ public function initializeTranslation(TranslatableInterface $entity, $languageOrLocale) { - $oid = spl_object_hash($entity); - $this->managedEntities[$oid] = $entity; + $this->managedEntities[$this->getId($entity)] = $entity; + /** @var TranslationService $translationService */ $translationService = $this->container->get('object_bg.translation.service.translation'); - $translations = $this->propertyAccessor->getValue($entity, $translationService->getTranslationsField($entity)); + $currentTranslation = $this->getTranslation($entity, $languageOrLocale); + if ($currentTranslation) { + $currentTranslationField = $translationService->getCurrentTranslationField($entity); + $this->propertyAccessor->setValue($entity, $currentTranslationField, $currentTranslation); - if (!$translations) { - return false; + return true; } + + return false; + } + + public function getTranslation(TranslatableInterface $translatable, $languageOrLocale) + { + /** @var TranslationService $translationService */ + $translationService = $this->container->get('object_bg.translation.service.translation'); + + $translations = $this->propertyAccessor->getValue( + $translatable, + $translationService->getTranslationsField($translatable) + ); + $propertyAccessor = $this->propertyAccessor; $currentTranslation = $translations->filter( @@ -113,11 +124,14 @@ function ($item) use ($translationService, $languageOrLocale, $propertyAccessor) )->first(); if (!$currentTranslation) { - return false; + return null; } - $currentTranslationField = $translationService->getCurrentTranslationField($entity); - $this->propertyAccessor->setValue($entity, $currentTranslationField, $currentTranslation); - return true; + return $currentTranslation; + } + + private function getId(TranslatableInterface $translatable) + { + return spl_object_hash($translatable); } }