Merge "Inject localizer service into HTML formatter class"

This commit is contained in:
jenkins-bot 2022-02-02 11:54:05 +00:00 committed by Gerrit Code Review
commit deeea30725
3 changed files with 56 additions and 22 deletions

View file

@ -231,8 +231,8 @@ class TemplateDataBlob {
* @return string * @return string
*/ */
public function getHtml( Language $lang ): string { public function getHtml( Language $lang ): string {
$formatter = new TemplateDataHtmlFormatter(); $formatter = new TemplateDataHtmlFormatter( new TemplateDataMessageLocalizer( $lang ) );
return $formatter->getHtml( $this->getDataInLanguage( $lang->getCode() ), $lang ); return $formatter->getHtml( $this->getDataInLanguage( $lang->getCode() ) );
} }
/** /**

View file

@ -3,18 +3,27 @@
namespace MediaWiki\Extension\TemplateData; namespace MediaWiki\Extension\TemplateData;
use Html; use Html;
use Language; use MessageLocalizer;
use stdClass; use stdClass;
class TemplateDataHtmlFormatter { class TemplateDataHtmlFormatter {
/** @var MessageLocalizer */
private $localizer;
/**
* @param MessageLocalizer $localizer
*/
public function __construct( MessageLocalizer $localizer ) {
$this->localizer = $localizer;
}
/** /**
* @param stdClass $data * @param stdClass $data
* @param Language $lang
* *
* @return string * @return string
*/ */
public function getHtml( stdClass $data, Language $lang ): string { public function getHtml( stdClass $data ): string {
if ( is_string( $data->format ) && isset( TemplateDataValidator::PREDEFINED_FORMATS[$data->format] ) ) { if ( is_string( $data->format ) && isset( TemplateDataValidator::PREDEFINED_FORMATS[$data->format] ) ) {
// The following icon names are used here: // The following icon names are used here:
// * template-format-block // * template-format-block
@ -35,13 +44,13 @@ class TemplateDataHtmlFormatter {
] ]
], ],
$data->description ?? $data->description ??
wfMessage( 'templatedata-doc-desc-empty' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-desc-empty' )->text()
) )
. '</header>' . '</header>'
. '<table class="wikitable mw-templatedata-doc-params' . $sorting . '">' . '<table class="wikitable mw-templatedata-doc-params' . $sorting . '">'
. Html::rawElement( 'caption', [], . Html::rawElement( 'caption', [],
Html::element( 'p', [], Html::element( 'p', [],
wfMessage( 'templatedata-doc-params' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-params' )->text()
) )
. ( $formatMsg !== null ? . ( $formatMsg !== null ?
Html::rawElement( 'p', [], Html::rawElement( 'p', [],
@ -53,7 +62,7 @@ class TemplateDataHtmlFormatter {
// * templatedata-doc-format-block // * templatedata-doc-format-block
// * templatedata-doc-format-custom // * templatedata-doc-format-custom
// * templatedata-doc-format-inline // * templatedata-doc-format-inline
wfMessage( 'templatedata-doc-format-' . $formatMsg )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-format-' . $formatMsg )->text()
) )
) : ) :
'' ''
@ -61,16 +70,16 @@ class TemplateDataHtmlFormatter {
) )
. '<thead><tr>' . '<thead><tr>'
. Html::element( 'th', [ 'colspan' => 2 ], . Html::element( 'th', [ 'colspan' => 2 ],
wfMessage( 'templatedata-doc-param-name' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-name' )->text()
) )
. Html::element( 'th', [], . Html::element( 'th', [],
wfMessage( 'templatedata-doc-param-desc' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-desc' )->text()
) )
. Html::element( 'th', [], . Html::element( 'th', [],
wfMessage( 'templatedata-doc-param-type' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-type' )->text()
) )
. Html::element( 'th', [], . Html::element( 'th', [],
wfMessage( 'templatedata-doc-param-status' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-status' )->text()
) )
. '</tr></thead>' . '</tr></thead>'
. '<tbody>'; . '<tbody>';
@ -83,7 +92,7 @@ class TemplateDataHtmlFormatter {
'class' => 'mw-templatedata-doc-muted', 'class' => 'mw-templatedata-doc-muted',
'colspan' => 7 'colspan' => 7
], ],
wfMessage( 'templatedata-doc-no-params-set' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-no-params-set' )->text()
) )
. '</tr>'; . '</tr>';
} }
@ -95,7 +104,7 @@ class TemplateDataHtmlFormatter {
$aliases = ''; $aliases = '';
if ( count( $param->aliases ) ) { if ( count( $param->aliases ) ) {
foreach ( $param->aliases as $alias ) { foreach ( $param->aliases as $alias ) {
$aliases .= wfMessage( 'word-separator' )->inLanguage( $lang )->escaped() $aliases .= $this->localizer->msg( 'word-separator' )->escaped()
. Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ], . Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$alias $alias
); );
@ -106,13 +115,13 @@ class TemplateDataHtmlFormatter {
if ( $param->suggestedvalues ) { if ( $param->suggestedvalues ) {
$suggestedValues = ''; $suggestedValues = '';
foreach ( $param->suggestedvalues as $suggestedValue ) { foreach ( $param->suggestedvalues as $suggestedValue ) {
$suggestedValues .= wfMessage( 'word-separator' )->inLanguage( $lang )->escaped() $suggestedValues .= $this->localizer->msg( 'word-separator' )->escaped()
. Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ], . Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$suggestedValue $suggestedValue
); );
} }
$suggestedValuesLine .= Html::element( 'dt', [], $suggestedValuesLine .= Html::element( 'dt', [],
wfMessage( 'templatedata-doc-param-suggestedvalues' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-suggestedvalues' )->text()
) . Html::rawElement( 'dd', [], $suggestedValues ); ) . Html::rawElement( 'dd', [], $suggestedValues );
} }
@ -141,28 +150,28 @@ class TemplateDataHtmlFormatter {
], ],
Html::element( 'p', [], Html::element( 'p', [],
$param->description ?? $param->description ??
wfMessage( 'templatedata-doc-param-desc-empty' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-desc-empty' )->text()
) )
. Html::rawElement( 'dl', [], . Html::rawElement( 'dl', [],
// Suggested Values // Suggested Values
$suggestedValuesLine . $suggestedValuesLine .
// Default // Default
( $param->default !== null ? ( Html::element( 'dt', [], ( $param->default !== null ? ( Html::element( 'dt', [],
wfMessage( 'templatedata-doc-param-default' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-default' )->text()
) )
. Html::element( 'dd', [], . Html::element( 'dd', [],
$param->default $param->default
) ) : '' ) ) ) : '' )
// Example // Example
. ( $param->example !== null ? ( Html::element( 'dt', [], . ( $param->example !== null ? ( Html::element( 'dt', [],
wfMessage( 'templatedata-doc-param-example' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-example' )->text()
) )
. Html::element( 'dd', [], . Html::element( 'dd', [],
$param->example $param->example
) ) : '' ) ) ) : '' )
// Auto value // Auto value
. ( $param->autovalue !== null ? ( Html::element( 'dt', [], . ( $param->autovalue !== null ? ( Html::element( 'dt', [],
wfMessage( 'templatedata-doc-param-autovalue' )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-autovalue' )->text()
) )
. Html::rawElement( 'dd', [], . Html::rawElement( 'dd', [],
Html::element( 'code', [], $param->autovalue ) Html::element( 'code', [], $param->autovalue )
@ -185,7 +194,7 @@ class TemplateDataHtmlFormatter {
// templatedata-doc-param-type-url, templatedata-doc-param-type-wiki-file-name, // templatedata-doc-param-type-url, templatedata-doc-param-type-wiki-file-name,
// templatedata-doc-param-type-wiki-page-name, templatedata-doc-param-type-wiki-template-name, // templatedata-doc-param-type-wiki-page-name, templatedata-doc-param-type-wiki-template-name,
// templatedata-doc-param-type-wiki-user-name // templatedata-doc-param-type-wiki-user-name
wfMessage( 'templatedata-doc-param-type-' . $param->type )->inLanguage( $lang )->text() $this->localizer->msg( 'templatedata-doc-param-type-' . $param->type )->text()
) )
// Status // Status
. Html::element( 'td', . Html::element( 'td',
@ -207,7 +216,7 @@ class TemplateDataHtmlFormatter {
// templatedata-doc-param-status-optional // templatedata-doc-param-status-optional
// templatedata-doc-param-status-required // templatedata-doc-param-status-required
// templatedata-doc-param-status-suggested // templatedata-doc-param-status-suggested
wfMessage( "templatedata-doc-param-status-$status" )->inLanguage( $lang )->text() $this->localizer->msg( "templatedata-doc-param-status-$status" )->text()
) )
. '</tr>'; . '</tr>';
} }

View file

@ -0,0 +1,25 @@
<?php
namespace MediaWiki\Extension\TemplateData;
use Language;
use MessageLocalizer;
class TemplateDataMessageLocalizer implements MessageLocalizer {
/** @var Language */
private $language;
/**
* @param Language $language
*/
public function __construct( Language $language ) {
$this->language = $language;
}
/** @inheritDoc */
public function msg( $key, ...$params ) {
return wfMessage( $key, ...$params )->inLanguage( $this->language );
}
}