Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
29.63% |
8 / 27 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CurrencyNames | |
29.63% |
8 / 27 |
|
0.00% |
0 / 2 |
44.85 | |
0.00% |
0 / 1 |
getNames | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
loadLanguage | |
18.18% |
4 / 22 |
|
0.00% |
0 / 1 |
43.05 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | /** |
8 | * A class for querying translated currency names from CLDR data. |
9 | * |
10 | * @author Niklas Laxström |
11 | * @author Ryan Kaldari |
12 | * @copyright Copyright © 2007-2012 |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class CurrencyNames { |
16 | |
17 | /** @var array */ |
18 | private static $cache = []; |
19 | |
20 | /** |
21 | * Get localized currency names for a particular language, using fallback languages for missing |
22 | * items. |
23 | * |
24 | * @param string $code The language to return the list in |
25 | * @return array an associative array of currency codes and localized currency names |
26 | */ |
27 | public static function getNames( $code ) { |
28 | // Load currency names localized for the requested language |
29 | $names = self::loadLanguage( $code ); |
30 | |
31 | // Load missing currency names from fallback languages |
32 | $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
33 | foreach ( $fallbacks as $fallback ) { |
34 | // Overwrite the things in fallback with what we have already |
35 | $names = array_merge( self::loadLanguage( $fallback ), $names ); |
36 | } |
37 | |
38 | return $names; |
39 | } |
40 | |
41 | /** |
42 | * Load currency names localized for a particular language. Helper function for getNames. |
43 | * |
44 | * @param string $code The language to return the list in |
45 | * @return array an associative array of currency codes and localized currency names |
46 | */ |
47 | private static function loadLanguage( $code ) { |
48 | if ( !isset( self::$cache[$code] ) ) { |
49 | |
50 | $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils(); |
51 | |
52 | if ( !$langNameUtils->isValidBuiltInCode( $code ) ) { |
53 | return []; |
54 | } |
55 | |
56 | /* Load override for wrong or missing entries in cldr */ |
57 | $override = __DIR__ . '/../LocalNames/' . |
58 | $langNameUtils->getFileName( 'LocalNames', $code, '.php' ); |
59 | if ( file_exists( $override ) ) { |
60 | $currencyNames = false; |
61 | require $override; |
62 | // @phan-suppress-next-line PhanImpossibleCondition |
63 | if ( is_array( $currencyNames ) ) { |
64 | self::$cache[$code] = $currencyNames; |
65 | } |
66 | } |
67 | |
68 | $filename = __DIR__ . '/../CldrMain/' . |
69 | $langNameUtils->getFileName( 'CldrMain', $code, '.php' ); |
70 | if ( file_exists( $filename ) ) { |
71 | $currencyNames = false; |
72 | require $filename; |
73 | // @phan-suppress-next-line PhanImpossibleCondition |
74 | if ( is_array( $currencyNames ) ) { |
75 | if ( isset( self::$cache[$code] ) ) { |
76 | // Add to existing list of localized currency names |
77 | self::$cache[$code] += $currencyNames; |
78 | } else { |
79 | // No list exists, so create it |
80 | self::$cache[$code] = $currencyNames; |
81 | } |
82 | } |
83 | } else { |
84 | wfDebug( __METHOD__ . ": Unable to load currency names for $filename\n" ); |
85 | } |
86 | } |
87 | |
88 | return self::$cache[$code] ?? []; |
89 | } |
90 | } |