summaryrefslogtreecommitdiff
blob: 6dc9ff9a31b7256ac70ae6a05aa3b832f8eecba3 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface;

use Html;
use IContextSource;
use MediaWiki\Extension\Translate\TranslatorInterface\Aid\MessageDefinitionAid;
use MediaWiki\Extension\Translate\TranslatorInterface\Aid\TranslationAidDataProvider;
use MediaWiki\Languages\LanguageFactory;
use MessageGroup;
use MessageHandle;
use Title;
use TranslateUtils;

/**
 * Provides minimal translation aids which integrate with the edit page and on diffs for
 * translatable messages.
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */
class LegacyTranslationAids {
	/** @var MessageHandle */
	private $handle;
	/** @var MessageGroup */
	private $group;
	/** @var IContextSource */
	private $context;
	/** @var LanguageFactory */
	private $languageFactory;

	public function __construct(
		MessageHandle $handle,
		IContextSource $context,
		LanguageFactory $languageFactory
	) {
		$this->handle = $handle;
		$this->context = $context;
		$this->group = $handle->getGroup();
		$this->languageFactory = $languageFactory;
	}

	private function getDefinition(): ?string {
		$obj = new MessageDefinitionAid(
			$this->group,
			$this->handle,
			$this->context,
			new TranslationAidDataProvider( $this->handle )
		);

		return $obj->getData()['value'];
	}

	/**
	 * Returns block element HTML snippet that contains the translation aids.
	 * Not all boxes are shown all the time depending on whether they have
	 * any information to show and on configuration variables.
	 * @return string Block level HTML snippet or empty string.
	 */
	public function getBoxes(): string {
		$boxes = [];

		try {
			$boxes[] = $this->getDocumentationBox();
		} catch ( TranslationHelperException $e ) {
			$boxes[] = "<!-- Documentation not available: {$e->getMessage()} -->";
		}

		try {
			$boxes[] = $this->getDefinitionBox();
		} catch ( TranslationHelperException $e ) {
			$boxes[] = "<!-- Definition not available: {$e->getMessage()} -->";
		}

		$this->context->getOutput()->addModuleStyles( 'ext.translate.quickedit' );
		return Html::rawElement(
			'div',
			[ 'class' => 'mw-sp-translate-edit-fields' ],
			implode( "\n\n", $boxes )
		);
	}

	private function getDefinitionBox(): string {
		$definition = $this->getDefinition();
		if ( (string)$definition === '' ) {
			throw new TranslationHelperException( 'Message lacks definition' );
		}

		$linkTag = self::ajaxEditLink( $this->handle->getTitle(), $this->group->getLabel() );
		$label =
			wfMessage( 'translate-edit-definition' )->escaped() .
			wfMessage( 'word-separator' )->escaped() .
			wfMessage( 'parentheses' )->rawParams( $linkTag )->escaped();

		$sl = $this->languageFactory->getLanguage( $this->group->getSourceLanguage() );

		$msg = Html::rawElement( 'div',
			[
				'class' => 'mw-translate-edit-deftext',
				'dir' => $sl->getDir(),
				'lang' => $sl->getHtmlCode(),
			],
			TranslateUtils::convertWhiteSpaceToHTML( $definition )
		);

		$class = [ 'class' => 'mw-sp-translate-edit-definition' ];

		return TranslateUtils::fieldset( $label, $msg, $class );
	}

	private function getDocumentationBox(): string {
		global $wgTranslateDocumentationLanguageCode;

		if ( !$wgTranslateDocumentationLanguageCode ) {
			throw new TranslationHelperException( 'Message documentation language code is not defined' );
		}

		$page = $this->handle->getKey();
		$ns = $this->handle->getTitle()->getNamespace();

		$title = $this->handle->getTitleForLanguage( $wgTranslateDocumentationLanguageCode );
		$edit = $this->ajaxEditLink(
			$title,
			$this->context->msg( 'translate-edit-contribute' )->text()
		);
		$info = TranslateUtils::getMessageContent( $page, $wgTranslateDocumentationLanguageCode, $ns );

		$class = 'mw-sp-translate-edit-info';

		// The information is most likely in English
		$divAttribs = [ 'dir' => 'ltr', 'lang' => 'en', 'class' => 'mw-content-ltr' ];

		if ( (string)$info === '' ) {
			$info = $this->context->msg( 'translate-edit-no-information' )->plain();
			$class = 'mw-sp-translate-edit-noinfo';
			$lang = $this->context->getLanguage();
			// The message saying that there's no info, should be translated
			$divAttribs = [ 'dir' => $lang->getDir(), 'lang' => $lang->getHtmlCode() ];
		}
		$class .= ' mw-sp-translate-message-documentation';

		$contents = $this->context->getOutput()->parseInlineAsInterface( $info );

		return TranslateUtils::fieldset(
			$this->context->msg( 'translate-edit-information' )->rawParams( $edit )->escaped(),
			Html::rawElement( 'div', $divAttribs, $contents ), [ 'class' => $class ]
		);
	}

	private function ajaxEditLink( Title $target, string $linkText ): string {
		$handle = new MessageHandle( $target );
		$uri = TranslateUtils::getEditorUrl( $handle );
		return Html::element(
			'a',
			[ 'href' => $uri ],
			$linkText
		);
	}
}