~budgester/irm/trunk

« back to all changes in this revision

Viewing changes to include/accept-to-gettext.php

  • Committer: budgester at budgester
  • Date: 2008-03-05 23:14:13 UTC
  • Revision ID: budgester@budgester.com-20080305231413-k5vqfuckfo09ju42
Initial import of IRM codebase

Show diffs side-by-side

added added

removed removed

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