36 use Wikimedia\Assert\Assert;
106 if ( $code ===
'qqq' ) {
124 Assert::parameterType(
'string', $code,
'$code' );
125 if ( !isset( $this->validCodeCache[$code] ) ) {
129 $this->validCodeCache[$code] =
131 strcspn( $code,
":/\\\000&<>'\"" ) === strlen( $code ) &&
134 return $this->validCodeCache[$code];
145 Assert::parameterType(
'string', $code,
'$code' );
147 return (
bool)preg_match(
'/^[a-z0-9-]{2,}$/', $code );
164 if ( isset( Data\Names::$names[$tag] ) || $this->
getLanguageName( $tag, $tag ) !==
'' ) {
182 public function getLanguageNames( $inLanguage = self::AUTONYMS, $include = self::DEFINED ) {
183 $cacheKey = $inLanguage === self::AUTONYMS ?
'null' : $inLanguage;
184 $cacheKey .=
":$include";
185 if ( !$this->languageNameCache ) {
186 $this->languageNameCache =
new HashBagOStuff( [
'maxKeys' => 20 ] );
189 $ret = $this->languageNameCache->get( $cacheKey );
192 $this->languageNameCache->set( $cacheKey, $ret );
205 if ( $inLanguage !== self::AUTONYMS && !$this->
isValidCode( $inLanguage ) ) {
211 if ( $inLanguage !== self::AUTONYMS ) {
212 # TODO: also include for self::AUTONYMS, when this code is more efficient
213 Hooks::run(
'LanguageGetTranslatedLanguageNames', [ &$names, $inLanguage ] );
217 if ( $this->options->get(
'UsePigLatinVariant' ) ) {
219 $mwNames[
'en-x-piglatin'] =
'Igpay Atinlay';
222 foreach ( $mwNames as $mwCode => $mwName ) {
223 # - Prefer own MediaWiki native name when not using the hook
224 # - For other names just add if not added through the hook
225 if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) {
226 $names[$mwCode] = $mwName;
230 if ( $include === self::ALL ) {
236 $coreCodes = array_keys( $mwNames );
237 foreach ( $coreCodes as $coreCode ) {
238 $returnMw[$coreCode] = $names[$coreCode];
241 if ( $include === self::SUPPORTED ) {
243 # We do this using a foreach over the codes instead of a directory loop so that messages
244 # files in extensions will work correctly.
245 foreach ( $returnMw as $code => $value ) {
249 $namesMwFile[$code] = $names[$code];
253 ksort( $namesMwFile );
258 # self::DEFINED option; default if it's not one of the other two options
259 # (self::ALL/self::SUPPORTED)
272 public function getLanguageName( $code, $inLanguage = self::AUTONYMS, $include = self::ALL ) {
273 $code = strtolower( $code );
275 return $array[$code] ??
'';
288 throw new MWException(
"Invalid language code \"$code\"" );
291 return $prefix . str_replace(
'-',
'_', ucfirst( $code ) ) . $suffix;
300 $file = $this->
getFileName(
"$IP/languages/messages/Messages", $code,
'.php' );
314 throw new MWException(
"Invalid language code \"$code\"" );
317 return "$IP/languages/i18n/$code.json";