-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilanguage.php
More file actions
55 lines (49 loc) · 1.55 KB
/
Copy pathMultilanguage.php
File metadata and controls
55 lines (49 loc) · 1.55 KB
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
<?php
namespace abcms\multilanguage;
use Yii;
use yii\base\BaseObject;
use abcms\multilanguage\models\Translation;
/**
* MultiLanguage Main Class
*/
class Multilanguage extends BaseObject implements MultilanguageInterface
{
/**
* @inheritdoc
*/
public static function getLanguagesList()
{
return isset(Yii::$app->params['languages']) ? Yii::$app->params['languages'] : [];
}
/**
* Return translated models
* Clone the [[models]] objects and replace the translated attributes
* @param array $models
* @param string $lang The language that models should be translated to
* @param boolean $onlyTranslatedModels Whether to include only translated models or all models
* @return array
*/
public static function translateMultiple($models, $lang = NULL, $onlyTranslatedModels = true)
{
if(!$lang) {
$lang = Yii::$app->language;
}
if($lang == Yii::$app->sourceLanguage) {
return $models;
}
$translation = Translation::translationMultiple($models, $lang);
$array = [];
foreach($models as $key => $model) {
if(isset($translation[$model->id])) {
$copy = clone $model;
$copy->attributes = $translation[$model->id];
$array[] = $copy;
}
elseif(!$onlyTranslatedModels) {
$copy = clone $model;
$array[] = $copy;
}
}
return $array;
}
}