-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLanguageHelper.php
executable file
·124 lines (111 loc) · 3.57 KB
/
FLanguageHelper.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
<?php
/**
* Helper class for easily working with system languages
*/
class FLanguageHelper
{
/**
* Returns language codes: ['en','ru',...]
* @return array
*/
public static function getIndices()
{
return array_keys(Yii::app()->languages);
}
/**
* Whether there are more than one language in system
* @return boolean
*/
public static function isMultiLingual()
{
return count(Yii::app()->languages) > 1;
}
/**
* Checks whether given code exists among system languages
* @param string $code of language
* @return boolean
*/
public static function exist($code)
{
return array_key_exists($code, Yii::app()->languages);
}
/**
* Check whether current language is different than default language
* @return boolean
*/
public static function isDifferent()
{
return (Yii::app()->language != Yii::app()->defaultLanguage) ? true : false;
}
/**
* Converts abstract field into default language field: name -> name_en
* @param string $field abstract field name
* @return string
*/
public static function getDefField($field)
{
return $field . '_' . Yii::app()->defaultLanguage;
}
/**
* Converts abstract field into current language field: name -> name_en
* @param string $field abstract field name
* @return string
*/
public static function getCurField($field)
{
return $field . '_' . Yii::app()->language;
}
/**
* Converts abstract field into given language field: name -> name_en
* @param string $field abstract field name
* @param string $language code
* @return string
*/
public static function getField($field, $language)
{
return $field . '_' . $language;
}
/**
* Initializes multilingual feature of the system. Called once in per request
* by controller.
*/
public static function init()
{
if(!FLanguageHelper::isMultiLingual())
{
Yii::app()->language = Yii::app()->defaultLanguage;
return;
}
$cookLang = Yii::app()->sitecookie->get('lang');
// if user visits default home page, but he chose other language before
// we redirect him to his prefered language
if(
empty($_GET) && $cookLang &&
!isset($_SERVER['HTTP_REFERER']) &&
($cookLang != Yii::app()->defaultLanguage)
)
{
Yii::app()->request->redirect(Yii::app()->createUrl('', ['lang' => $cookLang]));
}
// If user chose language by select option
elseif(isset($_POST['lang']) && FLanguageHelper::exist($_POST['lang']))
{
// we do nothing if he rechose his current language
if($cookLang && ($cookLang == $_POST['lang']))
Yii::app()->language = $_POST['lang'];
else
// we redirect him to chosen language url
Yii::app()->request->redirect($_POST[$_POST['lang']]);
}
// if language code is already provided by GET
elseif(isset($_GET['lang']) && FLanguageHelper::exist($_GET['lang']))
{
Yii::app()->language = $_GET['lang'];
}
else
// we neither POST, nor GET and we are not in homepage
Yii::app()->language = Yii::app()->defaultLanguage;
if(Yii::app()->language != $cookLang)
Yii::app()->sitecookie->set('lang', Yii::app()->language);
}
}