|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\View\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Core\Configure; |
|
5
|
|
|
use Cake\I18n\I18n; |
|
6
|
|
|
use Cake\ORM\Entity; |
|
7
|
|
|
use Cake\Utility\Inflector; |
|
8
|
|
|
use Cake\View\Helper; |
|
9
|
|
|
use Cake\View\View; |
|
10
|
|
|
|
|
11
|
|
|
class I18nHelper extends Helper |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Helpers used. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
public $helpers = ['Form']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Default config. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_defaultConfig = [ |
|
27
|
|
|
'locales' => null |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The locales used in the application. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_locales = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Construct method. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Cake\View\View $view The view that was fired. |
|
41
|
|
|
* @param array $config The config passed to the class. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(View $view, $config = []) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($view, $config); |
|
46
|
|
|
|
|
47
|
|
|
$this->_locales = (is_array($this->config('locales'))) ? $this->config('locales') : Configure::read('I18n.locales'); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Generate inputs in all locales. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Cake\ORM\Entity $entity The entity that was fired. |
|
54
|
|
|
* @param string $field The field to process. |
|
55
|
|
|
* @param array $options The options to pass to the input. |
|
56
|
|
|
* @param string $divClass The class to set to the div before the input. |
|
57
|
|
|
* @param string $id The id of the input. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function i18nInput(Entity $entity, $field, $options = [], $divClass = 'col-sm-5', $id = 'CkEditorBox') |
|
62
|
|
|
{ |
|
63
|
|
|
$html = ''; |
|
64
|
|
|
$options['CkEditor'] = (isset($options['CkEditor'])) ? $options['CkEditor'] : false; |
|
65
|
|
|
|
|
66
|
|
|
$i = 0; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->_locales as $locale => $lang) { |
|
69
|
|
|
if ($locale == I18n::defaultLocale()) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$i ++; |
|
74
|
|
|
$options['label'] = Inflector::humanize($lang); |
|
75
|
|
|
$options['value'] = $entity->translation($locale)->{$field}; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if ($options['CkEditor'] === true) { |
|
78
|
|
|
$options['id'] = $id . '-' . $i; |
|
79
|
|
|
unset($options['CkEditor']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$html .= '<div class="form-group">'; |
|
83
|
|
|
$html .= '<div class="col-sm-offset-2 ' . $divClass . '">'; |
|
84
|
|
|
$html .= $this->Form->input('translations.' . $locale . '.' . $field, $options); |
|
|
|
|
|
|
85
|
|
|
$html .= '</div></div>'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $html; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Generate the script tag to initialize CkEditor for textarea. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $options The options passed to the function. |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function i18nScript($options = []) |
|
99
|
|
|
{ |
|
100
|
|
|
$html = ''; |
|
101
|
|
|
$file = (isset($options['file'])) ? $options['file'] : 'article'; |
|
102
|
|
|
$name = (isset($options['name'])) ? $options['name'] : 'CkEditorBox'; |
|
103
|
|
|
$i = 0; |
|
104
|
|
|
|
|
105
|
|
|
foreach ($this->_locales as $locale => $lang) { |
|
106
|
|
|
if ($locale == I18n::defaultLocale()) { |
|
107
|
|
|
continue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$i ++; |
|
111
|
|
|
$html .= ' |
|
112
|
|
|
<script type="text/javascript"> |
|
113
|
|
|
CKEDITOR.replace(\'' . $name . '-' . $i . '\', { |
|
114
|
|
|
customConfig: \'config/' . $file . '.js\' |
|
115
|
|
|
}); |
|
116
|
|
|
</script>'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $html; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..