1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
trait Locale |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Returns the translation string of the given key |
18
|
|
|
* |
19
|
|
|
* @param array $placeholders |
20
|
|
|
* @param string $translateKey |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function _(string $translateKey, array $placeholders = []): string |
24
|
|
|
{ |
25
|
|
|
$translate = $this->getDI()->get('translate'); |
|
|
|
|
26
|
|
|
assert($translate instanceof \Phalcon\Translate\Adapter\AbstractAdapter); |
27
|
|
|
|
28
|
|
|
return $translate->_($translateKey, $placeholders); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Magic caller to set or get localed named field automagically using the current locale |
33
|
|
|
* - Allow to call $this->getName{Fr|En|Sp|...} |
34
|
|
|
* - Allow to call $this->setName{Fr|En|Sp|...} |
35
|
|
|
* |
36
|
|
|
* @param string $method method name |
37
|
|
|
* @param array $arguments method arguments |
38
|
|
|
* @return mixed |
39
|
|
|
* @throws \Phalcon\Mvc\Model\Exception |
40
|
|
|
*/ |
41
|
|
|
public function __call(string $method, array $arguments) |
42
|
|
|
{ |
43
|
|
|
$locale = $this->getDI()->get('locale'); |
44
|
|
|
assert($locale instanceof \Zemit\Locale); |
45
|
|
|
|
46
|
|
|
$lang = $locale->getLocale() ?: 'en'; |
47
|
|
|
|
48
|
|
|
if (mb_strrpos($method, ucfirst($lang)) !== mb_strlen($method) - mb_strlen($lang)) { |
49
|
|
|
$call = $method . ucfirst($lang); |
50
|
|
|
if (method_exists($this, $call)) { |
51
|
|
|
return $this->$call(...$arguments); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return parent::__call($method, $arguments); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Magic setter to set localed named field automatically using the current locale |
60
|
|
|
* - Allow to set $this->name{Fr|En|Sp|...} from missing name property |
61
|
|
|
* |
62
|
|
|
* @param string $property property name |
63
|
|
|
* @param mixed $value value to set |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function __set(string $property, $value) |
67
|
|
|
{ |
68
|
|
|
$locale = $this->getDI()->get('locale'); |
69
|
|
|
assert($locale instanceof \Zemit\Locale); |
70
|
|
|
|
71
|
|
|
$lang = $locale->getLocale(); |
72
|
|
|
|
73
|
|
|
if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) { |
|
|
|
|
74
|
|
|
$set = $property . ucfirst($lang); |
75
|
|
|
|
76
|
|
|
if (property_exists($this, $set)) { |
77
|
|
|
$this->writeAttribute($set, $value); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
parent::__set($property, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Magic getter to get localed named field automatically using the current locale |
88
|
|
|
* - Allow to get $this->name{Fr|En|Sp|...} from missing name property |
89
|
|
|
* |
90
|
|
|
* @param string $property property name |
91
|
|
|
* @return mixed|null |
92
|
|
|
*/ |
93
|
|
|
public function __get(string $property) |
94
|
|
|
{ |
95
|
|
|
$locale = $this->getDI()->get('locale'); |
96
|
|
|
assert($locale instanceof \Zemit\Locale); |
97
|
|
|
|
98
|
|
|
$lang = $locale->getLocale(); |
99
|
|
|
|
100
|
|
|
if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) { |
|
|
|
|
101
|
|
|
$set = $property . ucfirst($lang); |
102
|
|
|
|
103
|
|
|
if (property_exists($this, $set)) { |
104
|
|
|
return $this->readAttribute($set); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return parent::__get($property); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|