PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/accept-to-gettext.php

https://github.com/kpadilha/Tiny-Tiny-RSS
PHP | 184 lines | 84 code | 15 blank | 85 comment | 15 complexity | d3f76bd3e76200924137bc434dd50d26 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. /*
  3. * accept-to-gettext.inc -- convert information in 'Accept-*' headers to
  4. * gettext language identifiers.
  5. * Copyright (c) 2003, Wouter Verhelst <wouter@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Usage:
  22. *
  23. * $locale=al2gt(<array of supported languages/charsets in gettext syntax>,
  24. * <MIME type of document>);
  25. * setlocale('LC_ALL', $locale); // or 'LC_MESSAGES', or whatever...
  26. *
  27. * Example:
  28. *
  29. * $langs=array('nl_BE.ISO-8859-15','nl_BE.UTF-8','en_US.UTF-8','en_GB.UTF-8');
  30. * $locale=al2gt($langs, 'text/html');
  31. * setlocale('LC_ALL', $locale);
  32. *
  33. * Note that this will send out header information (to be
  34. * RFC2616-compliant), so it must be called before anything is sent to
  35. * the user.
  36. *
  37. * Assumptions made:
  38. * * Charset encodings are written the same way as the Accept-Charset
  39. * HTTP header specifies them (RFC2616), except that they're parsed
  40. * case-insensitive.
  41. * * Country codes and language codes are the same in both gettext and
  42. * the Accept-Language syntax (except for the case differences, which
  43. * are dealt with easily). If not, some input may be ignored.
  44. * * The provided gettext-strings are fully qualified; i.e., no "en_US";
  45. * always "en_US.ISO-8859-15" or "en_US.UTF-8", or whichever has been
  46. * used. "en.ISO-8859-15" is OK, though.
  47. * * The language is more important than the charset; i.e., if the
  48. * following is given:
  49. *
  50. * Accept-Language: nl-be, nl;q=0.8, en-us;q=0.5, en;q=0.3
  51. * Accept-Charset: ISO-8859-15, utf-8;q=0.5
  52. *
  53. * And the supplied parameter contains (amongst others) nl_BE.UTF-8
  54. * and nl.ISO-8859-15, then nl_BE.UTF-8 will be picked.
  55. *
  56. * $Log: accept-to-gettext.inc,v $
  57. * Revision 1.1.1.1 2003/11/19 19:31:15 wouter
  58. * * moved to new CVS repo after death of the old
  59. * * Fixed code to apply a default to both Accept-Charset and
  60. * Accept-Language if none of those headers are supplied; patch from
  61. * Dominic Chambers <dominic@encasa.com>
  62. *
  63. * Revision 1.2 2003/08/14 10:23:59 wouter
  64. * Removed little error in Content-Type header syntaxis.
  65. *
  66. */
  67. /* not really important, this one; perhaps I could've put it inline with
  68. * the rest. */
  69. function find_match($curlscore,$curcscore,$curgtlang,$langval,$charval,
  70. $gtlang)
  71. {
  72. if($curlscore < $langval) {
  73. $curlscore=$langval;
  74. $curcscore=$charval;
  75. $curgtlang=$gtlang;
  76. } else if ($curlscore == $langval) {
  77. if($curcscore < $charval) {
  78. $curcscore=$charval;
  79. $curgtlang=$gtlang;
  80. }
  81. }
  82. return array($curlscore, $curcscore, $curgtlang);
  83. }
  84. function al2gt($gettextlangs, $mime) {
  85. /* default to "everything is acceptable", as RFC2616 specifies */
  86. $acceptLang=(($_SERVER["HTTP_ACCEPT_LANGUAGE"] == '') ? '*' :
  87. $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  88. $acceptChar=(($_SERVER["HTTP_ACCEPT_CHARSET"] == '') ? '*' :
  89. $_SERVER["HTTP_ACCEPT_CHARSET"]);
  90. $alparts=@preg_split("/,/",$acceptLang);
  91. $acparts=@preg_split("/,/",$acceptChar);
  92. /* Parse the contents of the Accept-Language header.*/
  93. foreach($alparts as $part) {
  94. $part=trim($part);
  95. if(preg_match("/;/", $part)) {
  96. $lang=@preg_split("/;/",$part);
  97. $score=@preg_split("/=/",$lang[1]);
  98. $alscores[$lang[0]]=$score[1];
  99. } else {
  100. $alscores[$part]=1;
  101. }
  102. }
  103. /* Do the same for the Accept-Charset header. */
  104. /* RFC2616: ``If no "*" is present in an Accept-Charset field, then
  105. * all character sets not explicitly mentioned get a quality value of
  106. * 0, except for ISO-8859-1, which gets a quality value of 1 if not
  107. * explicitly mentioned.''
  108. *
  109. * Making it 2 for the time being, so that we
  110. * can distinguish between "not specified" and "specified as 1" later
  111. * on. */
  112. $acscores["ISO-8859-1"]=2;
  113. foreach($acparts as $part) {
  114. $part=trim($part);
  115. if(preg_match("/;/", $part)) {
  116. $cs=@preg_split("/;/",$part);
  117. $score=@preg_split("/=/",$cs[1]);
  118. $acscores[strtoupper($cs[0])]=$score[1];
  119. } else {
  120. $acscores[strtoupper($part)]=1;
  121. }
  122. }
  123. if($acscores["ISO-8859-1"]==2) {
  124. $acscores["ISO-8859-1"]=(isset($acscores["*"])?$acscores["*"]:1);
  125. }
  126. /*
  127. * Loop through the available languages/encodings, and pick the one
  128. * with the highest score, excluding the ones with a charset the user
  129. * did not include.
  130. */
  131. $curlscore=0;
  132. $curcscore=0;
  133. $curgtlang=NULL;
  134. foreach($gettextlangs as $gtlang) {
  135. $tmp1=preg_replace("/\_/","-",$gtlang);
  136. $tmp2=@preg_split("/\./",$tmp1);
  137. $allang=strtolower($tmp2[0]);
  138. $gtcs=strtoupper($tmp2[1]);
  139. $noct=@preg_split("/-/",$allang);
  140. $testvals=array(
  141. array($alscores[$allang], $acscores[$gtcs]),
  142. array($alscores[$noct[0]], $acscores[$gtcs]),
  143. array($alscores[$allang], $acscores["*"]),
  144. array($alscores[$noct[0]], $acscores["*"]),
  145. array($alscores["*"], $acscores[$gtcs]),
  146. array($alscores["*"], $acscores["*"]));
  147. $found=FALSE;
  148. foreach($testvals as $tval) {
  149. if(!$found && isset($tval[0]) && isset($tval[1])) {
  150. $arr=find_match($curlscore, $curcscore, $curgtlang, $tval[0],
  151. $tval[1], $gtlang);
  152. $curlscore=$arr[0];
  153. $curcscore=$arr[1];
  154. $curgtlang=$arr[2];
  155. $found=TRUE;
  156. }
  157. }
  158. }
  159. /* We must re-parse the gettext-string now, since we may have found it
  160. * through a "*" qualifier.*/
  161. $gtparts=@preg_split("/\./",$curgtlang);
  162. $tmp=strtolower($gtparts[0]);
  163. $lang=preg_replace("/\_/", "-", $tmp);
  164. $charset=$gtparts[1];
  165. header("Content-Language: $lang");
  166. header("Content-Type: $mime; charset=$charset");
  167. return $curgtlang;
  168. }
  169. ?>