Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
LegacyTranslationAids.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use Html;
7use IContextSource;
10use MediaWiki\Languages\LanguageFactory;
11use MessageGroup;
13use Title;
15
24 private $handle;
26 private $group;
28 private $context;
30 private $languageFactory;
31
32 public function __construct(
33 MessageHandle $handle,
34 IContextSource $context,
35 LanguageFactory $languageFactory
36 ) {
37 $this->handle = $handle;
38 $this->context = $context;
39 $this->group = $handle->getGroup();
40 $this->languageFactory = $languageFactory;
41 }
42
43 private function getDefinition(): ?string {
44 $obj = new MessageDefinitionAid(
45 $this->group,
46 $this->handle,
47 $this->context,
48 new TranslationAidDataProvider( $this->handle )
49 );
50
51 return $obj->getData()['value'];
52 }
53
60 public function getBoxes(): string {
61 $boxes = [];
62
63 try {
64 $boxes[] = $this->getDocumentationBox();
65 } catch ( TranslationHelperException $e ) {
66 $boxes[] = "<!-- Documentation not available: {$e->getMessage()} -->";
67 }
68
69 try {
70 $boxes[] = $this->getDefinitionBox();
71 } catch ( TranslationHelperException $e ) {
72 $boxes[] = "<!-- Definition not available: {$e->getMessage()} -->";
73 }
74
75 $this->context->getOutput()->addModuleStyles( 'ext.translate.quickedit' );
76 return Html::rawElement(
77 'div',
78 [ 'class' => 'mw-sp-translate-edit-fields' ],
79 implode( "\n\n", $boxes )
80 );
81 }
82
83 private function getDefinitionBox(): string {
84 $definition = $this->getDefinition();
85 if ( (string)$definition === '' ) {
86 throw new TranslationHelperException( 'Message lacks definition' );
87 }
88
89 $linkTag = self::ajaxEditLink( $this->handle->getTitle(), $this->group->getLabel() );
90 $label =
91 wfMessage( 'translate-edit-definition' )->escaped() .
92 wfMessage( 'word-separator' )->escaped() .
93 wfMessage( 'parentheses' )->rawParams( $linkTag )->escaped();
94
95 $sl = $this->languageFactory->getLanguage( $this->group->getSourceLanguage() );
96
97 $msg = Html::rawElement( 'div',
98 [
99 'class' => 'mw-translate-edit-deftext',
100 'dir' => $sl->getDir(),
101 'lang' => $sl->getHtmlCode(),
102 ],
103 TranslateUtils::convertWhiteSpaceToHTML( $definition )
104 );
105
106 $class = [ 'class' => 'mw-sp-translate-edit-definition' ];
107
108 return TranslateUtils::fieldset( $label, $msg, $class );
109 }
110
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 = TranslateUtils::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' ];
132
133 if ( (string)$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
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 = TranslateUtils::getEditorUrl( $handle );
153 return Html::element(
154 'a',
155 [ 'href' => $uri ],
156 $linkText
157 );
158 }
159}
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...
Class for pointing to messages, like Title class is for titles.
getGroup()
Get the primary MessageGroup this message belongs to.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
static getMessageContent( $key, $language, $namespace=NS_MEDIAWIKI)
Loads page content without side effects.
static fieldset( $legend, $contents, array $attributes=[])
Constructs a fieldset with contents.
static getEditorUrl(MessageHandle $handle)
Get an URL that points to an editor for this message handle.
Interface for message groups.