21publicconstMESSAGES = LanguageFallbackMode::MESSAGES;
34private $localisationCache;
37private $langNameUtils;
40private $fallbackCache = [];
55 $this->siteLangCode = $siteLangCode;
56 $this->localisationCache = $localisationCache;
57 $this->langNameUtils = $langNameUtils;
68return $this->
getAll( $code )[0] ??
null;
81publicfunctiongetAll( $code, $mode = LanguageFallbackMode::MESSAGES ) {
82// XXX The LanguageNameUtils dependency is just because of this line, is it needed? 83// Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should 85if ( $code ===
'en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
88if ( is_int( $mode ) ) {
89 $mode = LanguageFallbackMode::from( $mode );
91return match ( $mode ) {
92 LanguageFallbackMode::MESSAGES =>
93// For unknown languages, fallbackSequence returns an empty array. Hardcode fallback 94// to 'en' in that case, as English messages are always defined. 95 $this->localisationCache->getItem( $code,
'fallbackSequence' ) ?: [
'en' ],
98// Use this mode when you don't want to fall back to English unless explicitly 99// defined, for example, when you have language-variant icons and an international 100// language-independent fallback. 101 $this->localisationCache->getItem( $code,
'originalFallbackSequence' ),
114// Usually, we will only store a tiny number of fallback chains, so we cache in a member. 115 $cacheKey =
"{$code}-{$this->siteLangCode}";
117if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
118 $fallbacks = $this->
getAll( $code );
120if ( $code === $this->siteLangCode ) {
121// Don't bother hitting the localisation cache a second time 122 $siteFallbacks = [ $code ];
124// Append the site's fallback chain, including the site language itself 125 $siteFallbacks = $this->
getAll( $this->siteLangCode );
126 array_unshift( $siteFallbacks, $this->siteLangCode );
128// Eliminate any languages already included in the chain 129 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
132 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
134return $this->fallbackCache[$cacheKey];
139class_alias( LanguageFallback::class,
'MediaWiki\\Languages\\LanguageFallback' );
Caching for the contents of localisation files.
getAll( $code, $mode=LanguageFallbackMode::MESSAGES)
Get the ordered list of fallback languages.
getAllIncludingSiteLanguage( $code)
Get the ordered list of fallback languages, ending with the fallback language chain for the site lang...
const MESSAGES
Return a fallback chain for messages in getAll.
getFirst( $code)
Get the first fallback for a given language.
const STRICT
Return a strict fallback chain in getAll.
__construct( $siteLangCode, LocalisationCache $localisationCache, LanguageNameUtils $langNameUtils)
Do not call this directly.
A service that provides utilities to do with language names and codes.
@ STRICT
Return a fallback chain for messages in getAll.