MediaWiki
master
Toggle main menu visibility
LanguageFallback.php
Go to the documentation of this file.
1
<?php
6
7
namespace
MediaWiki\Language;
8
13
class
LanguageFallback
{
19
public
const
MESSAGES
= LanguageFallbackMode::MESSAGES;
20
26
public
const
STRICT
=
LanguageFallbackMode::STRICT
;
27
29
private
$fallbackCache = [];
30
39
public
function
__construct
(
40
private
readonly
string
$siteLangCode,
41
private
readonly
LocalisationCache
$localisationCache,
42
private
readonly
LanguageNameUtils
$langNameUtils,
43
) {
44
}
45
53
public
function
getFirst
( $code ) {
54
return
$this->
getAll
( $code )[0] ??
null
;
55
}
56
67
public
function
getAll
( $code, $mode = LanguageFallbackMode::MESSAGES ) {
68
// XXX The LanguageNameUtils dependency is just because of this line, is it needed?
69
// Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should
70
// actually be static?
71
if
( $code ===
'en'
|| !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
72
return
[];
73
}
74
if
( is_int( $mode ) ) {
75
$mode = LanguageFallbackMode::from( $mode );
76
}
77
return
match ( $mode ) {
78
LanguageFallbackMode::MESSAGES =>
79
// For unknown languages, fallbackSequence returns an empty array. Hardcode fallback
80
// to 'en' in that case, as English messages are always defined.
81
$this->localisationCache->getItem( $code,
'fallbackSequence'
) ?: [
'en'
],
82
83
LanguageFallbackMode::STRICT
=>
84
// Use this mode when you don't want to fall back to English unless explicitly
85
// defined, for example, when you have language-variant icons and an international
86
// language-independent fallback.
87
$this->localisationCache->getItem( $code,
'originalFallbackSequence'
),
88
};
89
}
90
99
public
function
getAllIncludingSiteLanguage
( $code ) {
100
// Usually, we will only store a tiny number of fallback chains, so we cache in a member.
101
$cacheKey =
"{$code}-{$this->siteLangCode}"
;
102
103
if
( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
104
$fallbacks = $this->
getAll
( $code );
105
106
if
( $code === $this->siteLangCode ) {
107
// Don't bother hitting the localisation cache a second time
108
$siteFallbacks = [ $code ];
109
}
else
{
110
// Append the site's fallback chain, including the site language itself
111
$siteFallbacks = $this->
getAll
( $this->siteLangCode );
112
array_unshift( $siteFallbacks, $this->siteLangCode );
113
114
// Eliminate any languages already included in the chain
115
$siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
116
}
117
118
$this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
119
}
120
return
$this->fallbackCache[$cacheKey];
121
}
122
}
123
125
class_alias( LanguageFallback::class,
'MediaWiki\\Languages\\LanguageFallback'
);
MediaWiki\Language\LanguageFallback
Definition
LanguageFallback.php:13
MediaWiki\Language\LanguageFallback\getAll
getAll( $code, $mode=LanguageFallbackMode::MESSAGES)
Get the ordered list of fallback languages.
Definition
LanguageFallback.php:67
MediaWiki\Language\LanguageFallback\getAllIncludingSiteLanguage
getAllIncludingSiteLanguage( $code)
Get the ordered list of fallback languages, ending with the fallback language chain for the site lang...
Definition
LanguageFallback.php:99
MediaWiki\Language\LanguageFallback\__construct
__construct(private readonly string $siteLangCode, private readonly LocalisationCache $localisationCache, private readonly LanguageNameUtils $langNameUtils,)
Do not call this directly.
Definition
LanguageFallback.php:39
MediaWiki\Language\LanguageFallback\MESSAGES
const MESSAGES
Return a fallback chain for messages in getAll.
Definition
LanguageFallback.php:19
MediaWiki\Language\LanguageFallback\getFirst
getFirst( $code)
Get the first fallback for a given language.
Definition
LanguageFallback.php:53
MediaWiki\Language\LanguageFallback\STRICT
const STRICT
Return a strict fallback chain in getAll.
Definition
LanguageFallback.php:26
MediaWiki\Language\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition
LanguageNameUtils.php:27
MediaWiki\Language\LocalisationCache
Caching for the contents of localisation files.
Definition
LocalisationCache.php:47
MediaWiki\Language\STRICT
@ STRICT
Return a fallback chain for messages in getAll.
Definition
LanguageFallbackMode.php:23
includes
Language
LanguageFallback.php
Generated on
for MediaWiki by
1.18.0