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\Languages\LanguageFactory;
14use MediaWiki\Title\Title;
15use MessageGroup;
16
24 private MessageHandle $handle;
25 private MessageGroup $group;
26 private IContextSource $context;
27 private LanguageFactory $languageFactory;
28
29 public function __construct(
30 MessageHandle $handle,
31 IContextSource $context,
32 LanguageFactory $languageFactory
33 ) {
34 $this->handle = $handle;
35 $this->context = $context;
36 $group = $handle->getGroup();
37 if ( !$group ) {
38 throw new InvalidArgumentException(
39 'Message handle ' . $handle->getTitle()->getPrefixedDbKey() . ' has no associated group'
40 );
41 }
42 $this->group = $group;
43 $this->languageFactory = $languageFactory;
44 }
45
46 private function getDefinition(): ?string {
47 $obj = new MessageDefinitionAid(
48 $this->group,
49 $this->handle,
50 $this->context,
51 new TranslationAidDataProvider( $this->handle )
52 );
53
54 return $obj->getData()['value'];
55 }
56
63 public function getBoxes(): string {
64 $boxes = [];
65
66 try {
67 $boxes[] = $this->getDocumentationBox();
68 } catch ( TranslationHelperException $e ) {
69 $boxes[] = "<!-- Documentation not available: {$e->getMessage()} -->";
70 }
71
72 try {
73 $boxes[] = $this->getDefinitionBox();
74 } catch ( TranslationHelperException $e ) {
75 $boxes[] = "<!-- Definition not available: {$e->getMessage()} -->";
76 }
77
78 $this->context->getOutput()->addModuleStyles( 'ext.translate.quickedit' );
79 return Html::rawElement(
80 'div',
81 [ 'class' => 'mw-sp-translate-edit-fields' ],
82 implode( "\n\n", $boxes )
83 );
84 }
85
86 private function getDefinitionBox(): string {
87 $definition = $this->getDefinition();
88 if ( $definition === null || $definition === '' ) {
89 throw new TranslationHelperException( 'Message lacks definition' );
90 }
91
92 $linkTag = self::ajaxEditLink( $this->handle->getTitle(), $this->group->getLabel() );
93 $label =
94 wfMessage( 'translate-edit-definition' )->escaped() .
95 wfMessage( 'word-separator' )->escaped() .
96 wfMessage( 'parentheses' )->rawParams( $linkTag )->escaped();
97
98 $sl = $this->languageFactory->getLanguage( $this->group->getSourceLanguage() );
99
100 $msg = Html::rawElement( 'div',
101 [
102 'class' => 'mw-translate-edit-deftext',
103 'dir' => $sl->getDir(),
104 'lang' => $sl->getHtmlCode(),
105 ],
106 Utilities::convertWhiteSpaceToHTML( $definition )
107 );
108
109 $class = [ 'class' => 'mw-sp-translate-edit-definition' ];
110
111 return Utilities::fieldset( $label, $msg, $class );
112 }
113
114 private function getDocumentationBox(): string {
115 global $wgTranslateDocumentationLanguageCode;
116
117 if ( !$wgTranslateDocumentationLanguageCode ) {
118 throw new TranslationHelperException( 'Message documentation language code is not defined' );
119 }
120
121 $page = $this->handle->getKey();
122 $ns = $this->handle->getTitle()->getNamespace();
123
124 $title = $this->handle->getTitleForLanguage( $wgTranslateDocumentationLanguageCode );
125 $edit = $this->ajaxEditLink(
126 $title,
127 $this->context->msg( 'translate-edit-contribute' )->text()
128 );
129 $info = Utilities::getMessageContent( $page, $wgTranslateDocumentationLanguageCode, $ns );
130
131 $class = 'mw-sp-translate-edit-info';
132
133 // The information is most likely in English
134 $divAttribs = [ 'dir' => 'ltr', 'lang' => 'en', 'class' => 'mw-content-ltr mw-parser-output' ];
135
136 if ( $info === null || $info === '' ) {
137 $info = $this->context->msg( 'translate-edit-no-information' )->plain();
138 $class = 'mw-sp-translate-edit-noinfo';
139 $lang = $this->context->getLanguage();
140 // The message saying that there's no info, should be translated
141 $divAttribs = [ 'dir' => $lang->getDir(), 'lang' => $lang->getHtmlCode() ];
142 }
143 $class .= ' mw-sp-translate-message-documentation';
144
145 $contents = $this->context->getOutput()->parseInlineAsInterface( $info );
146
147 return Utilities::fieldset(
148 $this->context->msg( 'translate-edit-information' )->rawParams( $edit )->escaped(),
149 Html::rawElement( 'div', $divAttribs, $contents ), [ 'class' => $class ]
150 );
151 }
152
153 private function ajaxEditLink( Title $target, string $linkText ): string {
154 $handle = new MessageHandle( $target );
155 $uri = Utilities::getEditorUrl( $handle );
156 return Html::element(
157 'a',
158 [ 'href' => $uri ],
159 $linkText
160 );
161 }
162}
Class for pointing to messages, like Title class is for titles.
getGroup()
Get the primary MessageGroup this message belongs to.
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:29
Interface for message groups.