Skip to content

Commit

Permalink
Added support for adding translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Wolf committed Aug 26, 2015
1 parent 4d1e163 commit b746c7b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Detail\Persistence\Model\Behavior\Translatable;

use Knp\DoctrineBehaviors\Model as ORMBehaviors;

trait KnpTranslatableTrait
{
/**
* @param string $locale
* @return TranslationInterface
*/
protected function createAndAddTranslation($locale)
{
$class = self::getTranslationEntityClass();
/** @var ORMBehaviors\Translatable\Translation $translation */
$translation = new $class();
$translation->setLocale($locale);

$this->addTranslation($translation);

return $translation;
}

/**
* @param ORMBehaviors\Translatable\Translation $translation
* @return $this
*/
abstract public function addTranslation($translation);

/**
* @return string
*/
abstract public function getTranslationEntityClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract public function getTranslations();
*/
public function getTranslation($locale)
{
return $this->getTranslations()->get($locale);
return $this->getTranslations()->get((string) $locale);
}

/**
Expand All @@ -26,7 +26,7 @@ public function getTranslation($locale)
*/
public function hasTranslation($locale)
{
return $this->getTranslation($locale) !== null;
return $this->getTranslation((string) $locale) !== null;
}

/**
Expand Down Expand Up @@ -54,11 +54,6 @@ public function __call($method, array $arguments)
// Note that we're removing this first argument from the array for later...
$locale = array_shift($arguments);

if (!is_string($locale)) {
/** @todo Add support for current/default locale */
$triggerError();
}

// Let's see if we have a translation for given locale
if (!$this->hasTranslation($locale)) {
$triggerError();
Expand All @@ -76,7 +71,27 @@ public function __call($method, array $arguments)
}

/**
* @param $method
* @param string $method
* @param TranslationText[] $texts
*/
protected function setTranslationTexts($method, array $texts)
{
foreach ($texts as $text) {
if (!$this->hasTranslation($text->getLocale())) {
// Create the translation
$this->createAndAddTranslation($text->getLocale());
}

// Update the translation using the magic setter
call_user_func_array(
array($this, $method),
array((string) $text->getLocale(), $text->getValue())
);
}
}

/**
* @param string $method
* @return TranslationText[]
*/
protected function getTranslationTexts($method)
Expand All @@ -87,7 +102,7 @@ protected function getTranslationTexts($method)
$text = call_user_func(array($translation, $method));

if ($text !== null) {
$texts[] = $this->createTranslationText($locale, $text);
$texts[] = $this->createTranslationText($translation->getLocale(), $text);
}
}

Expand Down Expand Up @@ -131,4 +146,10 @@ protected function createTranslationText($locale, $value)
{
return new TranslationText($locale, $value);
}

/**
* @param string $locale
* @return TranslationInterface
*/
abstract protected function createAndAddTranslation($locale);
}

0 comments on commit b746c7b

Please sign in to comment.