Add converted namespace names as aliases to avoid confusion.

Currently if the site language is zh and a user is using variant zh-tw,
namespace names from zh-hant are displayed because of the language
converter, but they're not accepted by MediaWiki as valid namespace names
by default because zh falls back to zh-hans.

For core namespaces, all converted namespace names are manually added as
$namespaceAliases in MessagesZh.php but it's not always done in extensions.
With this patch converted namespace names are automatically added as
namespace aliases when namespace aliases are loaded.

In some followup commit it makes sense to remove existing core namespace
aliases which were created for this reason.

Change-Id: I01873d9c64a9943afbb655d6203cec9ebd39fb72
This commit is contained in:
Liangent 2013-03-14 19:29:49 +08:00
parent 24ba042648
commit d0e3dc94c3
3 changed files with 42 additions and 4 deletions

View file

@ -682,7 +682,18 @@ class Language {
}
}
$this->namespaceAliases = $aliases;
# Also add converted namespace names as aliases, to avoid confusion.
$convertedNames = array();
foreach ( $this->getVariants() as $variant ) {
if ( $variant === $this->mCode ) {
continue;
}
foreach ( $this->getNamespaces() as $ns => $_ ) {
$convertedNames[$this->getConverter()->convertNamespace( $ns, $variant )] = $ns;
}
}
$this->namespaceAliases = $aliases + $convertedNames;
}
return $this->namespaceAliases;
}

View file

@ -551,7 +551,7 @@ class LanguageConverter {
$variant = $this->getPreferredVariant();
$index = $title->getNamespace();
if ( $index !== NS_MAIN ) {
$text = $this->convertNamespace( $index ) . ':';
$text = $this->convertNamespace( $index, $variant ) . ':';
} else {
$text = '';
}
@ -563,10 +563,13 @@ class LanguageConverter {
* Get the namespace display name in the preferred variant.
*
* @param $index int namespace id
* @param $variant string|null variant code or null for preferred variant
* @return String: namespace name for display
*/
public function convertNamespace( $index ) {
$variant = $this->getPreferredVariant();
public function convertNamespace( $index, $variant = null ) {
if ( $variant === null ) {
$variant = $this->getPreferredVariant();
}
if ( $index === NS_MAIN ) {
return '';
} else {

View file

@ -1504,4 +1504,28 @@ class LanguageTest extends LanguageClassesTestCase {
array( 'en', null, 'en does not have converter. Although FakeConverter handles en -> en conversion but it is useless' ),
);
}
/**
* @dataProvider provideGetNamespaceAliases
*/
function testGetNamespaceAliases( $languageCode, $subset ) {
$language = Language::factory( $languageCode );
$aliases = $language->getNamespaceAliases();
foreach ( $subset as $alias => $nsId ) {
$this->assertEquals( $nsId, $aliases[$alias] );
}
}
function provideGetNamespaceAliases() {
// TODO: Add tests for NS_PROJECT_TALK and GenderNamespaces
return array(
array(
'zh',
array(
'文件' => NS_FILE,
'檔案' => NS_FILE,
),
),
);
}
}