From 4467d11a468ba6c44e117eea72bdb06453a50d37 Mon Sep 17 00:00:00 2001 From: Platonides Date: Tue, 3 Aug 2010 13:23:31 +0000 Subject: [PATCH] Follow up r70356. Improve WebRequest getAcceptLang(): Add support for q=0 language and the special range "*", always return the language codes in lowercase. --- includes/WebRequest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 144dab480c..c269d19d93 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -730,6 +730,8 @@ class WebRequest { /** * Parse the Accept-Language header sent by the client into an array * @return array( languageCode => q-value ) sorted by q-value in descending order + * May contain the "language" '*', which applies to languages other than those explicitly listed. + * This is aligned with rfc2616 section 14.4 */ public function getAcceptLang() { // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header @@ -738,22 +740,29 @@ class WebRequest { return array(); } + // Return the language codes in lower case + $acceptLang = strtolower( $acceptLang ); + // Break up string into pieces (languages and q factors) $lang_parse = null; - preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i', + preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?|\*)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+)?)?)?/', $acceptLang, $lang_parse ); if ( !count( $lang_parse[1] ) ) { return array(); } + // Create a list like "en" => 0.8 $langs = array_combine( $lang_parse[1], $lang_parse[4] ); // Set default q factor to 1 foreach ( $langs as $lang => $val ) { if ( $val === '' ) { $langs[$lang] = 1; + } else if ( $val == 0 ) { + unset($langs[$lang]); } } + // Sort list arsort( $langs, SORT_NUMERIC ); return $langs; -- 2.20.1