/
Negotiator.php
377 lines (351 loc) · 9.57 KB
/
Negotiator.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
// +----------------------------------------------------------------------+
// | PEAR :: I18Nv2 :: Negotiator |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is available at http://www.php.net/license/3_0.txt |
// | If you did not receive a copy of the PHP license and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 The Authors |
// +----------------------------------------------------------------------+
// | Authors: Naoki Shima <murahachibu@php.net> |
// | Wolfram Kriesing <wk@visionp.de> |
// | Michael Wallner <mike@iworks.at> |
// +----------------------------------------------------------------------+
//
// $Id$
/**
* I18Nv2::Negotiator
*
* @package I18Nv2
* @category Internationalization
*/
/**
* I18Nv2_Negotiator
*
* @author Naoki Shima <murahachibu@php.net>
* @author Wolfram Kriesing <wk@visionp.de>
* @author Michael Wallner <mike@php.net>
* @version $Revision$
* @access public
* @package I18Nv2
*/
class I18Nv2_Negotiator
{
/**
* I18Nv2_Language
*
* @var object
* @access public
*/
var $I18NLang = null;
/**
* I18Nv2_Country
*
* @var object
* @access public
*/
var $I18NCountry = null;
/**
* Save default country code.
*
* @var string
* @access private
*/
var $_defaultCountry;
/**
* Save default language code.
*
* @var string
* @access private
*/
var $_defaultLanguage;
/**
* Save default encoding code.
*
* @var string
* @access private
*/
var $_defaultEncoding;
/**
* HTTP_ACCEPT_CHARSET
*
* @var array
* @access private
*/
var $_acceptEncoding = array();
/**
* HTTP_ACCEPT_LANGUAGE
*
* @var array
* @access private
*/
var $_acceptLanguage = array();
/**
* Language variations
*
* @var array
* @access private
*/
var $_langVariation = array();
/**
* Countries
*
* @var array
* @access private
*/
var $_country = array();
/**
* Constructor
*
* Find language code, country code, encoding code, and dialect or variant
* of Locale setting in HTTP request headers.
*
* @access public
* @param string $defaultLanguage Default Language
* @param string $defaultEncoding Default Encoding
* @param string $defaultCountry Default Country
*/
function I18Nv2_Negotiator($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
{
$this->__construct($defaultLanguage, $defaultEncoding, $defaultCountry);
}
/**
* ZE2 Constructor
* @ignore
*/
function __construct($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
{
$this->_defaultCountry = $defaultCountry;
$this->_defaultLanguage = $defaultLanguage;
$this->_defaultEncoding = $defaultEncoding;
$this->_negotiateLanguage();
$this->_negotiateEncoding();
}
/**
* Negotiate Language
*
* @access private
* @return void
*/
function _negotiateLanguage()
{
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return;
}
foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
// Cut off any q-value that might come after a semi-colon
if ($pos = strpos($lang, ';')) {
$lang = trim(substr($lang, 0, $pos));
}
if (strstr($lang, '-')) {
list($pri, $sub) = explode('-', $lang);
if ($pri == 'i') {
/**
* Language not listed in ISO 639 that are not variants
* of any listed language, which can be registerd with the
* i-prefix, such as i-cherokee
*/
$lang = $sub;
} else {
$lang = $pri;
$this->singleI18NCountry();
if ($this->I18NCountry->isValidCode($sub)) {
$this->_country[$lang][] = strToUpper($sub);
} else {
$this->_langVariation[$lang][] = $sub;
}
}
}
$this->_acceptLanguage[] = $lang;
}
}
/**
* Negotiate Encoding
*
* @access private
* @return void
*/
function _negotiateEncoding()
{
if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
return;
}
foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $encoding) {
if (!empty($encoding)) {
$this->_acceptEncoding[] = preg_replace('/;.*/', '', $encoding);
}
}
}
/**
* Find Country Match
*
* @access public
* @return array
* @param string $lang
* @param array $countries
*/
function getCountryMatch($lang, $countries = null)
{
return $this->_getMatch(
$countries,
@$this->_country[$lang],
$this->_defaultCountry
);
}
/**
* Return variant info for passed parameter.
*
* @access public
* @return string
* @param string $lang
*/
function getVariantInfo($lang)
{
return isset($this->_langVariation[$lang]) ? $this->_langVariation[$lang] : null;
}
/**
* Find Encoding match
*
* @deprecated
* @access public
* @return string
* @param array $encodings
*/
function getCharsetMatch($encodings = null)
{
return $this->_getMatch(
$encodings,
$this->_acceptEncoding,
$this->_defaultEncoding
);
}
/**
* Find Encoding match
*
* @access public
* @return string
* @param array $encodings
*/
function getEncodingMatch($encodings = null)
{
return $this->_getMatch(
$encodings,
$this->_acceptEncoding,
$this->_defaultEncoding
);
}
/**
* Find Language match
*
* @access public
* @return string
* @param array $langs
*/
function getLanguageMatch($langs = null)
{
return $this->_getMatch(
$langs,
$this->_acceptLanguage,
$this->_defaultLanguage
);
}
/**
* Find locale match
*
* @access public
* @return string
* @param array $langs
* @param array $countries
*/
function getLocaleMatch($langs = null, $countries = null)
{
$lang = $this->_getMatch($langs, $this->_acceptLanguage, $this->_defaultLanguage);
$ctry = $this->_getMatch($countries, @$this->_country[$lang], $this->_defaultCountry);
return $lang . ($ctry ? '_' . $ctry : '');
}
/**
* Return first matched value from first and second parameter.
* If there is no match found, then return third parameter.
*
* @access private
* @return string
* @param array $needle
* @param array $haystack
* @param string $default
*/
function _getMatch($needle, $haystack, $default = '')
{
if (!$haystack) {
return $default;
}
if (!$needle) {
return current($haystack);
}
if ($result = current($a = array_intersect($haystack, $needle))) {
return $result;
}
return $default;
}
/**
* Find Country name for country code passed
*
* @access private
* @return void
* @param string $code country code
*/
function getCountryName($code)
{
$this->singleI18NCountry();
return $this->I18NCountry->getName($code);
}
/**
* Find Country name for country code passed
*
* @access private
* @return void
* @param string $code language code
*/
function getLanguageName($code)
{
$this->singleI18NLanguage();
return $this->I18NLang->getName($code);
}
/**
* Create the Language helper object
*
* @access public
* @return object
*/
function &singleI18NLanguage()
{
if (!isset($this->I18NLang)) {
include_once 'I18Nv2/Language.php';
$this->I18NLang = &new I18Nv2_Language(
$this->_defaultLanguage,
$this->_defaultEncoding
);
}
return $this->I18NLang;
}
/**
* Create the Country helper object
*
* @access public
* @return object
*/
function &singleI18NCountry()
{
if (!isset($this->I18NCountry)) {
include_once 'I18Nv2/Country.php';
$this->I18NCountry = &new I18Nv2_Country(
$this->_defaultLanguage,
$this->_defaultEncoding
);
}
return $this->I18NCountry;
}
}
?>