Index: trunk/tools/ToolserverI18N/TsIntuition.php |
— | — | @@ -1217,42 +1217,6 @@ |
1218 | 1218 | } |
1219 | 1219 | |
1220 | 1220 | /** |
1221 | | - * Return a list of acceptable languages from an Accept-Language header |
1222 | | - * @param $acceptLanguage String List of language tags, as given in |
1223 | | - * http Accept-Language header (omit to fetch from $_SERVER['HTTP_ACCEPT_LANGUAGE']) |
1224 | | - * @return array sorted with the candidate languages as keys and q-values asvalues. |
1225 | | - */ |
1226 | | - static function acceptableLanguages($acceptLanguage = false) { |
1227 | | - if ( $acceptLanguage === false ) { |
1228 | | - $acceptLanguage = @$_SERVER['HTTP_ACCEPT_LANGUAGE']; |
1229 | | - } |
1230 | | - |
1231 | | - $acceptableLanguages = array(); |
1232 | | - |
1233 | | - //Accept-Language: 1#( language-range [ ";" "q" "=" qvalue ] ) |
1234 | | - //The list of elements is separated by comma and optional LWS |
1235 | | - $languages = explode( ',', $acceptLanguage ); |
1236 | | - foreach ( $languages as $language ) { |
1237 | | - $language = trim( $language ); // Remove optional LWS |
1238 | | - |
1239 | | - // Extract the language-range and q-value |
1240 | | - if ( !preg_match( '/^([A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*|\*)(?:\s*;\s*q\s*=\s*([01](?:\.[0-9]{0,3})?))?$/', $language, $m ) ) |
1241 | | - continue; |
1242 | | - |
1243 | | - // We are not interested in the total match. |
1244 | | - array_shift( $m ); |
1245 | | - $m[] = 1; // Default q-value is 1 |
1246 | | - list( $languageRange, $qvalue ) = $m; |
1247 | | - |
1248 | | - $acceptableLanguages[$languageRange] = $qvalue; |
1249 | | - } |
1250 | | - |
1251 | | - arsort( $acceptableLanguages, SORT_NUMERIC ); // This is not an stable sort, but it isn't needed |
1252 | | - |
1253 | | - return $acceptableLanguages; |
1254 | | - } |
1255 | | - |
1256 | | - /** |
1257 | 1221 | * Check language choice tree in the following order: |
1258 | 1222 | * - First: Construct override |
1259 | 1223 | * - Second: Parameter override |
— | — | @@ -1266,46 +1230,54 @@ |
1267 | 1231 | */ |
1268 | 1232 | private function initLangSelect( $option ) { |
1269 | 1233 | $set = false; |
| 1234 | + |
1270 | 1235 | if ( isset( $option ) && !empty( $option ) ) { |
1271 | 1236 | $set = $this->setLang( $option ); |
1272 | 1237 | } |
| 1238 | + |
1273 | 1239 | if ( !$set && $this->getUseRequestParam() === true && isset( $_GET[ $this->paramNames['userlang'] ] ) ) { |
1274 | 1240 | $set = $this->setLang( $_GET[ $this->paramNames['userlang'] ] ); |
1275 | 1241 | } |
| 1242 | + |
1276 | 1243 | if ( !$set && isset( $_COOKIE[ $this->cookieNames['userlang'] ] ) ) { |
1277 | 1244 | $set = $this->setLang( $_COOKIE[ $this->cookieNames['userlang'] ] ); |
1278 | 1245 | } |
1279 | 1246 | |
1280 | 1247 | if ( !$set ) { |
1281 | | - $acceptableLanguages = self::acceptableLanguages(); |
1282 | | - foreach ( $acceptableLanguages as $lang => $q ) { |
1283 | | - |
1284 | | - if ( $lang == '*' ) { |
1285 | | - /* We choose the first available language which is not in $acceptableLanguages |
1286 | | - * The special * range matches every tag not matched by any other range, languages |
1287 | | - * present in $acceptableLanguages will either have a lower q-value, or be missing |
1288 | | - * from availableLanguages. |
1289 | | - * The order will be the one in the i18n file: en, af, ar... |
| 1248 | + $acceptableLanguages = TsIntuitionUtil::GetAcceptableLanguages(); |
| 1249 | + foreach ( $acceptableLanguages as $acceptLang => $qVal ) { |
| 1250 | + |
| 1251 | + if ( $acceptLang == '*' ) { |
| 1252 | + |
| 1253 | + /** |
| 1254 | + * We pick the first available language which is not in $acceptableLanguages. |
| 1255 | + * The special * range matches every tag not matched by any other range. |
| 1256 | + * Other language codes in $acceptableLanguages will either have a lower q-value, |
| 1257 | + * or be missing from availableLanguages. |
| 1258 | + * The order will be the one in the i18n file: en, af, ar... |
1290 | 1259 | */ |
1291 | | - |
1292 | | - foreach ( $this->availableLanguages as $lang => $true ) { |
1293 | | - if (! isset( $acceptableLanguages[$lang] ) ) { |
1294 | | - $set = $this->setLang( $lang ); |
| 1260 | + |
| 1261 | + foreach ( $this->availableLanguages as $availableLang => $true ) { |
| 1262 | + if ( !isset( $acceptableLanguages[$availableLang] ) ) { |
| 1263 | + $set = $this->setLang( $availableLangf ); |
1295 | 1264 | break; |
1296 | 1265 | } |
1297 | 1266 | } |
1298 | | - if ( $set ) |
| 1267 | + if ( $set ) { |
1299 | 1268 | break; |
1300 | | - } elseif ( isset( $this->availableLanguages[$lang] ) ) { |
1301 | | - $set = $this->setLang( $lang ); |
| 1269 | + } |
| 1270 | + |
| 1271 | + } elseif ( isset( $this->availableLanguages[$acceptLang] ) ) { |
| 1272 | + $set = $this->setLang( $acceptLang ); |
1302 | 1273 | break; |
1303 | 1274 | } |
1304 | 1275 | } |
1305 | 1276 | } |
1306 | | - |
| 1277 | + |
1307 | 1278 | if ( !$set ) { |
1308 | 1279 | $set = $this->setLang( 'en' ); |
1309 | 1280 | } |
| 1281 | + |
1310 | 1282 | return $set; |
1311 | 1283 | } |
1312 | 1284 | |
Index: trunk/tools/ToolserverI18N/public_html/demo/demo6.php |
— | — | @@ -50,7 +50,23 @@ |
51 | 51 | |
52 | 52 | ); |
53 | 53 | |
| 54 | +// GetAcceptableLanguages |
| 55 | +echo "<br />getAcceptableLanguages: (default: \$_SERVER['HTTP_ACCEPT_LANGUAGE']: {$_SERVER['HTTP_ACCEPT_LANGUAGE']}):<br />"; |
| 56 | +var_dump( |
54 | 57 | |
| 58 | + TsIntuitionUtil::getAcceptableLanguages( @$_SERVER['HTTP_ACCEPT_LANGUAGE'] ) |
| 59 | + |
| 60 | +); |
| 61 | + |
| 62 | +$acceptLang = 'nl-be,nl;q=0.7,en-us,en;q=0.3'; |
| 63 | +echo "<br />getAcceptableLanguages: ( '{$acceptLang}' ):<br />"; |
| 64 | +var_dump( |
| 65 | + |
| 66 | + TsIntuitionUtil::getAcceptableLanguages( $acceptLang ) |
| 67 | + |
| 68 | +); |
| 69 | + |
| 70 | + |
55 | 71 | /* View source */ |
56 | 72 | view_source( __FILE__ ); |
57 | 73 | close_demo(); |
\ No newline at end of file |
Index: trunk/tools/ToolserverI18N/TsIntuitionUtil.php |
— | — | @@ -123,4 +123,57 @@ |
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
| 127 | + /** |
| 128 | + * Return a list of acceptable languages from an Accept-Language header |
| 129 | + * @param $rawList String List of language tags, formatted like an |
| 130 | + * HTTP Accept-Language header (optional; defaults to $_SERVER['HTTP_ACCEPT_LANGUAGE']) |
| 131 | + * @return array keyed by language codes with q-values as values. |
| 132 | + */ |
| 133 | + public static function getAcceptableLanguages( $rawList = false ) { |
| 134 | + if ( $rawList === false ) { |
| 135 | + $rawList = @$_SERVER['HTTP_ACCEPT_LANGUAGE']; |
| 136 | + } |
| 137 | + |
| 138 | + $acceptableLanguages = array(); |
| 139 | + |
| 140 | + // Accept-Language: 1#( language-range [ ";" "q" "=" qvalue ] ) |
| 141 | + // Example: "nl-be,nl;q=0.7,en-us,en;q=0.3" |
| 142 | + // The list of elements is separated by comma and optional LWS |
| 143 | + $languages = explode( ',', $rawList ); |
| 144 | + foreach ( $languages as $language ) { |
| 145 | + $language = trim( $language ); // Remove optional LWS |
| 146 | + |
| 147 | + // Extract the language-range and, if present, the q-value |
| 148 | + if ( !preg_match( '/^([A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*|\*)(?:\s*;\s*q\s*=\s*([01](?:\.[0-9]{0,3})?))?$/', $language, $m ) |
| 149 | + ) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * $m is now an array with either two or three values: |
| 155 | + * - array( 'lang-code', 'lang-code' ) |
| 156 | + * - array( 'lang-code;q=val', 'lang-code', 'val' ) |
| 157 | + */ |
| 158 | + |
| 159 | + // We are not interested in the first value. |
| 160 | + array_shift( $m ); |
| 161 | + |
| 162 | + // Default to 1 as q-val |
| 163 | + // @FIXME: In case "nl-be,nl;q=0.7,en-us,en;q=0.3", "en" gets defaulted to '1', |
| 164 | + // it should default to the next q-val (0.3 in this case) |
| 165 | + if ( !isset( $m[1] ) ) { |
| 166 | + $m[1] = '1'; |
| 167 | + } |
| 168 | + |
| 169 | + list( $langCode, $qVal ) = $m; |
| 170 | + |
| 171 | + $acceptableLanguages[$langCode] = $qVal; |
| 172 | + } |
| 173 | + |
| 174 | + // Sort by q value in descending order |
| 175 | + arsort( $acceptableLanguages, SORT_NUMERIC ); |
| 176 | + |
| 177 | + return $acceptableLanguages; |
| 178 | + } |
| 179 | + |
127 | 180 | } |