1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Santhosh Thottingal
20 */
21
22use MediaWiki\MediaWikiServices;
23
24/**
25 * Module for populating language specific data, such as grammar forms.
26 *
27 * @ingroup ResourceLoader
28 * @internal
29 */
30class ResourceLoaderLanguageDataModule extends ResourceLoaderFileModule {
31	protected $targets = [ 'desktop', 'mobile' ];
32
33	/**
34	 * Get all the dynamic data for the content language to an array.
35	 *
36	 * @internal Only public for use by GenerateJqueryMsgData (tests)
37	 * @param string $langCode
38	 * @return array
39	 */
40	public static function getData( $langCode ) : array {
41		$language = MediaWikiServices::getInstance()->getLanguageFactory()
42			->getLanguage( $langCode );
43		return [
44			'digitTransformTable' => $language->digitTransformTable(),
45			'separatorTransformTable' => $language->separatorTransformTable(),
46			'minimumGroupingDigits' => $language->minimumGroupingDigits(),
47			'grammarForms' => $language->getGrammarForms(),
48			'grammarTransformations' => $language->getGrammarTransformations(),
49			'pluralRules' => $language->getPluralRules(),
50			'digitGroupingPattern' => $language->digitGroupingPattern(),
51			'fallbackLanguages' => $language->getFallbackLanguages(),
52			'bcp47Map' => LanguageCode::getNonstandardLanguageCodeMapping(),
53		];
54	}
55
56	/**
57	 * @param ResourceLoaderContext $context
58	 * @return string JavaScript code
59	 */
60	public function getScript( ResourceLoaderContext $context ) {
61		return parent::getScript( $context )
62			. 'mw.language.setData('
63			. $context->encodeJson( $context->getLanguage() ) . ','
64			. $context->encodeJson( self::getData( $context->getLanguage() ) )
65			. ');';
66	}
67
68	/**
69	 * @return bool
70	 */
71	public function enableModuleContentVersion() {
72		return true;
73	}
74
75	/**
76	 * @return bool
77	 */
78	public function supportsURLLoading() {
79		return false;
80	}
81}
82