Display converted namespace names in Html::namespaceSelector().
authorLiangent <liangent@gmail.com>
Tue, 29 May 2012 09:56:21 +0000 (17:56 +0800)
committerTim Starling <tstarling@wikimedia.org>
Wed, 10 Oct 2012 05:53:17 +0000 (16:53 +1100)
By the way, the code to get converted titles and namespaces
has been cleaned up.

Change-Id: Ifcbd56c989d83b9d32dfa99e0b2f06d01e17a2bd

includes/Html.php
languages/Language.php
languages/LanguageConverter.php

index f4a3b55..8cb99f5 100644 (file)
@@ -796,10 +796,12 @@ class Html {
                        if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
                                continue;
                        }
-                       if ( $nsId === 0 ) {
+                       if ( $nsId === NS_MAIN ) {
                                // For other namespaces use use the namespace prefix as label, but for
                                // main we don't use "" but the user message descripting it (e.g. "(Main)" or "(Article)")
                                $nsName = wfMessage( 'blanknamespace' )->text();
+                       } elseif ( is_int( $nsId ) ) {
+                               $nsName = $wgContLang->convertNamespace( $nsId );
                        }
                        $optionsHtml[] = Html::element(
                                'option', array(
index d3841c9..c4807a6 100644 (file)
@@ -54,6 +54,7 @@ class FakeConverter {
        function convert( $t ) { return $t; }
        function convertTo( $text, $variant ) { return $text; }
        function convertTitle( $t ) { return $t->getPrefixedText(); }
+       function convertNamespace( $ns ) { return $this->mLang->getFormattedNsText( $ns ); }
        function getVariants() { return array( $this->mLang->getCode() ); }
        function getPreferredVariant() { return $this->mLang->getCode(); }
        function getDefaultVariant() { return $this->mLang->getCode(); }
@@ -3546,6 +3547,16 @@ class Language {
                return $this->mConverter->convertTitle( $title );
        }
 
+       /**
+        * Convert a namespace index to a string in the preferred variant
+        *
+        * @param $ns int
+        * @return string
+        */
+       public function convertNamespace( $ns ) {
+               return $this->mConverter->convertNamespace( $ns );
+       }
+
        /**
         * Check if this is a language with variants
         *
index 82e6359..bf5c217 100644 (file)
@@ -550,24 +550,41 @@ class LanguageConverter {
        public function convertTitle( $title ) {
                $variant = $this->getPreferredVariant();
                $index = $title->getNamespace();
-               if ( $index === NS_MAIN ) {
+               if ( $index !== NS_MAIN ) {
+                       $text = $this->convertNamespace( $index ) . ':';
+               } else {
                        $text = '';
+               }
+               $text .= $this->translate( $title->getText(), $variant );
+               return $text;
+       }
+
+       /**
+        * Get the namespace display name in the preferred variant.
+        *
+        * @param $index int namespace id
+        * @return String: namespace name for display
+        */
+       public function convertNamespace( $index ) {
+               $variant = $this->getPreferredVariant();
+               if ( $index === NS_MAIN ) {
+                       return '';
                } else {
-                       // first let's check if a message has given us a converted name
+                       // First check if a message gives a converted name in the target variant.
+                       $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inLanguage( $variant );
+                       if ( $nsConvMsg->exists() ) {
+                               return $nsConvMsg->plain();
+                       }
+                       // Then check if a message gives a converted name in content language
+                       // which needs extra translation to the target variant.
                        $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inContentLanguage();
                        if ( $nsConvMsg->exists() ) {
-                               $text = $nsConvMsg->plain();
-                       } else {
-                               // the message does not exist, try retrieve it from the current
-                               // variant's namespace names.
-                               $langObj = $this->mLangObj->factory( $variant );
-                               $text = $langObj->getFormattedNsText( $index );
+                               return $this->translate( $nsConvMsg->plain(), $variant );
                        }
-                       $text .= ':';
+                       // No message exists, retrieve it from the target variant's namespace names.
+                       $langObj = $this->mLangObj->factory( $variant );
+                       return $langObj->getFormattedNsText( $index );
                }
-               $text .= $title->getText();
-               $text = $this->translate( $text, $variant );
-               return $text;
        }
 
        /**