Skip to content

Commit

Permalink
Adding TranslateTrait to facilitate the management of multiple transl…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
lorenzo committed Feb 15, 2014
1 parent 3b4f69d commit d0047ed
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Model/Behavior/Translate/TranslateTrait.php
@@ -0,0 +1,60 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Behavior\Translate;

use Cake\ORM\Entity;

/**
* Contains a translation method aimed to help managing multiple translations
* for an entity.
*/
trait TranslateTrait {

/**
* Returns the entity containing the translated fields for this object and for
* the spedcified language. If the translation for the passed language is not
* present, a new empty entity will be created so that values can be added to
* it.
*
* @return void
*/
public function translation($language) {
if ($language === $this->get('_locale')) {
return $this;
}

$i18n = $this->get('_translations');
$created = false;

if (empty($i18n)) {
$i18n = [];
$created = true;
}

if ($created || empty($i18n[$language]) || !($i18n[$language] instanceof Entity)) {
$i18n[$language] = new Entity();
$created = true;
}

if ($created) {
$this->set('_translations', $i18n);
}

// Assume the user will modify any of the internal translations, helps with saving
$this->dirty('_translations', true);
return $i18n[$language];
}

}
80 changes: 80 additions & 0 deletions tests/TestCase/Model/Behavior/Translate/TranslateTraitTest.php
@@ -0,0 +1,80 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Behavior;

use Cake\Model\Behavior\Translate\TranslateTrait;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;

class TestEntity extends Entity {
use TranslateTrait;
}

/**
* Translate behavior test case
*/
class TranslateBehaviorTest extends TestCase {

/**
* Tests that missing translation entries are created automatically
*
* @return void
*/
public function testTranslationCreate() {
$entity = new TestEntity;
$entity->translation('eng')->set('title', 'My Title');
$this->assertEquals('My Title', $entity->translation('eng')->get('title'));

$this->assertTrue($entity->dirty('_translations'));

$entity->translation('spa')->set('body', 'Contenido');
$this->assertEquals('My Title', $entity->translation('eng')->get('title'));
$this->assertEquals('Contenido', $entity->translation('spa')->get('body'));
}

/**
* Tests that modifying existing translation entries work
*
* @return void
*/
public function testTranslationModify() {
$entity = new TestEntity;
$entity->set('_translations', [
'eng' => new Entity(['title' => 'My Title']),
'spa' => new Entity(['title' => 'Titulo'])
]);
$this->assertEquals('My Title', $entity->translation('eng')->get('title'));
$this->assertEquals('Titulo', $entity->translation('spa')->get('title'));
}

/**
* Tests that just accessing the translation will mark the property as dirty, this
* is to facilitate the saving process by not having to remember to mark the property
* manually
*
* @return void
*/
public function testTranslationDirty() {
$entity = new TestEntity;
$entity->set('_translations', [
'eng' => new Entity(['title' => 'My Title']),
'spa' => new Entity(['title' => 'Titulo'])
]);
$entity->clean();
$this->assertEquals('My Title', $entity->translation('eng')->get('title'));
$this->assertTrue($entity->dirty('_translations'));
}

}

0 comments on commit d0047ed

Please sign in to comment.