23use InvalidArgumentException;
25use Wikimedia\Assert\Assert;
45 private $siteLangCode;
48 private $localisationCache;
51 private $langNameUtils;
54 private $fallbackCache = [];
69 $this->siteLangCode = $siteLangCode;
70 $this->localisationCache = $localisationCache;
71 $this->langNameUtils = $langNameUtils;
82 return $this->
getAll( $code )[0] ??
null;
95 public function getAll( $code, $mode = self::MESSAGES ) {
99 if ( $code ===
'en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
106 $ret = $this->localisationCache->getItem( $code,
'fallbackSequence' ) ?: [
'en' ];
113 $ret = $this->localisationCache->getItem( $code,
'originalFallbackSequence' );
117 throw new InvalidArgumentException(
"Invalid fallback mode \"$mode\"" );
120 foreach ( $ret as $fallbackCode ) {
121 Assert::postcondition( $this->langNameUtils->isValidBuiltInCode( $fallbackCode ),
122 "Invalid fallback code '$fallbackCode' in fallback sequence for '$code'" );
138 $cacheKey =
"{$code}-{$this->siteLangCode}";
140 if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
141 $fallbacks = $this->
getAll( $code );
143 if ( $code === $this->siteLangCode ) {
145 $siteFallbacks = [ $code ];
148 $siteFallbacks = $this->
getAll( $this->siteLangCode );
149 array_unshift( $siteFallbacks, $this->siteLangCode );
152 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
155 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
157 return $this->fallbackCache[$cacheKey];
Caching for the contents of localisation files.