From 396e18a8e572ad350ce501492e8bf15d54ab6138 Mon Sep 17 00:00:00 2001 From: Liangent Date: Fri, 7 Jun 2013 08:50:10 +0000 Subject: [PATCH] New function Language::getParentLanguage(). Change-Id: Ib2109176b7dfc7ec2d0ee827c804cf93ea83b9e5 --- languages/Language.php | 33 ++++++++++++++++++++++-- tests/phpunit/languages/LanguageTest.php | 22 ++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index 92ea75c814..ead72c6bd0 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -81,7 +81,7 @@ class Language { public $mVariants, $mCode, $mLoaded = false; public $mMagicExtensions = array(), $mMagicHookDone = false; - private $mHtmlCode = null; + private $mHtmlCode = null, $mParentLanguage = false; public $dateFormatStrings = array(); public $mExtendedSpecialPageAliases; @@ -3933,6 +3933,34 @@ class Language { return $this; } + /** + * 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() { + if ( $this->mParentLanguage !== false ) { + return $this->mParentLanguage; + } + + $pieces = explode( '-', $this->getCode() ); + $code = $pieces[0]; + if ( !in_array( $code, LanguageConverter::$languagesWithVariants ) ) { + $this->mParentLanguage = null; + return null; + } + $lang = Language::factory( $code ); + if ( !$lang->hasVariant( $this->getCode() ) ) { + $this->mParentLanguage = null; + return null; + } + + $this->mParentLanguage = $lang; + return $lang; + } + /** * Get the RFC 3066 code for this language object * @@ -3967,8 +3995,9 @@ class Language { */ public function setCode( $code ) { $this->mCode = $code; - // Ensure we don't leave an incorrect html code lying around + // Ensure we don't leave incorrect cached data lying around $this->mHtmlCode = null; + $this->mParentLanguage = false; } /** diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index d687dbbd93..4d67f45dd7 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -1481,4 +1481,26 @@ class LanguageTest extends LanguageClassesTestCase { array( 'FI', false, 'is not supported language, input should be in lower case' ), ); } + + /** + * @dataProvider provideGetParentLanguage + */ + function testGetParentLanguage( $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 is the parent language of zh-cn' ), + 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' ), + ); + } } -- 2.20.1