r100234 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100233‎ | r100234 | r100235 >
Date:15:56, 19 October 2011
Author:platonides
Status:ok (Comments)
Tags:
Comment:
Guess the language from Accept-Language header.
Modified paths:
  • /trunk/tools/ToolserverI18N/TsIntuition.php (modified) (history)

Diff [purge]

Index: trunk/tools/ToolserverI18N/TsIntuition.php
@@ -1217,11 +1217,48 @@
12181218 }
12191219
12201220 /**
 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+ /**
12211257 * Check language choice tree in the following order:
12221258 * - First: Construct override
12231259 * - Second: Parameter override
12241260 * - Third: Saved cookie
1225 - * - Fourth: Nothing (default stays)
 1261+ * - Fourth: Preferences from Accept-Language header
 1262+ * - Fifth: English (default stays)
12261263 *
12271264 * @private
12281265 *
@@ -1238,7 +1275,35 @@
12391276 if ( !$set && isset( $_COOKIE[ $this->cookieNames['userlang'] ] ) ) {
12401277 $set = $this->setLang( $_COOKIE[ $this->cookieNames['userlang'] ] );
12411278 }
 1279+
12421280 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...
 1290+ */
 1291+
 1292+ foreach ( $this->availableLanguages as $lang => $true ) {
 1293+ if (! isset( $acceptableLanguages[$lang] ) ) {
 1294+ $set = $lang;
 1295+ break;
 1296+ }
 1297+ }
 1298+ if ( $set )
 1299+ break;
 1300+ } elseif ( isset( $this->availableLanguages[$lang] ) ) {
 1301+ $set = $lang;
 1302+ break;
 1303+ }
 1304+ }
 1305+ }
 1306+
 1307+ if ( !$set ) {
12431308 $set = $this->setLang( 'en' );
12441309 }
12451310 return $set;

Follow-up revisions

RevisionCommit summaryAuthorDate
r102205[TsIntuition] Minor fixes in acceptableLanguages()...krinkle23:02, 6 November 2011

Comments

#Comment by Krinkle (talk | contribs)   23:22, 3 November 2011

Marking fixme for now. Sandbox environment is limited, please add a small demo for this (or I'll add it later). Need to confirm this works on Toolserver in the sandbox checkout first (only way to do that is from within a demo).

Plus having a demo("test") is good practice anyway.

#Comment by Platonides (talk | contribs)   23:48, 3 November 2011

What do you mean by Sandbox? And how do you expect to demo it?

When people with no cookie or url parameter went to http://toolserver.org/~intuition/, he would automatically get it from their browser config.

You can see it in action at eg. http://wolfsbane.toolserver.org/~platonides/wlm/provincias.php (if your first preference isn't English, but another language supported by that tool)

#Comment by Krinkle (talk | contribs)   00:38, 5 November 2011
#Comment by Platonides (talk | contribs)   23:20, 5 November 2011

You don't need to add any code. This works automatically.

#Comment by Krinkle (talk | contribs)   00:42, 5 November 2011
#Comment by Platonides (talk | contribs)   23:28, 5 November 2011

Note I see that in Spanish, with no cookies, so it's working (does that "confirm this works on Toolserver in the sandbox checkout"?).

#Comment by Krinkle (talk | contribs)   21:48, 6 November 2011

Awesome!

Status & tagging log