forked from s-lyn/yii2-multilanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LangHelper.php
69 lines (61 loc) · 1.98 KB
/
LangHelper.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
<?php
namespace pjhl\multilanguage;
use Yii;
class LangHelper {
/**
* Returns a list of the configurations for all languages
* @return array
*/
public static function languages() {
if (!isset(Yii::$app->params['languages'])) {
Yii::error('You must specify the list of languages in params["languages"]', 'yii2-multilanguage');
return [];
}
$languages = Yii::$app->params['languages'];
if (!is_array($languages)) {
Yii::error('Yii::$app->params["languages"] must be an array', 'yii2-multilanguage');
return [];
}
return $languages;
}
/**
* Returns the language setting on the value of its parameter
* @param string $name Name
* @param mixed $value Value
* @return array|null Config array or NULL
*/
public static function getLanguageByParam($name, $value) {
foreach (self::languages() as $lang) {
if (isset($lang[ $name ]) && $lang[ $name ] == $value) {
return $lang;
}
}
return null;
}
/**
* Returns the current language settings
* @param string $key Key for return value
* @return mixed Config array or NULL
*/
public static function getLanguage($key = null) {
$lang = self::getLanguageByParam('locale', Yii::$app->language);
if ($lang !== null && $key!==null && isset($lang[$key])) {
return $lang[$key];
}
return $lang;
}
/**
* Returns regex pattern for language urls.
* Ex.: "(en)|(ru)"
* @return string Quoted regexp
*/
public static function pattern() {
$list = [];
foreach (self::languages() as $lang) {
if ($lang['url']) {
$list[] = '('.preg_quote($lang['url'], '/').')';
}
}
return implode('|',$list);
}
}