Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ULSJsonMessageLoader | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
getFilenames | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
getMessages | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
loadI18nFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Json formatted MessageLoader for ULS |
4 | * |
5 | * Copyright (C) 2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, |
6 | * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other |
7 | * contributors. See CREDITS for a list. |
8 | * |
9 | * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't |
10 | * have to do anything special to choose one license or the other and you don't |
11 | * have to notify anyone which license you are using. You are free to use |
12 | * UniversalLanguageSelector in commercial projects as long as the copyright |
13 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. |
14 | * |
15 | * @file |
16 | * @ingroup Extensions |
17 | * @license GPL-2.0-or-later |
18 | * @license MIT |
19 | * @since 2013.11 |
20 | */ |
21 | |
22 | namespace UniversalLanguageSelector; |
23 | |
24 | use MediaWiki\MediaWikiServices; |
25 | |
26 | class ULSJsonMessageLoader { |
27 | /** |
28 | * Returns all message files that are used to load messages for the given |
29 | * language. |
30 | * @param string $language Language code. |
31 | * @return string[] |
32 | */ |
33 | public static function getFilenames( string $language ) { |
34 | $filenames = []; |
35 | |
36 | $languages = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $language ); |
37 | // Prepend the requested language code |
38 | // to load them all in one loop |
39 | array_unshift( $languages, $language ); |
40 | |
41 | // jQuery.uls localization |
42 | foreach ( $languages as $language ) { |
43 | $filenames[] = __DIR__ . "/../lib/jquery.uls/i18n/$language.json"; |
44 | } |
45 | |
46 | // mediaWiki.uls localization |
47 | foreach ( $languages as $language ) { |
48 | $filenames[] = __DIR__ . "/../i18n/$language.json"; |
49 | } |
50 | |
51 | $filenames = array_filter( $filenames, 'file_exists' ); |
52 | |
53 | return $filenames; |
54 | } |
55 | |
56 | /** |
57 | * Get messages for the given language. |
58 | * @param string $language Language code. |
59 | * @return array |
60 | */ |
61 | public static function getMessages( string $language ) { |
62 | $contents = []; |
63 | |
64 | foreach ( self::getFilenames( $language ) as $filename ) { |
65 | $contents += self::loadI18nFile( $filename ); |
66 | } |
67 | |
68 | return $contents; |
69 | } |
70 | |
71 | /** |
72 | * Load messages from a json file. |
73 | * @param string $filename Directory of the json file. |
74 | * @return array |
75 | */ |
76 | protected static function loadI18nFile( $filename ) { |
77 | $contents = file_get_contents( $filename ); |
78 | |
79 | return json_decode( $contents, true ); |
80 | } |
81 | } |