Skip to content

Commit

Permalink
Merge pull request #27 from ADmad/translation
Browse files Browse the repository at this point in the history
Allow translating list values.
  • Loading branch information
steinkel committed Nov 15, 2016
2 parents ac4bf4a + 553044a commit 8eb21c6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Model/Behavior/EnumBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class EnumBehavior extends Behavior
* Default configuration.
*
* - `defaultStrategy`: the default strategy to use.
* - `translate`: Whether values of lists returned by enum() method should
* be translated. Defaults to `false`.
* - `translationDomain`: Domain to use when translating list value.
* Defaults to "default".
* - `implementedMethods`: custom table methods made accessible by this behavior.
* - `lists`: the defined enumeration lists. Lists can use different strategies,
* use prefixes to differentiate them (defaults to the uppercased list name) and
Expand All @@ -51,6 +55,8 @@ class EnumBehavior extends Behavior
*/
protected $_defaultConfig = [
'defaultStrategy' => 'lookup',
'translate' => false,
'translationDomain' => 'default',
'implementedMethods' => [
'enum' => 'enum',
],
Expand Down Expand Up @@ -169,7 +175,12 @@ public function enum($alias = null)
throw new MissingEnumConfigurationException([$alias]);
}

return $this->strategy($alias, $config['strategy'])->enum($config);
$return = $this->strategy($alias, $config['strategy'])->enum($config);
if ($this->config('translate')) {
$return = $this->_translate($return);
}

return $return;
}

$lists = $this->config('lists');
Expand All @@ -180,11 +191,29 @@ public function enum($alias = null)
$return = [];
foreach ($lists as $alias => $config) {
$return[$alias] = $this->strategy($alias, $config['strategy'])->enum($config);
if ($this->config('translate')) {
$return[$alias] = $this->_translate($return[$alias]);
}
}

return $return;
}

/**
* Translate list values.
*
* @param array $list List.
* @return array
*/
protected function _translate(array $list)
{
$domain = $this->config('translationDomain');

return array_map(function ($value) use ($domain) {
return __d($domain, $value);

This comment has been minimized.

Copy link
@inoas

inoas Nov 15, 2016

Contributor

Would it be a good idea if the translate function could be injected via the behavior setup thus that you can use a custom one?

}, $list);
}

/**
* @param \Cake\Event\Event $event Event.
* @param \Cake\ORM\RulesChecker $rules Rules checker.
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Model/Behavior/EnumBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function provideBasicConfiguration()
{
$expected = [
'defaultStrategy' => 'lookup',
'translate' => false,
'translationDomain' => 'default',
'implementedMethods' => ['enum' => 'enum'],
'lists' => [
'priority' => [
Expand Down Expand Up @@ -323,6 +325,22 @@ public function testEnumMultipleAlias()
$this->assertEquals($expected, $result);
}

public function testTranslatedValues()
{
$this->Articles->behaviors()->Enum->config('translate', true);
$result = $this->Articles->enum('node_group');

$this->assertEquals(['active' => 'Translated Active'], $result);

$result = $this->Articles->enum(['node_group', 'norules']);

$expected = [
'node_group' => ['active' => 'Translated Active'],
'norules' => ['FOO' => 'translated foo']
];
$this->assertEquals($expected, $result);
}

public function provideThirdPartyStrategy()
{
return [
Expand Down
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

use Aura\Intl\Package;
use Cake\I18n\I18n;

$findRoot = function ($root) {
do {
$lastRoot = $root;
Expand All @@ -32,3 +35,14 @@
require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';

\Cake\Core\Plugin::load('CakeDC/Enum', ['path' => dirname(dirname(__FILE__)) . DS]);

I18n::config('default', function ($name, $locale) {
$package = new Package('default');
$messages = [
'Active' => 'Translated Active',
'Foo' => 'translated foo'
];
$package->setMessages($messages);

return $package;
});

0 comments on commit 8eb21c6

Please sign in to comment.