-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
22 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
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,106 @@ | ||
<?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; | ||
|
||
use Cake\Collection\Collection; | ||
use Cake\Event\Event; | ||
use Cake\ORM\Behavior; | ||
use Cake\ORM\Entity; | ||
use Cake\ORM\Table; | ||
|
||
class TranslateBehavior extends Behavior { | ||
|
||
/** | ||
* Table instance | ||
* | ||
* @var \Cake\ORM\Table | ||
*/ | ||
protected $_table; | ||
|
||
protected $_locale; | ||
|
||
/** | ||
* Default config | ||
* | ||
* These are merged with user-provided config when the behavior is used. | ||
* | ||
* @var array | ||
*/ | ||
protected static $_defaultConfig = [ | ||
'implementedFinders' => [], | ||
'implementedMethods' => ['locale' => 'locale'] | ||
]; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Table $table The table this behavior is attached to. | ||
* @param array $config The config for this behavior. | ||
*/ | ||
public function __construct(Table $table, array $config = []) { | ||
parent::__construct($table, $config); | ||
$this->_table = $table; | ||
|
||
$table->hasMany('I18n', [ | ||
'strategy' => 'subquery', | ||
'foreignKey' => 'foreign_key', | ||
'conditions' => ['I18n.model' => $table->alias()], | ||
'propertyName' => '_i18n' | ||
]); | ||
} | ||
|
||
public function beforeFind(Event $event, $query) { | ||
$locale = (array)$this->locale(); | ||
$query->contain(['I18n' => function($q) use ($locale) { | ||
if ($locale) { | ||
$q->where(['I18n.locale IN' => $locale]); | ||
} | ||
|
||
return $q; | ||
}]); | ||
|
||
if (count($locale) === 1) { | ||
$locale = current($locale); | ||
$query->formatResults(function($results) use ($locale) { | ||
return $this->_rowMapper($results, $locale); | ||
}); | ||
} | ||
} | ||
|
||
public function locale($locale = null) { | ||
if ($locale === null) { | ||
return $this->_locale; | ||
} | ||
return $this->_locale = $locale; | ||
} | ||
|
||
protected function _rowMapper($results, $locale) { | ||
return $results->map(function($row) use ($locale) { | ||
$translations = (array)$row->get('_i18n'); | ||
$options = ['setter' => false, 'guard' => false]; | ||
|
||
foreach ($translations as $field) { | ||
$row->set([ | ||
$field->get('field') => $field->get('content'), | ||
'_locale' => $locale | ||
], $options); | ||
} | ||
$row->clean(); | ||
|
||
return $row; | ||
}); | ||
} | ||
|
||
} |
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
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
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,57 @@ | ||
<?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\Event\Event; | ||
use Cake\Model\Behavior\TranslateBehavior; | ||
use Cake\ORM\Entity; | ||
use Cake\ORM\TableRegistry; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* Translate behavior test case | ||
*/ | ||
class TranslateBehaviorTest extends TestCase { | ||
|
||
/** | ||
* fixtures | ||
* | ||
* @var array | ||
*/ | ||
public $fixtures = [ | ||
'core.translate', | ||
'core.article' | ||
]; | ||
|
||
/** | ||
* Tests that fields from a translated model are overriden | ||
* | ||
* @return void | ||
*/ | ||
public function testFindSingleLocale() { | ||
$table = TableRegistry::get('Articles'); | ||
$table->addBehavior('Translate'); | ||
$table->locale('eng'); | ||
$results = $table->find()->combine('title', 'body', 'id')->toArray(); | ||
$expected = [ | ||
1 => ['Title #1' => 'Content #1'], | ||
2 => ['Title #2' => 'Content #2'], | ||
3 => ['Title #3' => 'Content #3'], | ||
]; | ||
$this->assertSame($expected, $results); | ||
} | ||
|
||
} | ||
|