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
94 public function getAll( $code, $mode = self::MESSAGES ) {
95 // XXX The LanguageNameUtils dependency is just because of this line, is it needed?
96 // Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should
97 // actually be static?
98 if ( $code === 'en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
99 return [];
100 }
101 switch ( $mode ) {
102 case self::MESSAGES:
103 // For unknown languages, fallbackSequence returns an empty array. Hardcode fallback
104 // to 'en' in that case, as English messages are always defined.
105 $ret = $this->localisationCache->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ];
106 break;
107
108 case self::STRICT:
109 // Use this mode when you don't want to fall back to English unless explicitly
110 // defined, for example, when you have language-variant icons and an international
111 // language-independent fallback.
112 $ret = $this->localisationCache->getItem( $code, 'originalFallbackSequence' );
113 break;
114
115 default:
116 throw new InvalidArgumentException( "Invalid fallback mode \"$mode\"" );
117 }
118
119 return $ret;
120 }
121
130 public function getAllIncludingSiteLanguage( $code ) {
131 // Usually, we will only store a tiny number of fallback chains, so we cache in a member.
132 $cacheKey = "{$code}-{$this->siteLangCode}";
133
134 if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
135 $fallbacks = $this->getAll( $code );
136
137 if ( $code === $this->siteLangCode ) {
138 // Don't bother hitting the localisation cache a second time
139 $siteFallbacks = [ $code ];
140 } else {
141 // Append the site's fallback chain, including the site language itself
142 $siteFallbacks = $this->getAll( $this->siteLangCode );
143 array_unshift( $siteFallbacks, $this->siteLangCode );
144
145 // Eliminate any languages already included in the chain
146 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
147 }
148
149 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
150 }
151 return $this->fallbackCache[$cacheKey];
152 }
153}
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.