-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultilanguageBase.php
149 lines (136 loc) · 4.66 KB
/
MultilanguageBase.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
<?php
namespace abcms\multilanguage;
use Yii;
use yii\base\Component;
use yii\web\Cookie;
/**
* MultiLanguage Component Class
*/
abstract class MultilanguageBase extends Component
{
public function init()
{
parent::init();
Yii::$app->on(\yii\web\Controller::EVENT_BEFORE_ACTION, [$this, 'setApplicationLanguage']);
}
/**
* Set the language and save it in cookie if needed.
* First read it from url if available, second from cookies and last from application language.
*/
public function setApplicationLanguage()
{
$request = Yii::$app->getRequest();
$language = $request->get('lang');
// If language is available in the url
if ($language) {
// If language code exists in the languages array
if (key_exists($language, $this->getLanguages())) {
$cookie = new Cookie([
'name' => 'lang',
'value' => $language,
'expire' => time() + 86400 * 180, // 180 days
]);
$cookies = Yii::$app->getResponse()->getCookies();
$cookies->add($cookie);
} else {
$language = Yii::$app->language;
}
} else {
$cookies = $request->getCookies();
// If lang cookie already available
if ($cookies->has('lang')) {
$language = $cookies->getValue('lang');
} else {
$language = Yii::$app->language;
}
}
Yii::$app->language = $language;
}
/**
* Translate one model to a specific language
* @param \yii\db\ActiveRecord $model
* @param string $lang Language code
* @return \yii\db\ActiveRecord
*/
public function translate($model, $lang = NULL)
{
if (!$lang) {
$lang = Yii::$app->language;
}
if ($lang == Yii::$app->sourceLanguage) {
return $model;
}
$attributes = $this->translation($model, $lang);
$copy = clone $model;
$copy->attributes = $attributes;
return $copy;
}
/**
* Translate multiple models to a specific language
* @param \yii\db\ActiveRecord[] $models
* @param string $lang Language code
* @param boolean $onlyTranslatedModels Whether to include only translated models or all models
* @return \yii\db\ActiveRecord[]
*/
public function translateMultiple($models, $lang = NULL, $onlyTranslatedModels = true)
{
if (!$lang) {
$lang = Yii::$app->language;
}
if ($lang == Yii::$app->sourceLanguage) {
return $models;
}
$translation = $this->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;
}
/**
* Return all languages of the application except for the default language
* @return array
*/
public function getTranslationLanguages()
{
$languages = $this->getLanguages();
$default = Yii::$app->sourceLanguage;
if(isset($languages[$default])){
unset($languages[$default]);
}
return $languages;
}
/**
* Return the languages array where keys are the language code and values represents the name of the language.
* @return array Languages array
*/
abstract public function getLanguages();
/**
* Return the translated attributes of [[model]] parameter as array with the attribute name as key
* Only translated attributes will be returned.
* @param \yii\db\ActiveRecord $model
* @return array
*/
abstract public function translation($model, $lang);
/**
* Return the translated attributes of [[models]] parameter as array with the pk as key
* Only translated attributes will be returned.
* @param array $models
* @param string $lang
* @return array
*/
abstract public function translationMultiple($models, $lang);
/**
* Save translation for one attribute
* @param \yii\db\ActiveRecord $model
* @param array $data
*/
abstract public function saveTranslation($model, $data);
}