Allow more than one variant set in user preferences.
authorLiangent <liangent@gmail.com>
Wed, 12 Jun 2013 12:13:04 +0000 (12:13 +0000)
committerTim Starling <tstarling@wikimedia.org>
Fri, 16 Aug 2013 05:46:14 +0000 (05:46 +0000)
Now with the introduction of page language, a site can have pages
in all languages, and different languages have different variants.
This patch allows users to set preferred variants for every page
they may see on the site.

Change-Id: Ie7e82bee0b1f8f902b38bb4a464cf0ebc4df4d89

includes/Preferences.php
includes/User.php
languages/LanguageConverter.php
tests/phpunit/includes/LanguageConverterTest.php

index 709f15c..f5f63ea 100644 (file)
@@ -335,35 +335,44 @@ class Preferences {
 
                // see if there are multiple language variants to choose from
                if ( !$wgDisableLangConversion ) {
-                       $variants = $wgContLang->getVariants();
+                       foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
+                               if ( $langCode == $wgContLang->getCode() ) {
+                                       $variants = $wgContLang->getVariants();
 
-                       if ( count( $variants ) > 1 ) {
-                               $variantArray = array();
-                               foreach ( $variants as $v ) {
-                                       $v = str_replace( '_', '-', strtolower( $v ) );
-                                       $variantArray[$v] = $wgContLang->getVariantname( $v, false );
-                               }
+                                       if ( count( $variants ) <= 1 ) {
+                                               continue;
+                                       }
 
-                               $options = array();
-                               foreach ( $variantArray as $code => $name ) {
-                                       $display = wfBCP47( $code ) . ' - ' . $name;
-                                       $options[$display] = $code;
-                               }
+                                       $variantArray = array();
+                                       foreach ( $variants as $v ) {
+                                               $v = str_replace( '_', '-', strtolower( $v ) );
+                                               $variantArray[$v] = $lang->getVariantname( $v, false );
+                                       }
 
-                               $defaultPreferences['variant'] = array(
-                                       'label-message' => 'yourvariant',
-                                       'type' => 'select',
-                                       'options' => $options,
-                                       'section' => 'personal/i18n',
-                                       'help-message' => 'prefs-help-variant',
-                               );
+                                       $options = array();
+                                       foreach ( $variantArray as $code => $name ) {
+                                               $display = wfBCP47( $code ) . ' - ' . $name;
+                                               $options[$display] = $code;
+                                       }
 
-                               if ( !$wgDisableTitleConversion ) {
-                                       $defaultPreferences['noconvertlink'] =
-                                               array(
-                                               'type' => 'toggle',
+                                       $defaultPreferences['variant'] = array(
+                                               'label-message' => 'yourvariant',
+                                               'type' => 'select',
+                                               'options' => $options,
                                                'section' => 'personal/i18n',
-                                               'label-message' => 'tog-noconvertlink',
+                                               'help-message' => 'prefs-help-variant',
+                                       );
+
+                                       if ( !$wgDisableTitleConversion ) {
+                                               $defaultPreferences['noconvertlink'] = array(
+                                                       'type' => 'toggle',
+                                                       'section' => 'personal/i18n',
+                                                       'label-message' => 'tog-noconvertlink',
+                                               );
+                                       }
+                               } else {
+                                       $defaultPreferences["variant-$langCode"] = array(
+                                               'type' => 'api',
                                        );
                                }
                        }
index fedc6a9..3c0e1f6 100644 (file)
@@ -1240,7 +1240,10 @@ class User {
 
                $defOpt = $wgDefaultUserOptions;
                // Default language setting
-               $defOpt['language'] = $defOpt['variant'] = $wgContLang->getCode();
+               $defOpt['language'] = $wgContLang->getCode();
+               foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
+                       $defOpt[$langCode == $wgContLang->getCode() ? 'variant' : "variant-$langCode"] = $langCode;
+               }
                foreach ( SearchEngine::searchableNamespaces() as $nsnum => $nsname ) {
                        $defOpt['searchNs' . $nsnum] = !empty( $wgNamespacesToBeSearchedDefault[$nsnum] );
                }
index a02fc8a..11d36a8 100644 (file)
@@ -246,7 +246,7 @@ class LanguageConverter {
         * @return Mixed: variant if one found, false otherwise.
         */
        protected function getUserVariant() {
-               global $wgUser;
+               global $wgUser, $wgContLang;
 
                // memoizing this function wreaks havoc on parserTest.php
                /*
@@ -259,7 +259,11 @@ class LanguageConverter {
                // Don't call this on stub objects because that causes infinite
                // recursion during initialisation
                if ( $wgUser->isLoggedIn() ) {
-                       $ret = $wgUser->getOption( 'variant' );
+                       if ( $this->mMainLanguageCode == $wgContLang->getCode() ) {
+                               $ret = $wgUser->getOption( 'variant' );
+                       } else {
+                               $ret = $wgUser->getOption( 'variant-' . $this->mMainLanguageCode );
+                       }
                } else {
                        // figure out user lang without constructing wgLang to avoid
                        // infinite recursion
index 93ce119..070a680 100644 (file)
@@ -75,6 +75,20 @@ class LanguageConverterTest extends MediaWikiLangTestCase {
                $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
        }
 
+       function testGetPreferredVariantUserOptionForForeignLanguage() {
+               global $wgContLang, $wgUser;
+
+               $wgContLang = Language::factory( 'en' );
+               $wgUser = new User;
+               $wgUser->load(); // from 'defaults'
+               $wgUser->mId = 1;
+               $wgUser->mDataLoaded = true;
+               $wgUser->mOptionsLoaded = true;
+               $wgUser->setOption( 'variant-tg', 'tg-latn' );
+
+               $this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
+       }
+
        function testGetPreferredVariantHeaderUserVsUrl() {
                global $wgContLang, $wgRequest, $wgUser;