3
* convert information in 'Accept-*' headers to gettext language
6
* Copyright (c) 2003, Wouter Verhelst <wouter@debian.org>
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.
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.
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
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...
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);
34
* Note that this will send out header information (to be
35
* RFC2616-compliant), so it must be called before anything is sent to
39
* * Charset encodings are written the same way as the Accept-Charset
40
* HTTP header specifies them (RFC2616), except that they're parsed
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
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
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.
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>
64
* Revision 1.2 2003/08/14 10:23:59 wouter
65
* Removed little error in Content-Type header syntaxis.
69
/* not really important, this one; perhaps I could've put it inline with
71
function find_match($curlscore,$curcscore,$curgtlang,$langval,$charval,
74
if($curlscore < $langval) {
78
} else if ($curlscore == $langval) {
79
if($curcscore < $charval) {
84
return array($curlscore, $curcscore, $curgtlang);
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);
96
/* Parse the contents of the Accept-Language header.*/
97
foreach($alparts as $part) {
99
if(preg_match("/;/", $part)) {
100
$lang=@preg_split("/;/",$part);
101
$score=@preg_split("/=/",$lang[1]);
102
$alscores[$lang[0]]=$score[1];
108
/* Do the same for the Accept-Charset header. */
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.''
115
* Making it 2 for the time being, so that we
116
* can distinguish between "not specified" and "specified as 1" later
118
$acscores["ISO-8859-1"]=2;
120
foreach($acparts as $part) {
122
if(preg_match("/;/", $part)) {
123
$cs=@preg_split("/;/",$part);
124
$score=@preg_split("/=/",$cs[1]);
125
$acscores[strtoupper($cs[0])]=$score[1];
127
$acscores[strtoupper($part)]=1;
130
if($acscores["ISO-8859-1"]==2) {
131
$acscores["ISO-8859-1"]=(isset($acscores["*"])?$acscores["*"]:1);
135
* Loop through the available languages/encodings, and pick the one
136
* with the highest score, excluding the ones with a charset the user
142
foreach($gettextlangs as $gtlang) {
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);
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["*"]));
159
foreach($testvals as $tval) {
160
if(!$found && isset($tval[0]) && isset($tval[1])) {
161
$arr=find_match($curlscore, $curcscore, $curgtlang, $tval[0],
171
/* We must re-parse the gettext-string now, since we may have found it
172
* through a "*" qualifier.*/
174
$gtparts=@preg_split("/\./",$curgtlang);
175
$tmp=strtolower($gtparts[0]);
176
$lang=preg_replace("/\_/", "-", $tmp);
177
$charset=@$gtparts[1];
179
header("Content-Language: $lang");
180
header("Content-Type: $mime; charset=$charset");