1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TMultiView and TView class file. |
4
|
|
|
* |
5
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @copyright Copyright © 2005-2016 The PRADO Group |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
* @package Prado\I18N |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prado\I18N; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TGlobalizationAutoDetect class will automatically try to resolve the default |
16
|
|
|
* culture using the user browser language settings. |
17
|
|
|
* |
18
|
|
|
* You can specify the list of available cultures on the website by setting the {@link setAvailableLanguages AvailableLanguages} property, eg: |
19
|
|
|
* <code> |
20
|
|
|
* <module id="globalization" class="TGlobalizationAutoDetect" charset="UTF-8" DefaultCulture="it" TranslateDefaultCulture="false" AvailableLanguages="it, en"> |
21
|
|
|
* </code> |
22
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
23
|
|
|
* @package Prado\I18N |
24
|
|
|
*/ |
25
|
|
|
class TGlobalizationAutoDetect extends TGlobalization |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* A list of languages accepted by the browser. |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $languages; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A list of charsets accepted by the browser |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $charsets; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* First language accepted by the browser |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $detectedLanguage; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* A list of languages accepted by the website. |
47
|
|
|
* If empty or not defined, any language is accepted. |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $validLanguages; |
51
|
|
|
|
52
|
|
|
public function init($xml) |
53
|
|
|
{ |
54
|
|
|
parent::init($xml); |
55
|
|
|
|
56
|
|
|
//set the culture according to browser language settings |
57
|
|
|
$languages = $this->getLanguages(); |
58
|
|
|
foreach ($languages as $lang) { |
59
|
|
|
$mainLang = $lang; // strstr($lang, '_', true); |
60
|
|
|
if (count($this->validLanguages) > 0 && !in_array($mainLang, $this->validLanguages)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->detectedLanguage = $mainLang; |
65
|
|
|
$this->setCulture($mainLang); |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getDetectedLanguage() |
71
|
|
|
{ |
72
|
|
|
return $this->detectedLanguage; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Checks wether the specified locale is valid and available |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
protected function getIsValidLocale($locale) |
80
|
|
|
{ |
81
|
|
|
static $allLocales; |
82
|
|
|
if($allLocales === null) |
83
|
|
|
{ |
84
|
|
|
$all = \ResourceBundle::getLocales(''); |
85
|
|
|
} |
86
|
|
|
return in_array($locale, $all); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get a list of languages acceptable by the client browser |
91
|
|
|
* @return array languages ordered in the user browser preferences. |
92
|
|
|
*/ |
93
|
|
|
protected function getLanguages() |
94
|
|
|
{ |
95
|
|
|
if ($this->languages !== null) { |
96
|
|
|
return $this->languages; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->languages = []; |
100
|
|
|
|
101
|
|
|
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
102
|
|
|
return $this->languages; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { |
106
|
|
|
// Cut off any q-value that might come after a semi-colon |
107
|
|
|
if ($pos = strpos($lang, ';')) { |
108
|
|
|
$lang = trim(substr($lang, 0, $pos)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (strstr($lang, '-')) { |
112
|
|
|
$codes = explode('-', $lang); |
113
|
|
|
if ($codes[0] == 'i') { |
114
|
|
|
// Language not listed in ISO 639 that are not variants |
115
|
|
|
// of any listed language, which can be registerd with the |
116
|
|
|
// i-prefix, such as i-cherokee |
117
|
|
|
if (count($codes) > 1) { |
118
|
|
|
$lang = $codes[1]; |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
for ($i = 0, $k = count($codes); $i < $k; ++$i) { |
122
|
|
|
if ($i == 0) { |
123
|
|
|
$lang = strtolower($codes[0]); |
124
|
|
|
} else { |
125
|
|
|
$lang .= '_' . strtoupper($codes[$i]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($this->getIsValidLocale($lang)) { |
132
|
|
|
$this->languages[] = $lang; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->languages; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get a list of charsets acceptable by the client browser. |
141
|
|
|
* @return array list of charsets in preferable order. |
142
|
|
|
*/ |
143
|
|
|
protected function getCharsets() |
144
|
|
|
{ |
145
|
|
|
if ($this->charsets !== null) { |
146
|
|
|
return $this->charsets; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->charsets = []; |
150
|
|
|
|
151
|
|
|
if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) { |
152
|
|
|
return $this->charsets; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $charset) { |
156
|
|
|
if (!empty($charset)) { |
157
|
|
|
$this->charsets[] = preg_replace('/;.*/', '', $charset); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->charsets; |
162
|
|
|
} |
163
|
|
|
/** |
164
|
|
|
* Get the available languages. |
165
|
|
|
* @return array of languages |
166
|
|
|
*/ |
167
|
|
|
public function getAvailableLanguages() |
168
|
|
|
{ |
169
|
|
|
return $this->validLangs; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the available languages. |
174
|
|
|
* @param Comma separated string of available languages. |
175
|
|
|
*/ |
176
|
|
|
public function setAvailableLanguages($langs) |
177
|
|
|
{ |
178
|
|
|
$this->validLanguages = array_map('trim', explode(',', $langs)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: