-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathLanguage.php
217 lines (194 loc) · 5.71 KB
/
Language.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @author Lajos Molnár <lajax.m@gmail.com>
*
* @since 1.0
*/
namespace lajax\translatemanager\models;
use Yii;
/**
* This is the model class for table "language".
*
* @property string $language_id
* @property string $language
* @property string $country
* @property string $name
* @property string $name_ascii
* @property int $status
* @property LanguageTranslate $languageTranslate
* @property LanguageSource[] $languageSources
*/
class Language extends \yii\db\ActiveRecord
{
/**
* Status of inactive language.
*/
const STATUS_INACTIVE = 0;
/**
* Status of active language.
*/
const STATUS_ACTIVE = 1;
/**
* Status of ‘beta’ language.
*/
const STATUS_BETA = 2;
/**
* Array containing possible states.
*
* @var array
* @translate
*/
private static $_CONDITIONS = [
self::STATUS_INACTIVE => 'Inactive',
self::STATUS_ACTIVE => 'Active',
self::STATUS_BETA => 'Beta',
];
/**
* @inheritdoc
*/
public static function getDb()
{
return Yii::$app->get(Yii::$app->getModule('translatemanager')->connection);
}
/**
* @inheritdoc
*/
public static function tableName()
{
return Yii::$app->getModule('translatemanager') ?
Yii::$app->getModule('translatemanager')->languageTable : '{{%language}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['language_id', 'language', 'country', 'name', 'name_ascii', 'status'], 'required'],
[['language_id'], 'string', 'max' => 5],
[['language_id'], 'unique'],
[['language_id'], 'match', 'pattern' => '/^([a-z]{2}[_-][A-Z]{2}|[a-z]{2})$/'],
[['language', 'country'], 'string', 'max' => 2],
[['language', 'country'], 'match', 'pattern' => '/^[a-z]{2}$/i'],
[['name', 'name_ascii'], 'string', 'max' => 32],
[['status'], 'integer'],
[['status'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'language_id' => Yii::t('model', 'Language ID'),
'language' => Yii::t('model', 'Language'),
'country' => Yii::t('model', 'Country'),
'name' => Yii::t('model', 'Name'),
'name_ascii' => Yii::t('model', 'Name Ascii'),
'status' => Yii::t('model', 'Status'),
];
}
/**
* Returns the list of languages stored in the database in an array.
*
* @param bool $active True/False according to the status of the language.
*
* @return array
*
* @deprecated since version 1.5.2
*/
public static function getLanguageNames($active = false)
{
$languageNames = [];
foreach (self::getLanguages($active, true) as $language) {
$languageNames[$language['language_id']] = $language['name'];
}
return $languageNames;
}
/**
* Returns language objects.
*
* @param bool $active True/False according to the status of the language.
* @param bool $asArray Return the languages as language object or as 'flat' array
*
* @return Language|array
*
* @deprecated since version 1.5.2
*/
public static function getLanguages($active = true, $asArray = false)
{
if ($active) {
return self::find()->where(['status' => static::STATUS_ACTIVE])->asArray($asArray)->all();
} else {
return self::find()->asArray($asArray)->all();
}
}
/**
* Returns the state of the language (Active, Inactive or Beta) in the current language.
*
* @return string
*/
public function getStatusName()
{
return Yii::t('array', self::$_CONDITIONS[$this->status]);
}
/**
* Returns the names of possible states in an associative array.
*
* @return array
*/
public static function getStatusNames()
{
return \lajax\translatemanager\helpers\Language::a(self::$_CONDITIONS);
}
/**
* Returns the completness of a given translation (language).
*
* @return int
*/
public function getGridStatistic()
{
static $statistics;
if (!$statistics) {
$count = LanguageSource::find()->count();
if ($count == 0) {
return 0;
}
$languageTranslates = LanguageTranslate::find()
->select(['language', 'COUNT(*) AS cnt'])
->andWhere('translation IS NOT NULL')
->groupBy(['language'])
->all();
foreach ($languageTranslates as $languageTranslate) {
$statistics[$languageTranslate->language] = floor(($languageTranslate->cnt / $count) * 100);
}
}
return isset($statistics[$this->language_id]) ? $statistics[$this->language_id] : 0;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLanguageTranslate()
{
return $this->hasOne(LanguageTranslate::className(), ['language' => 'language_id']);
}
/**
* @return \yii\db\ActiveQuery
*
* @deprecated since version 1.4.5
*/
public function getIds()
{
return $this->hasMany(LanguageSource::className(), ['id' => 'id'])
->viaTable(LanguageTranslate::tableName(), ['language' => 'language_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLanguageSources()
{
return $this->hasMany(LanguageSource::className(), ['id' => 'id'])
->viaTable(LanguageTranslate::tableName(), ['language' => 'language_id']);
}
}