diff --git a/autoload.php b/autoload.php index f96d8986630..0f793238261 100644 --- a/autoload.php +++ b/autoload.php @@ -682,6 +682,7 @@ $wgAutoloadLocalClasses = [ 'LanguageBe_tarask' => __DIR__ . '/languages/classes/LanguageBe_tarask.php', 'LanguageBg' => __DIR__ . '/languages/classes/LanguageBg.php', 'LanguageBs' => __DIR__ . '/languages/classes/LanguageBs.php', + 'LanguageCode' => __DIR__ . '/languages/LanguageCode.php', 'LanguageConverter' => __DIR__ . '/languages/LanguageConverter.php', 'LanguageCu' => __DIR__ . '/languages/classes/LanguageCu.php', 'LanguageDsb' => __DIR__ . '/languages/classes/LanguageDsb.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index a3d68c6bfea..add4bfa12d5 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -2825,8 +2825,9 @@ $wgUsePrivateIPs = false; * MediaWiki out of the box. Not all languages listed there have translations, * see languages/messages/ for the list of languages with some localisation. * - * Warning: Don't use language codes listed in $wgDummyLanguageCodes like "no" - * for Norwegian (use "nb" instead), or things will break unexpectedly. + * Warning: Don't use any of MediaWiki's deprecated language codes listed in + * LanguageCode::getDeprecatedCodeMapping or $wgDummyLanguageCodes, like "no" + * for Norwegian (use "nb" instead). If you do, things will break unexpectedly. * * This defines the default interface language for all users, but users can * change it in their preferences. @@ -2885,25 +2886,22 @@ $wgExtraInterlanguageLinkPrefixes = []; $wgExtraLanguageNames = []; /** - * List of language codes that don't correspond to an actual language. - * These codes are mostly left-offs from renames, or other legacy things. - * This array makes them not appear as a selectable language on the installer, - * and excludes them when running the transstat.php script. + * List of mappings from one language code to another. + * This array makes the codes not appear as a selectable language on the + * installer, and excludes them when running the transstat.php script. + * + * In Setup.php, the variable $wgDummyLanguageCodes is created by combining + * these codes with a list of "deprecated" codes, which are mostly leftovers + * from renames or other legacy things, and the internal codes 'qqq' and 'qqx'. + * If a mapping in $wgExtraLanguageCodes collide with a built-in mapping, the + * value in $wgExtraLanguageCodes will be used. + * + * @since 1.29 */ -$wgDummyLanguageCodes = [ - 'als' => 'gsw', - 'bat-smg' => 'sgs', - 'be-x-old' => 'be-tarask', - 'bh' => 'bho', - 'fiu-vro' => 'vro', - 'no' => 'nb', - 'qqq' => 'qqq', # Used for message documentation. - 'qqx' => 'qqx', # Used for viewing message keys. - 'roa-rup' => 'rup', - 'simple' => 'en', - 'zh-classical' => 'lzh', - 'zh-min-nan' => 'nan', - 'zh-yue' => 'yue', +$wgExtraLanguageCodes = [ + 'bh' => 'bho', // Bihari language family + 'no' => 'nb', // Norwegian language family + 'simple' => 'en', // Simple English ]; /** diff --git a/includes/Setup.php b/includes/Setup.php index b61de733769..e686cd81d6e 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -403,6 +403,14 @@ if ( is_array( $wgExtraNamespaces ) ) { $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces; } +// Merge in the legacy language codes, unless overridden in the config +if ( !isset( $wgDummyLanguageCodes ) ) { + $wgDummyLanguageCodes = [ + 'qqq' => 'qqq', // Used for message documentation + 'qqx' => 'qqx', // Used for viewing message keys + ] + $wgExtraLanguageCodes + LanguageCode::getDeprecatedCodeMapping(); +} + // These are now the same, always // To determine the user language, use $wgLang->getCode() $wgContLanguageCode = $wgLanguageCode; diff --git a/languages/LanguageCode.php b/languages/LanguageCode.php new file mode 100644 index 00000000000..7c9da77ba61 --- /dev/null +++ b/languages/LanguageCode.php @@ -0,0 +1,54 @@ + 'gsw', + 'bat-smg' => 'sgs', + 'be-x-old' => 'be-tarask', + 'fiu-vro' => 'vro', + 'roa-rup' => 'rup', + 'zh-classical' => 'lzh', + 'zh-min-nan' => 'nan', + 'zh-yue' => 'yue', + ]; + } +} diff --git a/tests/phpunit/languages/LanguageCodeTest.php b/tests/phpunit/languages/LanguageCodeTest.php new file mode 100644 index 00000000000..b33360cfe9f --- /dev/null +++ b/tests/phpunit/languages/LanguageCodeTest.php @@ -0,0 +1,40 @@ +assertInstanceOf( LanguageCode::class, $instance ); + } + + public function testGetDeprecatedCodeMapping() { + $map = LanguageCode::getDeprecatedCodeMapping(); + + $this->assertInternalType( 'array', $map ); + $this->assertContainsOnly( 'string', array_keys( $map ) ); + $this->assertArrayNotHasKey( '', $map ); + $this->assertContainsOnly( 'string', $map ); + $this->assertNotContains( '', $map ); + + // Codes special to MediaWiki should never appear in a map of "deprecated" codes + $this->assertArrayNotHasKey( 'qqq', $map, 'documentation' ); + $this->assertNotContains( 'qqq', $map, 'documentation' ); + $this->assertArrayNotHasKey( 'qqx', $map, 'debug code' ); + $this->assertNotContains( 'qqx', $map, 'debug code' ); + + // Valid language codes that are currently not "deprecated" + $this->assertArrayNotHasKey( 'bh', $map, 'family of Bihari languages' ); + $this->assertArrayNotHasKey( 'no', $map, 'family of Norwegian languages' ); + $this->assertArrayNotHasKey( 'simple', $map ); + } + +}