1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\PageTranslation;
use Content;
use ContentHandler;
use Language;
use MessageCollection;
use Parser;
use Title;
use TMessage;
use WikiPageMessageGroup;
/**
* Generates wikitext source code for translation pages.
*
* Also handles loading of translations, but that can be skipped and translations given directly.
*
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @since 2020.08
*/
class TranslationPage {
/** @var ParserOutput */
private $output;
/** @var WikiPageMessageGroup */
private $group;
/** @var Language */
private $targetLanguage;
/** @var Language */
private $sourceLanguage;
/** @var bool */
private $showOutdated;
/** @var bool */
private $wrapUntranslated;
/** @var Title */
private $sourcePageTitle;
public function __construct(
ParserOutput $output,
WikiPageMessageGroup $group,
Language $targetLanguage,
Language $sourceLanguage,
bool $showOutdated,
bool $wrapUntranslated,
Title $sourcePageTitle
) {
$this->output = $output;
$this->group = $group;
$this->targetLanguage = $targetLanguage;
$this->sourceLanguage = $sourceLanguage;
$this->showOutdated = $showOutdated;
$this->wrapUntranslated = $wrapUntranslated;
$this->sourcePageTitle = $sourcePageTitle;
}
/** @since 2021.07 */
public function getPageContent( Parser $parser ): Content {
$text = $this->generateSource( $parser );
$model = $this->sourcePageTitle->getContentModel();
return ContentHandler::makeContent( $text, null, $model );
}
public function getMessageCollection(): MessageCollection {
return $this->group->initCollection( $this->targetLanguage->getCode() );
}
public function filterMessageCollection( MessageCollection $collection ): void {
$collection->loadTranslations();
if ( $this->showOutdated ) {
$collection->filter( 'hastranslation', false );
} else {
$collection->filter( 'translated', false );
}
}
/** @return TMessage[] */
public function extractMessages( MessageCollection $collection ): array {
$messages = [];
$prefix = $this->sourcePageTitle->getPrefixedDBkey() . '/';
foreach ( $this->output->units() as $unit ) {
$messages[$unit->id] = $collection[$prefix . $unit->id] ?? null;
}
return $messages;
}
/**
* @param Parser $parser
* @param TMessage[] $messages
*/
public function generateSourceFromTranslations( Parser $parser, array $messages ): string {
$replacements = [];
foreach ( $this->output->units() as $placeholder => $unit ) {
/** @var TMessage $msg */
$msg = $messages[$unit->id] ?? null;
$replacements[$placeholder] = $unit->getTextForRendering(
$msg,
$this->sourceLanguage,
$this->targetLanguage,
$this->wrapUntranslated,
$parser
);
}
$template = $this->output->translationPageTemplate();
return strtr( $template, $replacements );
}
/** Generate translation page source using default options. */
private function generateSource( Parser $parser ): string {
$collection = $this->getMessageCollection();
$this->filterMessageCollection( $collection );
$messages = $this->extractMessages( $collection );
return $this->generateSourceFromTranslations( $parser, $messages );
}
}
|