Commit 007a29f2b0fdeb31ea402217ebdf0642d53a44d3
1 parent
7987dbe8
Type: i18n Framework.
Description: Added i18n support functions. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@2844 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
2 changed files
with
230 additions
and
0 deletions
lib/i18n/accept-to-gettext.inc
0 → 100644
| 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$ | ||
| 57 | + * Revision 1.1 2004/05/10 15:58:37 michael | ||
| 58 | + * Type: i18n Framework. | ||
| 59 | + * Description: Added i18n support functions. | ||
| 60 | + * | ||
| 61 | + * Revision 1.1.1.1 2003/11/19 19:31:15 wouter | ||
| 62 | + * * moved to new CVS repo after death of the old | ||
| 63 | + * * Fixed code to apply a default to both Accept-Charset and | ||
| 64 | + * Accept-Language if none of those headers are supplied; patch from | ||
| 65 | + * Dominic Chambers <dominic@encasa.com> | ||
| 66 | + * | ||
| 67 | + * Revision 1.2 2003/08/14 10:23:59 wouter | ||
| 68 | + * Removed little error in Content-Type header syntaxis. | ||
| 69 | + * | ||
| 70 | + */ | ||
| 71 | + | ||
| 72 | +/* not really important, this one; perhaps I could've put it inline with | ||
| 73 | + * the rest. */ | ||
| 74 | +function find_match($curlscore,$curcscore,$curgtlang,$langval,$charval, | ||
| 75 | + $gtlang) | ||
| 76 | +{ | ||
| 77 | + if($curlscore < $langval) { | ||
| 78 | + $curlscore=$langval; | ||
| 79 | + $curcscore=$charval; | ||
| 80 | + $curgtlang=$gtlang; | ||
| 81 | + } else if ($curlscore == $langval) { | ||
| 82 | + if($curcscore < $charval) { | ||
| 83 | + $curcscore=$charval; | ||
| 84 | + $curgtlang=$gtlang; | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + return array($curlscore, $curcscore, $curgtlang); | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +function al2gt($gettextlangs, $mime) { | ||
| 91 | + /* default to "everything is acceptable", as RFC2616 specifies */ | ||
| 92 | + $acceptLang=(($_SERVER["HTTP_ACCEPT_LANGUAGE"] == '') ? '*' : | ||
| 93 | + $_SERVER["HTTP_ACCEPT_LANGUAGE"]); | ||
| 94 | + $acceptChar=(($_SERVER["HTTP_ACCEPT_CHARSET"] == '') ? '*' : | ||
| 95 | + $_SERVER["HTTP_ACCEPT_CHARSET"]); | ||
| 96 | + $alparts=@preg_split("/,/",$acceptLang); | ||
| 97 | + $acparts=@preg_split("/,/",$acceptChar); | ||
| 98 | + | ||
| 99 | + /* Parse the contents of the Accept-Language header.*/ | ||
| 100 | + foreach($alparts as $part) { | ||
| 101 | + $part=trim($part); | ||
| 102 | + if(preg_match("/;/", $part)) { | ||
| 103 | + $lang=@preg_split("/;/",$part); | ||
| 104 | + $score=@preg_split("/=/",$lang[1]); | ||
| 105 | + $alscores[$lang[0]]=$score[1]; | ||
| 106 | + } else { | ||
| 107 | + $alscores[$part]=1; | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + /* Do the same for the Accept-Charset header. */ | ||
| 112 | + | ||
| 113 | + /* RFC2616: ``If no "*" is present in an Accept-Charset field, then | ||
| 114 | + * all character sets not explicitly mentioned get a quality value of | ||
| 115 | + * 0, except for ISO-8859-1, which gets a quality value of 1 if not | ||
| 116 | + * explicitly mentioned.'' | ||
| 117 | + * | ||
| 118 | + * Making it 2 for the time being, so that we | ||
| 119 | + * can distinguish between "not specified" and "specified as 1" later | ||
| 120 | + * on. */ | ||
| 121 | + $acscores["ISO-8859-1"]=2; | ||
| 122 | + | ||
| 123 | + foreach($acparts as $part) { | ||
| 124 | + $part=trim($part); | ||
| 125 | + if(preg_match("/;/", $part)) { | ||
| 126 | + $cs=@preg_split("/;/",$part); | ||
| 127 | + $score=@preg_split("/=/",$cs[1]); | ||
| 128 | + $acscores[strtoupper($cs[0])]=$score[1]; | ||
| 129 | + } else { | ||
| 130 | + $acscores[strtoupper($part)]=1; | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + if($acscores["ISO-8859-1"]==2) { | ||
| 134 | + $acscores["ISO-8859-1"]=(isset($acscores["*"])?$acscores["*"]:1); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + /* | ||
| 138 | + * Loop through the available languages/encodings, and pick the one | ||
| 139 | + * with the highest score, excluding the ones with a charset the user | ||
| 140 | + * did not include. | ||
| 141 | + */ | ||
| 142 | + $curlscore=0; | ||
| 143 | + $curcscore=0; | ||
| 144 | + $curgtlang=NULL; | ||
| 145 | + foreach($gettextlangs as $gtlang) { | ||
| 146 | + | ||
| 147 | + $tmp1=preg_replace("/\_/","-",$gtlang); | ||
| 148 | + $tmp2=@preg_split("/\./",$tmp1); | ||
| 149 | + $allang=strtolower($tmp2[0]); | ||
| 150 | + $gtcs=strtoupper($tmp2[1]); | ||
| 151 | + $noct=@preg_split("/-/",$allang); | ||
| 152 | + | ||
| 153 | + $testvals=array( | ||
| 154 | + array($alscores[$allang], $acscores[$gtcs]), | ||
| 155 | + array($alscores[$noct[0]], $acscores[$gtcs]), | ||
| 156 | + array($alscores[$allang], $acscores["*"]), | ||
| 157 | + array($alscores[$noct[0]], $acscores["*"]), | ||
| 158 | + array($alscores["*"], $acscores[$gtcs]), | ||
| 159 | + array($alscores["*"], $acscores["*"])); | ||
| 160 | + | ||
| 161 | + $found=FALSE; | ||
| 162 | + foreach($testvals as $tval) { | ||
| 163 | + if(!$found && isset($tval[0]) && isset($tval[1])) { | ||
| 164 | + $arr=find_match($curlscore, $curcscore, $curgtlang, $tval[0], | ||
| 165 | + $tval[1], $gtlang); | ||
| 166 | + $curlscore=$arr[0]; | ||
| 167 | + $curcscore=$arr[1]; | ||
| 168 | + $curgtlang=$arr[2]; | ||
| 169 | + $found=TRUE; | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + /* We must re-parse the gettext-string now, since we may have found it | ||
| 175 | + * through a "*" qualifier.*/ | ||
| 176 | + | ||
| 177 | + $gtparts=@preg_split("/\./",$curgtlang); | ||
| 178 | + $tmp=strtolower($gtparts[0]); | ||
| 179 | + $lang=preg_replace("/\_/", "-", $tmp); | ||
| 180 | + $charset=$gtparts[1]; | ||
| 181 | + | ||
| 182 | + header("Content-Language: $lang"); | ||
| 183 | + header("Content-Type: $mime; charset=$charset"); | ||
| 184 | + | ||
| 185 | + return $curgtlang; | ||
| 186 | +} | ||
| 187 | + | ||
| 188 | +?> | ||
| 0 | \ No newline at end of file | 189 | \ No newline at end of file |
lib/i18n/languageFunctions.inc
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * $Id$ | ||
| 4 | + * | ||
| 5 | + * i18n helper functions. | ||
| 6 | + * | ||
| 7 | + * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com | ||
| 8 | + * | ||
| 9 | + * This program is free software; you can redistribute it and/or modify | ||
| 10 | + * it under the terms of the GNU General Public License as published by | ||
| 11 | + * the Free Software Foundation; either version 2 of the License, or | ||
| 12 | + * (at your option) any later version. | ||
| 13 | + * | ||
| 14 | + * This program is distributed in the hope that it will be useful, | ||
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | + * GNU General Public License for more details. | ||
| 18 | + * | ||
| 19 | + * You should have received a copy of the GNU General Public License | ||
| 20 | + * along with this program; if not, write to the Free Software | ||
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 22 | + */ | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Returns a list of available gettext locales. | ||
| 26 | + */ | ||
| 27 | +function getInstalledLocales() { | ||
| 28 | + global $default; | ||
| 29 | + // get a list of directories in ($default->fileSystemRoot . "/i18n") | ||
| 30 | + $aLocales = array(); | ||
| 31 | + if ($handle = opendir($default->fileSystemRoot . "/i18n")) { | ||
| 32 | + while (false !== ($file = readdir($handle))) { | ||
| 33 | + if ($file != "." && $file != ".." && | ||
| 34 | + $file != "CVS" && is_dir("$default->fileSystemRoot/i18n/$file")) { | ||
| 35 | + $aLocales[] = $file; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + closedir($handle); | ||
| 39 | + } | ||
| 40 | + return $aLocales; | ||
| 41 | +} | ||
| 42 | +?> | ||
| 0 | \ No newline at end of file | 43 | \ No newline at end of file |