PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_v2/inc/class.I18Nlocale.inc.php

http://flaimo-php.googlecode.com/
PHP | 340 lines | 173 code | 37 blank | 130 comment | 35 complexity | a2820e7f9d51c71a0c0aa614fbeaeffb MD5 | raw file
  1. <?php
  2. /**
  3. * load base class which takes care of all the other includes via it's autoload function
  4. */
  5. require_once 'class.I18Nbase.inc.php';
  6. /**
  7. * holds information about locale/country/language selected by the user or automatically; also loads settings L10N settings
  8. * @author Michael Wimmer <flaimo@gmail.com>
  9. * @category flaimo-php
  10. * @example ../www_root/i18n_example_script.php i18n example script
  11. * @license GNU General Public License v3
  12. * @link http://code.google.com/p/flaimo-php/
  13. * @package i18n
  14. * @version 2.3.1
  15. */
  16. class I18Nlocale extends I18Nbase {
  17. /**#@+
  18. * @var string
  19. */
  20. protected $locale;
  21. protected $country;
  22. protected $language;
  23. /**#@-*/
  24. /**#@+
  25. * @var array
  26. */
  27. protected $locales;
  28. protected $countries;
  29. protected $languages;
  30. protected $locale_settings;
  31. /**#@-*/
  32. /**
  33. * @var boolean
  34. */
  35. protected $use_prefs = TRUE;
  36. const TRANSLATE_IP = TRUE;
  37. /**#@-*/
  38. /**
  39. * @param string $locale
  40. * @return void
  41. * @uses I18Nbase::isValidLocaleCode()
  42. * @uses I18Nlocale::addLocale()
  43. * @uses I18Nlocale::$use_prefs
  44. * @uses I18Nlocale::$locale
  45. * @uses I18Nlocale::$language
  46. * @uses I18Nlocale::$country
  47. * @uses I18Nbase::__construct()
  48. */
  49. public function __construct($locale = '') {
  50. if (parent::isValidLocaleCode($locale) === TRUE) {
  51. // hier lokale check machen
  52. $this->addLocale($locale);
  53. $this->use_prefs = FALSE;
  54. $this->locale =& $this->locales[0];
  55. $this->language =& $this->languages[0];
  56. if (count($this->countries) > 0) {
  57. $this->country =& $this->countries[0];
  58. } // end if
  59. } // end if
  60. parent::__construct();
  61. } // end if
  62. /**
  63. * fetches l10n settings from the ini file
  64. * @return void
  65. * @uses I18Nlocale::$locale_settings
  66. * @uses I18Nbase::getI18Nsetting()
  67. */
  68. protected function readL10Nsettings() {
  69. $this->locale_settings = @parse_ini_file(parent::getI18Nsetting('locales_path') . '/' . $this->getI18NLocale() . '/' . 'l10n_settings.ini');
  70. return (boolean) ($this->locale_settings == FALSE) ? FALSE : TRUE;
  71. } // end function
  72. /**
  73. * returns a value for a l10n setting
  74. * @param string $setting
  75. * @return mixed
  76. * @uses I18Nlocale::readL10Nsettings()
  77. * @uses I18Nlocale::$locale_settings
  78. */
  79. public function getL10NSetting($setting = '') {
  80. if (!isset($this->locale_settings)) {
  81. $this->readL10Nsettings();
  82. } // end if
  83. if (array_key_exists($setting, $this->locale_settings)) {
  84. return $this->locale_settings[$setting];
  85. } // end if
  86. return (boolean) FALSE;
  87. } // end function
  88. /**
  89. * returns a country from a given domainname
  90. * @param string $host
  91. * @return mixed
  92. */
  93. protected static function domain2Country($host = '') {
  94. if (($lastdot = strrpos($host,'.')) < 1) {
  95. return (boolean) FALSE;
  96. } // end if
  97. $parts = split('[.]', $host);
  98. $domain_name = array_pop($parts);
  99. /* top level domains (com, org, gov, aero, info)
  100. or eu-domain are all english */
  101. if (strlen($domain_name) > 2 || $domain_name === 'eu') {
  102. return 'en';
  103. } elseif (strlen($domain_name) == 2) { // country domains
  104. return $domain_name;
  105. } // end if
  106. return (boolean) FALSE;
  107. } // end function
  108. /**
  109. * adds a given locale to the locales array
  110. * @param string $locale
  111. * @return void
  112. * @uses I18Nlocale::isValidLocaleCode()
  113. * @uses I18Nlocale::$locales
  114. * @uses I18Nlocale::$languages
  115. * @uses I18Nlocale::$countries
  116. */
  117. protected function addLocale($locale = '') {
  118. if (parent::isValidLocaleCode($locale) == FALSE) {
  119. return (boolean) FALSE;
  120. } // endif
  121. $this->locales[] = strtolower($locale);
  122. $temp = (array) explode('-', $locale);
  123. $language = trim($temp[0]);
  124. if (parent::isValidLocaleCode($language) === TRUE) {
  125. $this->languages[] = strtolower($language);
  126. } // endif
  127. if (isset($temp[1]) &&
  128. parent::isValidLocaleCode(trim($temp[1])) === TRUE) {
  129. $this->countries[] = strtolower(trim($temp[1]));
  130. } // end if
  131. } // end if
  132. /**
  133. * gets the data from the http header request
  134. * @return boolean
  135. * @uses I18Nlocale::addLocale()
  136. * @uses I18Nlocale::$locales
  137. * @uses I18Nlocale::$languages
  138. * @uses I18Nlocale::$countries
  139. */
  140. protected function readUserHeader() {
  141. $client_header = split('[,]', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  142. if (count($client_header) < 1) {
  143. return (boolean) FALSE;
  144. } // end if
  145. foreach ($client_header as $raw_entry) {
  146. $temp = split('[;]', $raw_entry);
  147. $locale = trim($temp[0]);
  148. $this->addLocale($locale);
  149. } // end foreach
  150. if (is_array($this->languages)) {
  151. $this->languages = array_unique($this->languages);
  152. } // end if
  153. if (is_array($this->countries)) {
  154. $this->countries = array_unique($this->countries);
  155. } // end if
  156. $_SESSION['i18n']['settings']['tmp_user_locales'] = $this->locales;
  157. $_SESSION['i18n']['settings']['tmp_user_languages'] = $this->languages;
  158. return (boolean) TRUE;
  159. } // end function
  160. /**
  161. * translates the users ip to a domainname
  162. * @return boolean
  163. * @uses I18Nlocale::domain2Country()
  164. * @uses I18Nlocale::$countries
  165. */
  166. protected function readUserIP() {
  167. $country = $this->domain2Country(gethostbyaddr($_SERVER['REMOTE_ADDR']));
  168. if ($country == FALSE) {
  169. $country = $this->domain2Country($_SERVER['SERVER_NAME']);
  170. } // end if
  171. if ($country == FALSE) {
  172. return (boolean) FALSE;
  173. } // end if
  174. $this->countries[] = $country;
  175. $this->countries = array_unique($this->countries);
  176. $_SESSION['i18n']['settings']['tmp_user_countries'] = $this->countries;
  177. return (boolean) TRUE;
  178. } // end function
  179. /**
  180. * creates locale/language/country arrays
  181. * @return boolean
  182. * @uses I18Nlocale::$locales
  183. * @uses I18Nlocale::$use_prefs
  184. * @uses I18Nbase::getI18Nuser()
  185. * @uses I18Nlocale::addLocale()
  186. * @uses I18Nlocale::$languages
  187. * @uses I18Nlocale::$countries
  188. * @uses I18Nlocale::readUserHeader()
  189. * @uses I18Nlocale::readUserIP()
  190. * @uses I18Nlocale::$translate_ip
  191. * @uses I18Nbase::getI18NSetting(()
  192. */
  193. protected function setLocaleVars() {
  194. if (isset($this->locales) && isset($this->locale)) {
  195. return (boolean) TRUE;
  196. } // end if
  197. if ($this->use_prefs == TRUE && parent::getI18Nuser()->getPrefLocale() != FALSE) {
  198. $this->addLocale(parent::getI18Nuser()->getPrefLocale());
  199. $this->countries[] =& parent::getI18Nuser()->getPrefCountry();
  200. $this->languages[] =& parent::getI18Nuser()->getPrefLanguage();
  201. } elseif (isset($_SESSION['i18n']['settings']['tmp_user_locales'])) {
  202. $sess =& $_SESSION['i18n']['settings'];
  203. $this->locales =& $sess['tmp_user_locales'];
  204. $this->countries =& $sess['tmp_user_countries'];
  205. $this->languages =& $sess['tmp_user_languages'];
  206. } else {
  207. $this->readUserHeader();
  208. if (count($this->countries) < 1 && self::TRANSLATE_IP == TRUE) {
  209. $this->readUserIP();
  210. } // end if
  211. } // end if
  212. if (count($this->locales) < 1) {
  213. $this->locale =& parent::getI18NSetting('default_locale');
  214. $this->country =& parent::getI18NSetting('default_country');
  215. $this->language =& parent::getI18NSetting('default_language');
  216. return (boolean) TRUE;
  217. } // end if
  218. $this->locale =& $this->locales[0];
  219. $this->language =& $this->languages[0];
  220. if (count($this->countries) > 0) {
  221. $this->country =& $this->countries[0];
  222. return (boolean) TRUE;
  223. } // end if
  224. $this->country =& parent::getI18Nsetting('default_country');
  225. return (boolean) TRUE;
  226. } // end function
  227. /**
  228. * returns a class var
  229. * @param string $var
  230. * @return mixed
  231. * @uses I18Nlocale::setLocaleVars()
  232. * @uses I18Nbase::getVar()
  233. */
  234. protected function getVar($var = '') {
  235. if (!isset($this->$var)) {
  236. $this->setLocaleVars();
  237. } // end if
  238. return parent::getVar($var);
  239. } // end function
  240. /**
  241. * assigns a value to a class var
  242. * @param string $data
  243. * @param string $var
  244. * @return boolean
  245. * @uses I18Nlocale::isValidLocaleCode()
  246. * @uses I18Nbase::setVar()
  247. */
  248. protected function setVar($data = '', $var = '', $type = '') {
  249. if (parent::isValidLocaleCode($data) == FALSE) {
  250. return (boolean) FALSE;
  251. } // end if
  252. return (boolean) parent::setVar(strtolower($data), $var, 'string');
  253. } // end function
  254. /**#@+
  255. * assigns a value to a class var
  256. * @return boolean
  257. * @uses I18Nlocale::setVar()
  258. */
  259. /**
  260. * @param string $locale
  261. */
  262. public function setI18NLocale($locale = '') {
  263. return (boolean) $this->setVar($locale, 'locale');
  264. } // end function
  265. /**
  266. * @param string $country
  267. */
  268. public function setI18NCountry($country = '') {
  269. return (boolean) $this->setVar($country, 'country');
  270. } // end function
  271. /**
  272. * @param string $language
  273. */
  274. public function setI18NLanguage($language = '') {
  275. return (boolean) $this->setVar($language, 'language');
  276. } // end function
  277. /**#@-*/
  278. /**#@+
  279. * returns a class var
  280. * @return string
  281. * @uses I18Nlocale::getVar()
  282. */
  283. public function getI18NLocale() {
  284. return $this->getVar('locale');
  285. } // end function
  286. public function getI18NCountry() {
  287. return $this->getVar('country');
  288. } // end function
  289. public function getI18NLanguage() {
  290. return $this->getVar('language');
  291. } // end function
  292. public function getI18NLanguages() {
  293. return $this->getVar('languages');
  294. } // end function
  295. public function getI18NLocales() {
  296. return $this->getVar('locales');
  297. } // end function
  298. /**#@-*/
  299. function __toString() {
  300. return $this->getI18NLocale();
  301. } // end function
  302. } // end class I18Nlocale
  303. ?>