Partly disable title conversion if variant == main language code
authorLiangent <liangent@gmail.com>
Wed, 15 Aug 2012 00:35:04 +0000 (08:35 +0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 3 May 2013 06:16:19 +0000 (06:16 +0000)
Currently the main language code means "no conversion",
but may technically be used as a standalone variant (and
usually falls back to another variant). However for the
page title, since there can be a nice unconverted version
from real page name, it seems better to provide people who
choose "no conversion" with this unconverted version, rather
than a fallback from a variant which is always converted.

If a string targeting exactly the main language code exists
(ie. no fallbacks needed), it's still used. Because the
magic word DISPLAYTITLE cannot be used together with
language conversion syntax and people must use -{T| }- to
show a modified display title for "no conversion" version.

Change-Id: I25cf5a94f6e47f85a9d0b80cc1c9c9f957288478

languages/LanguageConverter.php
tests/parser/parserTests.txt

index d9227d7..4969a7c 100644 (file)
@@ -1357,6 +1357,37 @@ class ConverterRule {
                }
        }
 
+       /**
+        * Similar to getRuleConvertedStr(), but this prefers to use original
+        * page title if $variant === $this->mConverter->mMainLanguageCode
+        * and may return false in this case (so this title conversion rule
+        * will be ignored and the original title is shown).
+        *
+        * @since 1.20
+        * @param $variant The variant code to display page title in
+        * @return String|false The converted title or false if just page name
+        */
+       function getRuleConvertedTitle( $variant ) {
+               if ( $variant === $this->mConverter->mMainLanguageCode ) {
+                       // If a string targeting exactly this variant is set,
+                       // use it. Otherwise, just return false, so the real
+                       // page name can be shown (and because variant === main,
+                       // there'll be no further automatic conversion).
+                       $disp = $this->getTextInBidtable( $variant );
+                       if ( $disp ) {
+                               return $disp;
+                       }
+                       if ( array_key_exists( $variant, $this->mUnidtable ) ) {
+                               $disp = array_values( $this->mUnidtable[$variant] );
+                               $disp = $disp[0];
+                       }
+                       // Assigned above or still false.
+                       return $disp;
+               } else {
+                       return $this->getRuleConvertedStr( $variant );
+               }
+       }
+
        /**
         * Generate conversion table for all text.
         * @private
@@ -1509,7 +1540,7 @@ class ConverterRule {
                                        $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
                                        break;
                                case 'T':
-                                       $this->mRuleTitle = $this->getRuleConvertedStr( $variant );
+                                       $this->mRuleTitle = $this->getRuleConvertedTitle( $variant );
                                        $this->mRuleDisplay = '';
                                        break;
                                default:
index f38d826..a4e32ab 100644 (file)
@@ -12130,6 +12130,30 @@ This won't take interferes with the title rule.
 </p>
 !! end
 
+!! test
+Partly disable title conversion if variant == main language code
+!! options
+language=zh variant=zh title=[[ZH]] showtitle
+!! input
+-{T|zh-cn:CN;zh-tw:TW}-
+!! result
+ZH
+<p>
+</p>
+!! end
+
+!! test
+Partly disable title conversion if variant == main language code, more
+!! options
+language=zh variant=zh title=[[ZH]] showtitle
+!! input
+-{T|TW}-
+!! result
+ZH
+<p>
+</p>
+!! end
+
 !! test
 Raw output of variant escape tags (R flag)
 !! options