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
// see if there are multiple language variants to choose from
if ( !$wgDisableLangConversion ) {
// 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',
'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',
$defOpt = $wgDefaultUserOptions;
// Default language setting
$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] );
}
foreach ( SearchEngine::searchableNamespaces() as $nsnum => $nsname ) {
$defOpt['searchNs' . $nsnum] = !empty( $wgNamespacesToBeSearchedDefault[$nsnum] );
}
* @return Mixed: variant if one found, false otherwise.
*/
protected function getUserVariant() {
* @return Mixed: variant if one found, false otherwise.
*/
protected function getUserVariant() {
+ global $wgUser, $wgContLang;
// memoizing this function wreaks havoc on parserTest.php
/*
// memoizing this function wreaks havoc on parserTest.php
/*
// Don't call this on stub objects because that causes infinite
// recursion during initialisation
if ( $wgUser->isLoggedIn() ) {
// 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
} else {
// figure out user lang without constructing wgLang to avoid
// infinite recursion
$this->assertEquals( 'tg-latn', $this->lc->getPreferredVariant() );
}
$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;
function testGetPreferredVariantHeaderUserVsUrl() {
global $wgContLang, $wgRequest, $wgUser;