Liangent has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/67399


Change subject: New function Language::getParentLanguage().
......................................................................

New function Language::getParentLanguage().

Change-Id: Ib2109176b7dfc7ec2d0ee827c804cf93ea83b9e5
---
M languages/Language.php
M tests/phpunit/languages/LanguageTest.php
2 files changed, 41 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/99/67399/1

diff --git a/languages/Language.php b/languages/Language.php
index ea34363..cd46ae5 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -3931,6 +3931,25 @@
        }
 
        /**
+        * Get the "parent" language which has a converter to convert a 
"compatible" language
+        * (in another variant) to this language (eg. zh for zh-cn, but not en 
for en-gb).
+        *
+        * @return Language|null
+        * @since 1.22
+        */
+       public function getParentLanguage() {
+               $code = explode( '-', $this->getCode() )[0];
+               if ( !in_array( $code, 
LanguageConverter::$languagesWithVariants ) ) {
+                       return null;
+               }
+               $lang = Language::factory( $code );
+               if ( !in_array( $this->getCode(), $lang->getVariants() ) ) {
+                       return null;
+               }
+               return $lang;
+       }
+
+       /**
         * Get the RFC 3066 code for this language object
         *
         * NOTE: The return value of this function is NOT HTML-safe and must be 
escaped with
diff --git a/tests/phpunit/languages/LanguageTest.php 
b/tests/phpunit/languages/LanguageTest.php
index d687dbb..995b242 100644
--- a/tests/phpunit/languages/LanguageTest.php
+++ b/tests/phpunit/languages/LanguageTest.php
@@ -1481,4 +1481,26 @@
                        array( 'FI', false, 'is not supported language, input 
should be in lower case' ),
                );
        }
+
+       /**
+        * @dataProvider provideGetParentLanguage
+        */
+       function testIsSupportedLanguage( $code, $expected, $comment ) {
+               $lang = Language::factory( $code );
+               if ( is_null( $expected ) ) {
+                       $this->assertNull( $lang->getParentLanguage(), $comment 
);
+               } else {
+                       $this->assertEquals( $expected, 
$lang->getParentLanguage()->getCode(), $comment );
+               }
+       }
+
+       public static function provideGetParentLanguage() {
+               return array(
+                       array( 'zh-cn', 'zh', 'zh-cn is the parent language of 
zh' ),
+                       array( 'zh', 'zh', 'zh is defined as the parent 
language of zh, because zh converter can convert zh-cn to zh' ),
+                       array( 'zh-invalid', null, 'do not be fooled by 
arbitrarily composed language codes' ),
+                       array( 'en-gb', null, 'en does not have converter' ),
+                       array( 'en', null, 'en does not have converter. 
Although FakeConverter handles en -> en conversion but it is useless' ),
+               );
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/67399
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib2109176b7dfc7ec2d0ee827c804cf93ea83b9e5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Liangent <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to