MediaWiki master
LanguageFallback.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Languages;
22
23use InvalidArgumentException;
25
35 public const MESSAGES = 0;
36
41 public const STRICT = 1;
42
44 private $siteLangCode;
45
47 private $localisationCache;
48
50 private $langNameUtils;
51
53 private $fallbackCache = [];
54
63 public function __construct(
64 $siteLangCode,
65 LocalisationCache $localisationCache,
66 LanguageNameUtils $langNameUtils
67 ) {
68 $this->siteLangCode = $siteLangCode;
69 $this->localisationCache = $localisationCache;
70 $this->langNameUtils = $langNameUtils;
71 }
72
80 public function getFirst( $code ) {
81 return $this->getAll( $code )[0] ?? null;
82 }
83
93 public function getAll( $code, $mode = self::MESSAGES ) {
94 // XXX The LanguageNameUtils dependency is just because of this line, is it needed?
95 // Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should
96 // actually be static?
97 if ( $code === 'en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
98 return [];
99 }
100 switch ( $mode ) {
101 case self::MESSAGES:
102 // For unknown languages, fallbackSequence returns an empty array. Hardcode fallback
103 // to 'en' in that case, as English messages are always defined.
104 $ret = $this->localisationCache->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ];
105 break;
106
107 case self::STRICT:
108 // Use this mode when you don't want to fall back to English unless explicitly
109 // defined, for example, when you have language-variant icons and an international
110 // language-independent fallback.
111 $ret = $this->localisationCache->getItem( $code, 'originalFallbackSequence' );
112 break;
113
114 default:
115 throw new InvalidArgumentException( "Invalid fallback mode \"$mode\"" );
116 }
117
118 return $ret;
119 }
120
129 public function getAllIncludingSiteLanguage( $code ) {
130 // Usually, we will only store a tiny number of fallback chains, so we cache in a member.
131 $cacheKey = "{$code}-{$this->siteLangCode}";
132
133 if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
134 $fallbacks = $this->getAll( $code );
135
136 if ( $code === $this->siteLangCode ) {
137 // Don't bother hitting the localisation cache a second time
138 $siteFallbacks = [ $code ];
139 } else {
140 // Append the site's fallback chain, including the site language itself
141 $siteFallbacks = $this->getAll( $this->siteLangCode );
142 array_unshift( $siteFallbacks, $this->siteLangCode );
143
144 // Eliminate any languages already included in the chain
145 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
146 }
147
148 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
149 }
150 return $this->fallbackCache[$cacheKey];
151 }
152}
Caching for the contents of localisation files.
getAllIncludingSiteLanguage( $code)
Get the ordered list of fallback languages, ending with the fallback language chain for the site lang...
const STRICT
Return a strict fallback chain in getAll.
getFirst( $code)
Get the first fallback for a given language.
const MESSAGES
Return a fallback chain for messages in getAll.
__construct( $siteLangCode, LocalisationCache $localisationCache, LanguageNameUtils $langNameUtils)
Do not call this directly.
getAll( $code, $mode=self::MESSAGES)
Get the ordered list of fallback languages.
A service that provides utilities to do with language names and codes.