public $mVariants, $mCode, $mLoaded = false;
public $mMagicExtensions = array(), $mMagicHookDone = false;
- private $mHtmlCode = null;
+ private $mHtmlCode = null, $mParentLanguage = false;
public $dateFormatStrings = array();
public $mExtendedSpecialPageAliases;
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
*
*/
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;
}
/**
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' ),
+ );
+ }
}