wiki.techinc.nl/includes/resourceloader/ResourceLoaderLanguageDataModule.php
C. Scott Ananian 21ead7a98d Ensure LanguageCode::bcp47() returns a valid BCP 47 language code
MediaWiki uses a number of nonstandard codes which do not validate
according to the IANA language subtag registry.  Some of them have
the wrong semantics entirely: MediaWiki's `sr-ec` variant maps to
BCP 47 `sr-EC` which is "Serbian as used in Ethiopia" (!).

Extend LanguageCode::bcp47() to map our nonstandard codes to valid
BCP 47 language codes.  Export the mapping so that it can be used
in JavaScript's corresponding mw.language.bcp47() implementation
as well, and return the standard BCP 47 codes in the siteinfo
API.

Thanks to TheDJ (I10b4473c7e53f027812bbccf26bb47aec15fddfd) and
Fomafix (I93efc190714ba76247d30ba49fc21ae872fc3555) for previous
attempts at this!

Also removed a fixme for the name of 'Twi', dating back to 2004
(f59c3be23b) -- checking
tw.wikipedia.org it certainly appears that the autonym of 'Twi'
is correctly 'Twi'.

Tracking bugs for invalid language codes are T125073 and T145535.
Discussion of zh-XX => zh-HanX-XX mapping is at T198419.

This is a replay of an earlier merged patch,
8380f0173e, which had to be reverted
because it caused regressions in the Babel extension (T199941).

Bug: T34483
Bug: T106367
Bug: T120847
Depends-On: I27a5b8e45b34c6b57c1b612b11548001c88cd483
Change-Id: Iebbc604af21d7f2af9c1f1ab2574cb5f309bf6ed
2018-10-11 01:53:54 -04:00

83 lines
2.5 KiB
PHP

<?php
/**
* ResourceLoader module for populating language specific data.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Santhosh Thottingal
* @author Timo Tijhof
*/
/**
* ResourceLoader module for populating language specific data, such as grammar forms.
*/
class ResourceLoaderLanguageDataModule extends ResourceLoaderFileModule {
protected $targets = [ 'desktop', 'mobile' ];
/**
* Get all the dynamic data for the content language to an array.
*
* @param ResourceLoaderContext $context
* @return array
*/
protected function getData( ResourceLoaderContext $context ) {
$language = Language::factory( $context->getLanguage() );
return [
'digitTransformTable' => $language->digitTransformTable(),
'separatorTransformTable' => $language->separatorTransformTable(),
'minimumGroupingDigits' => $language->minimumGroupingDigits(),
'grammarForms' => $language->getGrammarForms(),
'grammarTransformations' => $language->getGrammarTransformations(),
'pluralRules' => $language->getPluralRules(),
'digitGroupingPattern' => $language->digitGroupingPattern(),
'fallbackLanguages' => $language->getFallbackLanguages(),
'bcp47Map' => LanguageCode::getNonstandardLanguageCodeMapping(),
];
}
/**
* @param ResourceLoaderContext $context
* @return string JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
$fileScript = parent::getScript( $context );
$langDataScript = Xml::encodeJsCall(
'mw.language.setData',
[
$context->getLanguage(),
$this->getData( $context )
],
ResourceLoader::inDebugMode()
);
return $fileScript . $langDataScript;
}
/**
* @return bool
*/
public function enableModuleContentVersion() {
return true;
}
/**
* @return bool
*/
public function supportsURLLoading() {
return false;
}
}