summaryrefslogtreecommitdiff
blob: 465a57859800e1648befcba4d1b47da09f992fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
 * ResourceLoader module for client-side loading of json-based localization.
 *
 * @file
 * @ingroup Extensions
 * @author Santhosh Thottingal
 */

/**
 * ResourceLoader module for client-side loading of json-based localization.
 */
class ResourceLoaderULSJsonMessageModule extends ResourceLoaderModule {
	/**
	 * Part of the ResourceLoader module interface.
	 * Declares the core ext.uls.i18n module as a dependency.
	 * @param ResourceLoaderContext|null $context
	 * @return string[] Module names.
	 */
	public function getDependencies( ResourceLoaderContext $context = null ) {
		return [ 'ext.uls.i18n' ];
	}

	/**
	 * Get supported mobile targets
	 * @return string[] supported targets
	 */
	public function getTargets() {
		return [ 'desktop', 'mobile' ];
	}

	/**
	 * @param ResourceLoaderContext $context
	 * @return array
	 */
	public function getDefinitionSummary( ResourceLoaderContext $context ) {
		$code = $context->getLanguage();
		if ( !Language::isValidCode( $code ) ) {
			$code = 'en';
		}
		$fileHashes = array_map(
			[ __CLASS__, 'safeFileHash' ],
			ULSJsonMessageLoader::getFilenames( $code )
		);

		$summary = parent::getDefinitionSummary( $context );
		$summary[] = [
			'fileHashes' => $fileHashes
		];
		return $summary;
	}

	/**
	 * Get the message strings for the current UI language. Uses
	 * mw.uls.loadLocalization to register them on the frontend.
	 * @param ResourceLoaderContext $context
	 * @return string JavaScript code.
	 */
	public function getScript( ResourceLoaderContext $context ) {
		$code = $context->getLanguage();
		if ( !Language::isValidCode( $code ) ) {
			$code = 'en';
		}

		$params = [ $code, ULSJsonMessageLoader::getMessages( $code ) ];

		return Xml::encodeJsCall( 'mw.uls.loadLocalization', $params );
	}
}