Skip to content

Commit

Permalink
Add internal API Method - getTranslation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Hristov committed Aug 4, 2017
1 parent 26be26f commit 2081c83
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/CurrentTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class CurrentTranslationLoader
*/
private $propertyAccessor;

/**
* @var bool
*/
private $fallback = true;
/**
* @var array
*/
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -76,7 +71,7 @@ private function initializeFallbackTranslation(TranslatableInterface $entity)

foreach ($fallbackLocales as $fallbackLocale) {
if ($this->initializeTranslation($entity, $fallbackLocale)) {
break;
return $fallbackLocale;
}
}
}
Expand All @@ -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(
Expand All @@ -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);
}
}

0 comments on commit 2081c83

Please sign in to comment.