Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4129155
MessageBundleTranslationLoader.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
MessageBundleTranslationLoader.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\MessageBundleTranslation
;
use
MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups
;
use
MediaWiki\Extension\Translate\MessageLoading\MessageCollection
;
use
MediaWiki\Languages\LanguageFallback
;
use
RuntimeException
;
class
MessageBundleTranslationLoader
{
private
LanguageFallback
$languageFallback
;
public
function
__construct
(
LanguageFallback
$languageFallback
)
{
$this
->
languageFallback
=
$languageFallback
;
}
/**
* Given a language code, returns translation for that language or its fallbacks in an array format.
* @param MessageBundle $messageBundle
* @param string $languageCode
* @param bool $skipFallbacks Skip loading the fallback languages
* @return array<string, string> Key is the key in the message bundle, value is the translation.
*/
public
function
get
(
MessageBundle
$messageBundle
,
string
$languageCode
,
bool
$skipFallbacks
):
array
{
$translations
=
$this
->
getTranslationsWithFallback
(
$messageBundle
,
$languageCode
,
$skipFallbacks
);
$normalizedTranslations
=
[];
$prefixToReplace
=
'/^'
.
preg_quote
(
$messageBundle
->
getTitle
()->
getPrefixedDBkey
()
.
'/'
,
'/'
)
.
'/'
;
foreach
(
$translations
as
$key
=>
$translation
)
{
$normalizedTranslations
[
preg_replace
(
$prefixToReplace
,
''
,
$key
)
]
=
$translation
;
}
return
$normalizedTranslations
;
}
private
function
getTranslationsWithFallback
(
MessageBundle
$messageBundle
,
string
$languageCode
,
bool
$skipFallbacks
):
array
{
$messageBundleGroup
=
MessageGroups
::
getGroup
(
$messageBundle
->
getMessageGroupId
()
);
if
(
!
$messageBundleGroup
)
{
throw
new
RuntimeException
(
"Did not find message group for message bundle: {$messageBundle->getTitle()->getPrefixedText()}"
);
}
if
(
$skipFallbacks
)
{
$fallbackChain
=
[
$languageCode
];
}
else
{
$fallbackChain
=
[
$languageCode
,
...
$this
->
languageFallback
->
getAll
(
$languageCode
),
$messageBundleGroup
->
getSourceLanguage
()
];
}
$collection
=
$messageBundleGroup
->
initCollection
(
$fallbackChain
[
0
]
);
$translations
=
[];
foreach
(
$fallbackChain
as
$fallbackLanguageCode
)
{
$collection
->
resetForNewLanguage
(
$fallbackLanguageCode
);
// TODO use custom tag after fixing MessageCollection::filter
$collection
->
setTags
(
'ignored'
,
array_keys
(
$translations
)
);
$collection
->
filter
(
MessageCollection
::
FILTER_IGNORED
,
MessageCollection
::
EXCLUDE_MATCHING
);
if
(
count
(
$collection
)
===
0
)
{
break
;
}
$collection
->
loadTranslations
();
$collection
->
filter
(
MessageCollection
::
FILTER_HAS_TRANSLATION
,
MessageCollection
::
INCLUDE_MATCHING
);
foreach
(
$collection
as
$key
=>
$message
)
{
if
(
$message
->
translation
()
!==
null
)
{
$translations
[
$key
]
=
$message
->
translation
();
}
}
}
return
$translations
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 09:39 (2 w, 6 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
1d/27/fbd3988433a2e0908dadb3b91e90
Default Alt Text
MessageBundleTranslationLoader.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment