2 // +----------------------------------------------------------------------+
3 // | PEAR :: I18Nv2 :: Negotiator |
4 // +----------------------------------------------------------------------+
5 // | This source file is subject to version 3.0 of the PHP license, |
6 // | that is available at http://www.php.net/license/3_0.txt |
7 // | If you did not receive a copy of the PHP license and are unable |
8 // | to obtain it through the world-wide-web, please send a note to |
9 // | license@php.net so we can mail you a copy immediately. |
10 // +----------------------------------------------------------------------+
11 // | Copyright (c) 2004 The Authors |
12 // +----------------------------------------------------------------------+
13 // | Authors: Naoki Shima <murahachibu@php.net> |
14 // | Wolfram Kriesing <wk@visionp.de> |
15 // | Michael Wallner <mike@iworks.at> |
16 // +----------------------------------------------------------------------+
18 // $Id: Negotiator.php,v 1.11 2005/11/28 15:33:22 mike Exp $
24 * @category Internationalization
30 * @author Naoki Shima <murahachibu@php.net>
31 * @author Wolfram Kriesing <wk@visionp.de>
32 * @author Michael Wallner <mike@php.net>
33 * @version $Revision: 1.11 $
37 class I18Nv2_Negotiator
53 var $I18NCountry = null;
56 * Save default country code.
64 * Save default language code.
69 var $_defaultLanguage;
72 * Save default encoding code.
77 var $_defaultEncoding;
85 var $_acceptEncoding = array();
88 * HTTP_ACCEPT_LANGUAGE
93 var $_acceptLanguage = array();
101 var $_langVariation = array();
109 var $_country = array();
114 * Find language code, country code, encoding code, and dialect or variant
115 * of Locale setting in HTTP request headers.
118 * @param string $defaultLanguage Default Language
119 * @param string $defaultEncoding Default Encoding
120 * @param string $defaultCountry Default Country
122 function I18Nv2_Negotiator($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
124 $this->__construct($defaultLanguage, $defaultEncoding, $defaultCountry);
131 function __construct($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
133 $this->_defaultCountry = $defaultCountry;
134 $this->_defaultLanguage = $defaultLanguage;
135 $this->_defaultEncoding = $defaultEncoding;
137 $this->_negotiateLanguage();
138 $this->_negotiateEncoding();
147 function _negotiateLanguage()
149 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
152 foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
153 // Cut off any q-value that might come after a semi-colon
154 if ($pos = strpos($lang, ';')) {
155 $lang = trim(substr($lang, 0, $pos));
157 if (strstr($lang, '-')) {
158 list($pri, $sub) = explode('-', $lang);
161 * Language not listed in ISO 639 that are not variants
162 * of any listed language, which can be registerd with the
163 * i-prefix, such as i-cherokee
168 $this->singleI18NCountry();
169 if ($this->I18NCountry->isValidCode($sub)) {
170 $this->_country[$lang][] = strToUpper($sub);
172 $this->_langVariation[$lang][] = $sub;
176 $this->_acceptLanguage[] = $lang;
186 function _negotiateEncoding()
188 if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
191 foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $encoding) {
192 if (!empty($encoding)) {
193 $this->_acceptEncoding[] = preg_replace('/;.*/', '', $encoding);
203 * @param string $lang
204 * @param array $countries
206 function getCountryMatch($lang, $countries = null)
208 return $this->_getMatch(
210 @$this->_country[$lang],
211 $this->_defaultCountry
216 * Return variant info for passed parameter.
220 * @param string $lang
222 function getVariantInfo($lang)
224 return isset($this->_langVariation[$lang]) ? $this->_langVariation[$lang] : null;
228 * Find Encoding match
233 * @param array $encodings
235 function getCharsetMatch($encodings = null)
237 return $this->_getMatch(
239 $this->_acceptEncoding,
240 $this->_defaultEncoding
245 * Find Encoding match
249 * @param array $encodings
251 function getEncodingMatch($encodings = null)
253 return $this->_getMatch(
255 $this->_acceptEncoding,
256 $this->_defaultEncoding
261 * Find Language match
265 * @param array $langs
267 function getLanguageMatch($langs = null)
269 return $this->_getMatch(
271 $this->_acceptLanguage,
272 $this->_defaultLanguage
281 * @param array $langs
282 * @param array $countries
284 function getLocaleMatch($langs = null, $countries = null)
286 $lang = $this->_getMatch($langs, $this->_acceptLanguage, $this->_defaultLanguage);
287 $ctry = $this->_getMatch($countries, @$this->_country[$lang], $this->_defaultCountry);
288 return $lang . ($ctry ? '_' . $ctry : '');
292 * Return first matched value from first and second parameter.
293 * If there is no match found, then return third parameter.
297 * @param array $needle
298 * @param array $haystack
299 * @param string $default
301 function _getMatch($needle, $haystack, $default = '')
307 return current($haystack);
309 if ($result = current($a = array_intersect($haystack, $needle))) {
316 * Find Country name for country code passed
320 * @param string $code country code
322 function getCountryName($code)
324 $this->singleI18NCountry();
325 return $this->I18NCountry->getName($code);
329 * Find Country name for country code passed
333 * @param string $code language code
335 function getLanguageName($code)
337 $this->singleI18NLanguage();
338 return $this->I18NLang->getName($code);
342 * Create the Language helper object
347 function &singleI18NLanguage()
349 if (!isset($this->I18NLang)) {
350 include_once 'I18Nv2/Language.php';
351 $this->I18NLang = &new I18Nv2_Language(
352 $this->_defaultLanguage,
353 $this->_defaultEncoding
356 return $this->I18NLang;
360 * Create the Country helper object
365 function &singleI18NCountry()
367 if (!isset($this->I18NCountry)) {
368 include_once 'I18Nv2/Country.php';
369 $this->I18NCountry = &new I18Nv2_Country(
370 $this->_defaultLanguage,
371 $this->_defaultEncoding
374 return $this->I18NCountry;