Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding TranslateTrait to facilitate the management of multiple transl…
…ations
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
80
tests/TestCase/Model/Behavior/Translate/TranslateTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
|
||
} |