Skip to content
Permalink
Browse files

Adding TranslateTrait to facilitate the management of multiple transl…

…ations
  • Loading branch information...
lorenzo committed Feb 15, 2014
1 parent 3b4f69d commit d0047ed9bf61fd27a87e01651f57e2b2339f23ac
@@ -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];
}
}
@@ -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.
You can’t perform that action at this time.