/xampp/php/PEAR/I18Nv2/Negotiator.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site · PHP · 377 lines · 150 code · 26 blank · 201 comment · 15 complexity · 3c35408bea0dd89bbe16aab86ffb1ae4 MD5 · raw file

  1. <?php
  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. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Negotiator.php,v 1.11 2005/11/28 15:33:22 mike Exp $
  19. /**
  20. * I18Nv2::Negotiator
  21. *
  22. * @package I18Nv2
  23. * @category Internationalization
  24. */
  25. /**
  26. * I18Nv2_Negotiator
  27. *
  28. * @author Naoki Shima <murahachibu@php.net>
  29. * @author Wolfram Kriesing <wk@visionp.de>
  30. * @author Michael Wallner <mike@php.net>
  31. * @version $Revision: 1.11 $
  32. * @access public
  33. * @package I18Nv2
  34. */
  35. class I18Nv2_Negotiator
  36. {
  37. /**
  38. * I18Nv2_Language
  39. *
  40. * @var object
  41. * @access public
  42. */
  43. var $I18NLang = null;
  44. /**
  45. * I18Nv2_Country
  46. *
  47. * @var object
  48. * @access public
  49. */
  50. var $I18NCountry = null;
  51. /**
  52. * Save default country code.
  53. *
  54. * @var string
  55. * @access private
  56. */
  57. var $_defaultCountry;
  58. /**
  59. * Save default language code.
  60. *
  61. * @var string
  62. * @access private
  63. */
  64. var $_defaultLanguage;
  65. /**
  66. * Save default encoding code.
  67. *
  68. * @var string
  69. * @access private
  70. */
  71. var $_defaultEncoding;
  72. /**
  73. * HTTP_ACCEPT_CHARSET
  74. *
  75. * @var array
  76. * @access private
  77. */
  78. var $_acceptEncoding = array();
  79. /**
  80. * HTTP_ACCEPT_LANGUAGE
  81. *
  82. * @var array
  83. * @access private
  84. */
  85. var $_acceptLanguage = array();
  86. /**
  87. * Language variations
  88. *
  89. * @var array
  90. * @access private
  91. */
  92. var $_langVariation = array();
  93. /**
  94. * Countries
  95. *
  96. * @var array
  97. * @access private
  98. */
  99. var $_country = array();
  100. /**
  101. * Constructor
  102. *
  103. * Find language code, country code, encoding code, and dialect or variant
  104. * of Locale setting in HTTP request headers.
  105. *
  106. * @access public
  107. * @param string $defaultLanguage Default Language
  108. * @param string $defaultEncoding Default Encoding
  109. * @param string $defaultCountry Default Country
  110. */
  111. function I18Nv2_Negotiator($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
  112. {
  113. $this->__construct($defaultLanguage, $defaultEncoding, $defaultCountry);
  114. }
  115. /**
  116. * ZE2 Constructor
  117. * @ignore
  118. */
  119. function __construct($defaultLanguage = 'en', $defaultEncoding = 'iso-8859-1', $defaultCountry = '')
  120. {
  121. $this->_defaultCountry = $defaultCountry;
  122. $this->_defaultLanguage = $defaultLanguage;
  123. $this->_defaultEncoding = $defaultEncoding;
  124. $this->_negotiateLanguage();
  125. $this->_negotiateEncoding();
  126. }
  127. /**
  128. * Negotiate Language
  129. *
  130. * @access private
  131. * @return void
  132. */
  133. function _negotiateLanguage()
  134. {
  135. if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  136. return;
  137. }
  138. foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
  139. // Cut off any q-value that might come after a semi-colon
  140. if ($pos = strpos($lang, ';')) {
  141. $lang = trim(substr($lang, 0, $pos));
  142. }
  143. if (strstr($lang, '-')) {
  144. list($pri, $sub) = explode('-', $lang);
  145. if ($pri == 'i') {
  146. /**
  147. * Language not listed in ISO 639 that are not variants
  148. * of any listed language, which can be registerd with the
  149. * i-prefix, such as i-cherokee
  150. */
  151. $lang = $sub;
  152. } else {
  153. $lang = $pri;
  154. $this->singleI18NCountry();
  155. if ($this->I18NCountry->isValidCode($sub)) {
  156. $this->_country[$lang][] = strToUpper($sub);
  157. } else {
  158. $this->_langVariation[$lang][] = $sub;
  159. }
  160. }
  161. }
  162. $this->_acceptLanguage[] = $lang;
  163. }
  164. }
  165. /**
  166. * Negotiate Encoding
  167. *
  168. * @access private
  169. * @return void
  170. */
  171. function _negotiateEncoding()
  172. {
  173. if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  174. return;
  175. }
  176. foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $encoding) {
  177. if (!empty($encoding)) {
  178. $this->_acceptEncoding[] = preg_replace('/;.*/', '', $encoding);
  179. }
  180. }
  181. }
  182. /**
  183. * Find Country Match
  184. *
  185. * @access public
  186. * @return array
  187. * @param string $lang
  188. * @param array $countries
  189. */
  190. function getCountryMatch($lang, $countries = null)
  191. {
  192. return $this->_getMatch(
  193. $countries,
  194. @$this->_country[$lang],
  195. $this->_defaultCountry
  196. );
  197. }
  198. /**
  199. * Return variant info for passed parameter.
  200. *
  201. * @access public
  202. * @return string
  203. * @param string $lang
  204. */
  205. function getVariantInfo($lang)
  206. {
  207. return isset($this->_langVariation[$lang]) ? $this->_langVariation[$lang] : null;
  208. }
  209. /**
  210. * Find Encoding match
  211. *
  212. * @deprecated
  213. * @access public
  214. * @return string
  215. * @param array $encodings
  216. */
  217. function getCharsetMatch($encodings = null)
  218. {
  219. return $this->_getMatch(
  220. $encodings,
  221. $this->_acceptEncoding,
  222. $this->_defaultEncoding
  223. );
  224. }
  225. /**
  226. * Find Encoding match
  227. *
  228. * @access public
  229. * @return string
  230. * @param array $encodings
  231. */
  232. function getEncodingMatch($encodings = null)
  233. {
  234. return $this->_getMatch(
  235. $encodings,
  236. $this->_acceptEncoding,
  237. $this->_defaultEncoding
  238. );
  239. }
  240. /**
  241. * Find Language match
  242. *
  243. * @access public
  244. * @return string
  245. * @param array $langs
  246. */
  247. function getLanguageMatch($langs = null)
  248. {
  249. return $this->_getMatch(
  250. $langs,
  251. $this->_acceptLanguage,
  252. $this->_defaultLanguage
  253. );
  254. }
  255. /**
  256. * Find locale match
  257. *
  258. * @access public
  259. * @return string
  260. * @param array $langs
  261. * @param array $countries
  262. */
  263. function getLocaleMatch($langs = null, $countries = null)
  264. {
  265. $lang = $this->_getMatch($langs, $this->_acceptLanguage, $this->_defaultLanguage);
  266. $ctry = $this->_getMatch($countries, @$this->_country[$lang], $this->_defaultCountry);
  267. return $lang . ($ctry ? '_' . $ctry : '');
  268. }
  269. /**
  270. * Return first matched value from first and second parameter.
  271. * If there is no match found, then return third parameter.
  272. *
  273. * @access private
  274. * @return string
  275. * @param array $needle
  276. * @param array $haystack
  277. * @param string $default
  278. */
  279. function _getMatch($needle, $haystack, $default = '')
  280. {
  281. if (!$haystack) {
  282. return $default;
  283. }
  284. if (!$needle) {
  285. return current($haystack);
  286. }
  287. if ($result = current($a = array_intersect($haystack, $needle))) {
  288. return $result;
  289. }
  290. return $default;
  291. }
  292. /**
  293. * Find Country name for country code passed
  294. *
  295. * @access private
  296. * @return void
  297. * @param string $code country code
  298. */
  299. function getCountryName($code)
  300. {
  301. $this->singleI18NCountry();
  302. return $this->I18NCountry->getName($code);
  303. }
  304. /**
  305. * Find Country name for country code passed
  306. *
  307. * @access private
  308. * @return void
  309. * @param string $code language code
  310. */
  311. function getLanguageName($code)
  312. {
  313. $this->singleI18NLanguage();
  314. return $this->I18NLang->getName($code);
  315. }
  316. /**
  317. * Create the Language helper object
  318. *
  319. * @access public
  320. * @return object
  321. */
  322. function &singleI18NLanguage()
  323. {
  324. if (!isset($this->I18NLang)) {
  325. include_once 'I18Nv2/Language.php';
  326. $this->I18NLang = &new I18Nv2_Language(
  327. $this->_defaultLanguage,
  328. $this->_defaultEncoding
  329. );
  330. }
  331. return $this->I18NLang;
  332. }
  333. /**
  334. * Create the Country helper object
  335. *
  336. * @access public
  337. * @return object
  338. */
  339. function &singleI18NCountry()
  340. {
  341. if (!isset($this->I18NCountry)) {
  342. include_once 'I18Nv2/Country.php';
  343. $this->I18NCountry = &new I18Nv2_Country(
  344. $this->_defaultLanguage,
  345. $this->_defaultEncoding
  346. );
  347. }
  348. return $this->I18NCountry;
  349. }
  350. }
  351. ?>