Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
LegacyTranslationAids.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use InvalidArgumentException;
7use MediaWiki\Context\IContextSource;
12use MediaWiki\Html\Html;
13use MediaWiki\Language\LanguageFactory;
14use MediaWiki\Title\Title;
15use MessageGroup;
16
24
25 private MessageGroup $group;
26
27 public function __construct(
28 private readonly MessageHandle $handle,
29 private readonly IContextSource $context,
30 private readonly LanguageFactory $languageFactory,
31 ) {
32 $group = $handle->getGroup();
33 if ( !$group ) {
34 throw new InvalidArgumentException(
35 'Message handle ' . $handle->getTitle()->getPrefixedDbKey() . ' has no associated group'
36 );
37 }
38 $this->group = $group;
39 }
40
41 private function getDefinition(): ?string {
42 $obj = new MessageDefinitionAid(
43 $this->group,
44 $this->handle,
45 $this->context,
46 new TranslationAidDataProvider( $this->handle )
47 );
48
49 return $obj->getData()['value'];
50 }
51
58 public function getBoxes(): string {
59 $boxes = [];
60
61 try {
62 $boxes[] = $this->getDocumentationBox();
63 } catch ( TranslationHelperException $e ) {
64 $boxes[] = "<!-- Documentation not available: {$e->getMessage()} -->";
65 }
66
67 try {
68 $boxes[] = $this->getDefinitionBox();
69 } catch ( TranslationHelperException $e ) {
70 $boxes[] = "<!-- Definition not available: {$e->getMessage()} -->";
71 }
72
73 $this->context->getOutput()->addModuleStyles( 'ext.translate.quickedit' );
74 return Html::rawElement(
75 'div',
76 [ 'class' => 'mw-sp-translate-edit-fields' ],
77 implode( "\n\n", $boxes )
78 );
79 }
80
82 private function getDefinitionBox(): string {
83 $definition = $this->getDefinition();
84 if ( $definition === null || $definition === '' ) {
85 throw new TranslationHelperException( 'Message lacks definition' );
86 }
87
88 $linkTag = self::ajaxEditLink( $this->handle->getTitle(), $this->group->getLabel() );
89 $label =
90 wfMessage( 'translate-edit-definition' )->escaped() .
91 wfMessage( 'word-separator' )->escaped() .
92 wfMessage( 'parentheses' )->rawParams( $linkTag )->escaped();
93
94 $sl = $this->languageFactory->getLanguage( $this->group->getSourceLanguage() );
95
96 $msg = Html::rawElement( 'div',
97 [
98 'class' => 'mw-translate-edit-deftext',
99 'dir' => $sl->getDir(),
100 'lang' => $sl->getHtmlCode(),
101 ],
102 Utilities::convertWhiteSpaceToHTML( $definition )
103 );
104
105 $class = [ 'class' => 'mw-sp-translate-edit-definition' ];
106
107 return Utilities::fieldset( $label, $msg, $class );
108 }
109
111 private function getDocumentationBox(): string {
112 global $wgTranslateDocumentationLanguageCode;
113
114 if ( !$wgTranslateDocumentationLanguageCode ) {
115 throw new TranslationHelperException( 'Message documentation language code is not defined' );
116 }
117
118 $page = $this->handle->getKey();
119 $ns = $this->handle->getTitle()->getNamespace();
120
121 $title = $this->handle->getTitleForLanguage( $wgTranslateDocumentationLanguageCode );
122 $edit = $this->ajaxEditLink(
123 $title,
124 $this->context->msg( 'translate-edit-contribute' )->text()
125 );
126 $info = Utilities::getMessageContent( $page, $wgTranslateDocumentationLanguageCode, $ns );
127
128 $class = 'mw-sp-translate-edit-info';
129
130 // The information is most likely in English
131 $divAttribs = [ 'dir' => 'ltr', 'lang' => 'en', 'class' => 'mw-content-ltr mw-parser-output' ];
132
133 if ( $info === null || $info === '' ) {
134 $info = $this->context->msg( 'translate-edit-no-information' )->plain();
135 $class = 'mw-sp-translate-edit-noinfo';
136 $lang = $this->context->getLanguage();
137 // The message saying that there's no info, should be translated
138 $divAttribs = [ 'dir' => $lang->getDir(), 'lang' => $lang->getHtmlCode() ];
139 }
140 $class .= ' mw-sp-translate-message-documentation';
141
142 $contents = $this->context->getOutput()->parseInlineAsInterface( $info );
143
144 return Utilities::fieldset(
145 $this->context->msg( 'translate-edit-information' )->rawParams( $edit )->escaped(),
146 Html::rawElement( 'div', $divAttribs, $contents ), [ 'class' => $class ]
147 );
148 }
149
150 private function ajaxEditLink( Title $target, string $linkText ): string {
151 $handle = new MessageHandle( $target );
152 $uri = Utilities::getEditorUrl( $handle );
153 return Html::element(
154 'a',
155 [ 'href' => $uri ],
156 $linkText
157 );
158 }
159}
Class for pointing to messages, like Title class is for titles.
Provides minimal translation aids which integrate with the edit page and on diffs for translatable me...
getBoxes()
Returns block element HTML snippet that contains the translation aids.
Translation helpers can throw this exception when they cannot do anything useful with the current mes...
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
Interface for message groups.