Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
34.78% |
16 / 46 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
LanguageNames | |
35.56% |
16 / 45 |
|
0.00% |
0 / 2 |
104.72 | |
0.00% |
0 / 1 |
getNames | |
47.83% |
11 / 23 |
|
0.00% |
0 / 1 |
28.18 | |||
loadLanguage | |
22.73% |
5 / 22 |
|
0.00% |
0 / 1 |
29.61 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Languages\LanguageNameUtils; |
7 | use MediaWiki\MediaWikiServices; |
8 | |
9 | /** |
10 | * A class for querying translated language names from CLDR data. |
11 | * |
12 | * @author Niklas Laxström |
13 | * @author Ryan Kaldari |
14 | * @copyright Copyright © 2007-2011 |
15 | * @license GPL-2.0-or-later |
16 | */ |
17 | class LanguageNames { |
18 | |
19 | private static $cache = []; |
20 | |
21 | /** |
22 | * Missing entries fallback to native name |
23 | */ |
24 | public const FALLBACK_NATIVE = 0; |
25 | /** |
26 | * Missing entries fallback through the fallback chain |
27 | */ |
28 | public const FALLBACK_NORMAL = 1; |
29 | /** |
30 | * Only names that have localisation in MediaWiki |
31 | */ |
32 | public const LIST_MW_SUPPORTED = 0; |
33 | /** |
34 | * All names that are in Names.php |
35 | */ |
36 | public const LIST_MW = 1; |
37 | /** |
38 | * Combination of Names.php and what is in cldr |
39 | */ |
40 | public const LIST_MW_AND_CLDR = 2; |
41 | |
42 | /** |
43 | * Get localized language names for a particular language, using fallback languages for missing |
44 | * items. |
45 | * |
46 | * @param string $code |
47 | * @param int $fbMethod |
48 | * @param int $list |
49 | * @return array an associative array of language codes and localized language names |
50 | */ |
51 | public static function getNames( $code, $fbMethod = self::FALLBACK_NATIVE, |
52 | $list = self::LIST_MW |
53 | ) { |
54 | $xx = self::loadLanguage( $code ); |
55 | |
56 | $native = MediaWikiServices::getInstance()->getLanguageNameUtils() |
57 | ->getLanguageNames( |
58 | LanguageNameUtils::AUTONYMS, |
59 | $list === self::LIST_MW_SUPPORTED ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED |
60 | ); |
61 | |
62 | if ( $fbMethod === self::FALLBACK_NATIVE ) { |
63 | $names = array_merge( $native, $xx ); |
64 | } elseif ( $fbMethod === self::FALLBACK_NORMAL ) { |
65 | // Load missing language names from fallback languages |
66 | $fb = $xx; |
67 | |
68 | $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
69 | foreach ( $fallbacks as $fallback ) { |
70 | // Overwrite the things in fallback with what we have already |
71 | $fb = array_merge( self::loadLanguage( $fallback ), $fb ); |
72 | } |
73 | |
74 | /* Add native names for codes that are not in cldr */ |
75 | $names = array_merge( $native, $fb ); |
76 | |
77 | /* As a last resort, try the native name in Names.php */ |
78 | if ( !isset( $names[$code] ) && isset( $native[$code] ) ) { |
79 | $names[$code] = $native[$code]; |
80 | } |
81 | } else { |
82 | throw new InvalidArgumentException( "Invalid value for 2:\$fallback in " . __METHOD__ ); |
83 | } |
84 | |
85 | switch ( $list ) { |
86 | case self::LIST_MW: |
87 | /** @noinspection PhpMissingBreakStatementInspection */ |
88 | case self::LIST_MW_SUPPORTED: |
89 | /* Remove entries that are not in fb */ |
90 | $names = array_intersect_key( $names, $native ); |
91 | /* And fall to the return */ |
92 | case self::LIST_MW_AND_CLDR: |
93 | return $names; |
94 | default: |
95 | throw new InvalidArgumentException( "Invalid value for 3:\$list in " . __METHOD__ ); |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * Load currency names localized for a particular language. Helper function for getNames. |
101 | * |
102 | * @param string $code The language to return the list in |
103 | * @return array an associative array of language codes and localized language names |
104 | */ |
105 | private static function loadLanguage( $code ) { |
106 | if ( isset( self::$cache[$code] ) ) { |
107 | return self::$cache[$code]; |
108 | } |
109 | |
110 | self::$cache[$code] = []; |
111 | |
112 | $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils(); |
113 | |
114 | if ( !$langNameUtils->isValidBuiltInCode( $code ) ) { |
115 | return []; |
116 | } |
117 | |
118 | /* Load override for wrong or missing entries in cldr */ |
119 | $override = __DIR__ . '/../LocalNames/' . |
120 | $langNameUtils->getFileName( 'LocalNames', $code, '.php' ); |
121 | if ( file_exists( $override ) ) { |
122 | $languageNames = false; |
123 | require $override; |
124 | // @phan-suppress-next-line PhanImpossibleCondition |
125 | if ( is_array( $languageNames ) ) { |
126 | self::$cache[$code] = $languageNames; |
127 | } |
128 | } |
129 | |
130 | $filename = __DIR__ . '/../CldrNames/' . |
131 | $langNameUtils->getFileName( 'CldrNames', $code, '.php' ); |
132 | if ( file_exists( $filename ) ) { |
133 | $languageNames = false; |
134 | require $filename; |
135 | // @phan-suppress-next-line PhanImpossibleCondition |
136 | if ( is_array( $languageNames ) ) { |
137 | self::$cache[$code] += $languageNames; |
138 | } |
139 | } else { |
140 | wfDebug( __METHOD__ . ": Unable to load language names for $filename\n" ); |
141 | } |
142 | |
143 | return self::$cache[$code]; |
144 | } |
145 | } |
146 | |
147 | class_alias( LanguageNames::class, 'LanguageNames' ); |