1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/LAV45/yii2-translated-behavior |
4
|
|
|
* @copyright Copyright (c) 2015 LAV45! |
5
|
|
|
* @author Alexey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\translate; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Behavior; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseTranslatedBehavior |
16
|
|
|
* @package lav45\translate |
17
|
|
|
* |
18
|
|
|
* @property array $translateAttributes |
19
|
|
|
* @property string $language |
20
|
|
|
* @property string $sourceLanguage |
21
|
|
|
*/ |
22
|
|
|
class BaseTranslatedBehavior extends Behavior |
23
|
|
|
{ |
24
|
|
|
use LocaleHelperTrait; |
25
|
|
|
/** |
26
|
|
|
* @var string the current translate language. If not set, it will use the value of |
27
|
|
|
* [[\yii\base\Application::language]]. |
28
|
|
|
*/ |
29
|
|
|
private $_language; |
30
|
|
|
/** |
31
|
|
|
* @var string the language that the original messages are in. If not set, it will use the value of |
32
|
|
|
* [[\yii\base\Application::sourceLanguage]]. |
33
|
|
|
*/ |
34
|
|
|
private $_sourceLanguage; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $_attributes = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
11 |
|
public function getLanguage() |
44
|
|
|
{ |
45
|
11 |
|
if ($this->_language === null) { |
46
|
10 |
|
$this->setLanguage(Yii::$app->language); |
47
|
10 |
|
} |
48
|
11 |
|
return $this->_language; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $locale |
53
|
|
|
*/ |
54
|
11 |
|
public function setLanguage($locale) |
55
|
|
|
{ |
56
|
11 |
|
$this->_language = $this->getPrimaryLanguage($locale); |
57
|
11 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
10 |
|
public function getSourceLanguage() |
63
|
|
|
{ |
64
|
10 |
|
if ($this->_sourceLanguage === null) { |
65
|
10 |
|
$this->setSourceLanguage(Yii::$app->sourceLanguage); |
66
|
10 |
|
} |
67
|
10 |
|
return $this->_sourceLanguage; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $locale |
72
|
|
|
*/ |
73
|
10 |
|
public function setSourceLanguage($locale) |
74
|
|
|
{ |
75
|
10 |
|
$this->_sourceLanguage = $this->getPrimaryLanguage($locale); |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
1 |
|
public function getTranslateAttributes() |
82
|
|
|
{ |
83
|
1 |
|
return $this->_attributes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $attributes the list of translateAttributes to be translated |
88
|
|
|
*/ |
89
|
15 |
|
public function setTranslateAttributes($attributes) |
90
|
|
|
{ |
91
|
15 |
|
$this->_attributes = []; |
92
|
15 |
|
foreach ((array) $attributes as $key => $value) { |
93
|
15 |
|
$key = is_int($key) ? $value : $key; |
94
|
15 |
|
$this->_attributes[$key] = $value; |
95
|
15 |
|
} |
96
|
15 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
13 |
|
protected function isAttribute($name) |
103
|
|
|
{ |
104
|
13 |
|
return isset($this->_attributes[$name]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
9 |
|
public function getTranslateAttributeName($name) |
112
|
|
|
{ |
113
|
9 |
|
return $this->isAttribute($name) ? $this->_attributes[$name] : null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
2 |
|
public function isSourceLanguage() |
120
|
|
|
{ |
121
|
2 |
|
return $this->getLanguage() === $this->getSourceLanguage(); |
122
|
|
|
} |
123
|
|
|
} |