MediaWiki master
LanguageFallback.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Language;
8
19 public const MESSAGES = LanguageFallbackMode::MESSAGES;
20
27
29 private $siteLangCode;
30
32 private $localisationCache;
33
35 private $langNameUtils;
36
38 private $fallbackCache = [];
39
48 public function __construct(
49 $siteLangCode,
50 LocalisationCache $localisationCache,
51 LanguageNameUtils $langNameUtils
52 ) {
53 $this->siteLangCode = $siteLangCode;
54 $this->localisationCache = $localisationCache;
55 $this->langNameUtils = $langNameUtils;
56 }
57
65 public function getFirst( $code ) {
66 return $this->getAll( $code )[0] ?? null;
67 }
68
79 public function getAll( $code, $mode = LanguageFallbackMode::MESSAGES ) {
80 // XXX The LanguageNameUtils dependency is just because of this line, is it needed?
81 // Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should
82 // actually be static?
83 if ( $code === 'en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
84 return [];
85 }
86 if ( is_int( $mode ) ) {
87 $mode = LanguageFallbackMode::from( $mode );
88 }
89 return match ( $mode ) {
90 LanguageFallbackMode::MESSAGES =>
91 // For unknown languages, fallbackSequence returns an empty array. Hardcode fallback
92 // to 'en' in that case, as English messages are always defined.
93 $this->localisationCache->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ],
94
96 // Use this mode when you don't want to fall back to English unless explicitly
97 // defined, for example, when you have language-variant icons and an international
98 // language-independent fallback.
99 $this->localisationCache->getItem( $code, 'originalFallbackSequence' ),
100 };
101 }
102
111 public function getAllIncludingSiteLanguage( $code ) {
112 // Usually, we will only store a tiny number of fallback chains, so we cache in a member.
113 $cacheKey = "{$code}-{$this->siteLangCode}";
114
115 if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
116 $fallbacks = $this->getAll( $code );
117
118 if ( $code === $this->siteLangCode ) {
119 // Don't bother hitting the localisation cache a second time
120 $siteFallbacks = [ $code ];
121 } else {
122 // Append the site's fallback chain, including the site language itself
123 $siteFallbacks = $this->getAll( $this->siteLangCode );
124 array_unshift( $siteFallbacks, $this->siteLangCode );
125
126 // Eliminate any languages already included in the chain
127 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
128 }
129
130 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
131 }
132 return $this->fallbackCache[$cacheKey];
133 }
134}
135
137class_alias( LanguageFallback::class, 'MediaWiki\\Languages\\LanguageFallback' );
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.
Caching for the contents of localisation files.
@ STRICT
Return a fallback chain for messages in getAll.