Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
LanguageFallback.php
Go to the documentation of this file.
1<?php
7namespaceMediaWiki\Language;
8
9useLocalisationCache;
10
15classLanguageFallback {
21publicconstMESSAGES = LanguageFallbackMode::MESSAGES;
22
28publicconstSTRICT =LanguageFallbackMode::STRICT;
29
31private $siteLangCode;
32
34private $localisationCache;
35
37private $langNameUtils;
38
40private $fallbackCache = [];
41
50publicfunction__construct(
51 $siteLangCode,
52LocalisationCache $localisationCache,
53LanguageNameUtils $langNameUtils
54 ) {
55 $this->siteLangCode = $siteLangCode;
56 $this->localisationCache = $localisationCache;
57 $this->langNameUtils = $langNameUtils;
58 }
59
67publicfunctiongetFirst( $code ) {
68return $this->getAll( $code )[0] ??null;
69 }
70
81publicfunctiongetAll( $code, $mode = LanguageFallbackMode::MESSAGES ) {
82// XXX The LanguageNameUtils dependency is just because of this line, is it needed?
83// Especially because isValidBuiltInCode() is just a one-line regex anyway, maybe it should
84// actually be static?
85if ( $code ==='en' || !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
86return [];
87 }
88if ( is_int( $mode ) ) {
89 $mode = LanguageFallbackMode::from( $mode );
90 }
91return match ( $mode ) {
92 LanguageFallbackMode::MESSAGES =>
93// For unknown languages, fallbackSequence returns an empty array. Hardcode fallback
94// to 'en' in that case, as English messages are always defined.
95 $this->localisationCache->getItem( $code,'fallbackSequence' ) ?: ['en' ],
96
98// Use this mode when you don't want to fall back to English unless explicitly
99// defined, for example, when you have language-variant icons and an international
100// language-independent fallback.
101 $this->localisationCache->getItem( $code,'originalFallbackSequence' ),
102 };
103 }
104
113publicfunctiongetAllIncludingSiteLanguage( $code ) {
114// Usually, we will only store a tiny number of fallback chains, so we cache in a member.
115 $cacheKey ="{$code}-{$this->siteLangCode}";
116
117if ( !array_key_exists( $cacheKey, $this->fallbackCache ) ) {
118 $fallbacks = $this->getAll( $code );
119
120if ( $code === $this->siteLangCode ) {
121// Don't bother hitting the localisation cache a second time
122 $siteFallbacks = [ $code ];
123 }else {
124// Append the site's fallback chain, including the site language itself
125 $siteFallbacks = $this->getAll( $this->siteLangCode );
126 array_unshift( $siteFallbacks, $this->siteLangCode );
127
128// Eliminate any languages already included in the chain
129 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
130 }
131
132 $this->fallbackCache[$cacheKey] = [ $fallbacks, $siteFallbacks ];
133 }
134return $this->fallbackCache[$cacheKey];
135 }
136}
137
139class_alias( LanguageFallback::class,'MediaWiki\\Languages\\LanguageFallback' );
LocalisationCache
Caching for the contents of localisation files.
DefinitionLocalisationCache.php:34
MediaWiki\Language\LanguageFallback
DefinitionLanguageFallback.php:15
MediaWiki\Language\LanguageFallback\getAll
getAll( $code, $mode=LanguageFallbackMode::MESSAGES)
Get the ordered list of fallback languages.
DefinitionLanguageFallback.php:81
MediaWiki\Language\LanguageFallback\getAllIncludingSiteLanguage
getAllIncludingSiteLanguage( $code)
Get the ordered list of fallback languages, ending with the fallback language chain for the site lang...
DefinitionLanguageFallback.php:113
MediaWiki\Language\LanguageFallback\MESSAGES
const MESSAGES
Return a fallback chain for messages in getAll.
DefinitionLanguageFallback.php:21
MediaWiki\Language\LanguageFallback\getFirst
getFirst( $code)
Get the first fallback for a given language.
DefinitionLanguageFallback.php:67
MediaWiki\Language\LanguageFallback\STRICT
const STRICT
Return a strict fallback chain in getAll.
DefinitionLanguageFallback.php:28
MediaWiki\Language\LanguageFallback\__construct
__construct( $siteLangCode, LocalisationCache $localisationCache, LanguageNameUtils $langNameUtils)
Do not call this directly.
DefinitionLanguageFallback.php:50
MediaWiki\Language\LanguageNameUtils
A service that provides utilities to do with language names and codes.
DefinitionLanguageNameUtils.php:27
MediaWiki\Language\STRICT
@ STRICT
Return a fallback chain for messages in getAll.
DefinitionLanguageFallbackMode.php:23
MediaWiki\Language
DefinitionConverterRule.php:8

[8]ページ先頭

©2009-2025 Movatter.jp