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 $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
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
125class_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...
__construct(private readonly string $siteLangCode, private readonly LocalisationCache $localisationCache, private readonly LanguageNameUtils $langNameUtils,)
Do not call this directly.
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.
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.