Source for file class.Language.inc

Documentation is available at class.Language.inc

  1. <?php //-*-php-*-
  2. /* ******************************************************************** **
  3. ** Copyright notice **
  4. ** **
  5. ** (c) 1995-2003 PHPOpenChat Development Team **
  6. ** http://phpopenchat.sourceforge.net/ **
  7. ** **
  8. ** All rights reserved **
  9. ** **
  10. ** This script is part of the PHPOpenChat project. The PHPOpenChat **
  11. ** project is free software; you can redistribute it and/or modify **
  12. ** it under the terms of the GNU General Public License as published by **
  13. ** the Free Software Foundation; either version 2 of the License, or **
  14. ** (at your option) any later version. **
  15. ** **
  16. ** The GNU General Public License can be found at **
  17. ** http://www.gnu.org/copyleft/gpl.html. **
  18. ** A copy is found in the textfile GPL and important notices to the **
  19. ** license from the team is found in the textfile LICENSE distributed **
  20. ** with these scripts. **
  21. ** **
  22. ** This script is distributed in the hope that it will be useful, **
  23. ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
  24. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
  25. ** GNU General Public License for more details. **
  26. ** **
  27. ** This copyright notice MUST APPEAR in all copies of the script! **
  28. ** ******************************************************************** */
  29.  
  30. /**
  31. * Implements language negotiation
  32. *
  33. * @package phpopenchat
  34. * @author Carlos Falo Hervás <carles@bisinteractive.net> {@link http://www.bisinteractive.net/}
  35. * @access public
  36. * @version $Id: class.Language.inc,v 1.5 2003/10/18 08:39:03 letreo Exp $
  37. */
  38. class Language {
  39. /**
  40. * Counter for how many languages are supported
  41. *
  42. * @var int
  43. * @access public
  44. * @see lang_init()
  45. */
  46. var $langs = 0;
  47. /**
  48. * Language List [lang,q] pairs
  49. *
  50. * @var array
  51. * @access public
  52. * @see lang_init()
  53. */
  54. var $lang_list;
  55. /**
  56. * Original http header field HTTP_ACCEPT_LANGUAGES
  57. *
  58. * @var string
  59. * @access private
  60. * @see lang_detect()
  61. */
  62. var $accept_lang;
  63. /**
  64. * Constructor.
  65. *
  66. * Sets values for initial workout
  67. *
  68. * @access public
  69. */
  70. function Language()
  71. {
  72. $this->accept_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  73. $this->lang_init();
  74. }
  75. /**
  76. * Compares given locale PREFIX with PRIMARY LANG PREFIX
  77. *
  78. * @access public
  79. * @return int if given locale PREFIX matches PRIMARY LANG PREFIX
  80. */
  81. function is_primarylang($locale)
  82. {
  83. $this->reset_list();
  84. $slist = explode('-',$locale);
  85. $lc = $slist[0];
  86. $slist = explode('-',key($this->lang_list));
  87. $tlc = $slist[0];
  88. if ($lc==$tlc)
  89. {
  90. return 1;
  91. }
  92. else
  93. {
  94. return 0;
  95. }
  96. }
  97. /**
  98. * Compares given locale PREFIX with PRIMARY LANG PREFIX
  99. *
  100. * @access public
  101. * @return int if given locale PREFIX matches PRIMARY LANG PREFIX
  102. */
  103. function get_primarylang()
  104. {
  105. // Return full locale string for primary language
  106. $this->reset_list();
  107. return key($this->lang_list);
  108. }
  109. /**
  110. * Returns the PREFIX string for the client's primary language selection
  111. *
  112. * @access public
  113. * @return int PREFIX string for primary language
  114. */
  115. function get_primaryprefix()
  116. {
  117. $this->reset_list();
  118. $slist = explode('-',key($this->lang_list));
  119. return $slist[0];
  120. }
  121. /**
  122. * Retunrs QUALITY for PREFIX in given $loc (QUALITY = q*100 -> percent)
  123. *
  124. * @access public
  125. * @return int for the PREFIX in given locale
  126. */
  127. function find_lang($loc)
  128. {
  129. $found = 0;
  130. $this->reset_list();
  131. $slist = explode('-',$locale);
  132. $lc = $slist[0];
  133. while ($curr = each($this->lang_list))
  134. {
  135. $slist = explode('-',$curr[0]);
  136. $tlc = $slist[0];
  137. if ($tlc==$lc)
  138. return $curr[1];
  139. }
  140. return 0;
  141. }
  142. /**
  143. * Fills sublist with an ordered list of accepted languages from given array
  144. * based on each locale PREFIX... ordering is on Q DESC so first element in
  145. * the array is best match from the given list.
  146. *
  147. * @access public
  148. * @return int
  149. */
  150. function get_list($arr,&$sublist)
  151. {
  152. $this->reset_list();
  153. while($curr = each($this->lang_list))
  154. {
  155. $slist = explode('-',$curr[0]);
  156. //letreo, 07/13/2002
  157. //bugfix: without a trim, there is a leading space in $loc
  158. //from this it follows that no language within $arr will be found
  159. $loc = trim($slist[0]);
  160. if (in_array($loc,$arr))
  161. $sublist += array($loc => $curr[1]);
  162. }
  163. if (count($sublist))
  164. {
  165. // This will order based on Q DESC
  166. arsort($sublist);
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * @access public
  174. */
  175. function print_list()
  176. {
  177. $this->reset_list();
  178. while($curr=each($this->lang_list))
  179. {
  180. echo $curr[0] . ': ' . $curr[1] .'<br>';
  181. }
  182. }
  183. /**
  184. * Initialize language list
  185. *
  186. * @access private
  187. * @return array
  188. */
  189. function lang_init()
  190. {
  191. $this->lang_list = Array();
  192. $list = explode(',',$this->accept_lang);
  193. for ($i=0;$i<count($list);$i++)
  194. {
  195. $pos = strchr($list[$i],';');
  196. if ($pos === false)
  197. {
  198. // No Q it is only a locale...
  199. $this->lang_list += Array($list[$i] => 100);
  200. $this->langs++;
  201. } else {
  202. // Has a Q rating
  203. $q = explode(';',$list[$i]);
  204. $loc = $q[0];
  205. $q = explode('=',$q[1]);
  206. $this->lang_list += Array($loc => ($q[1]*100));
  207. $this->langs++;
  208. }
  209. }
  210. return ($this->langs);
  211. }
  212. /**
  213. * Reset language list
  214. *
  215. * @access private
  216. */
  217. function reset_list()
  218. {
  219. reset($this->lang_list);
  220. }
  221. }
  222. ?>

Documentation generated on Tue, 29 Jun 2004 14:42:07 +0200 by phpDocumentor 1.3.0RC3