From 7f281e2db47575bedb08f8c73dd430cb0f3cd838 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 2 Feb 2018 12:54:02 -0800 Subject: [PATCH] skins: Deprecate MediaWikiI18N::translate() Remove all usage of $tpl->translator->translate() with wfMessage()->text(), which does the same thing, given the deprecated $tpl->translator->set() and $tpl->setTranslator() methods have no callers. These new wfMessage() callers should probably be changed to context calls via BaseTemplate::msg() or Skin::msg() at some point, but I'm reserving that for a separate commit given wfMessage() is what MediaWikiI18N::translate() currently does - to reduce risk of unrelated behaviour changes. While `$tpl->setTranslator` and `MediaWikiI18N::set` ($tpl->translator->set) have no known callers in Wikimedia Git, the `$tpl->translator->translate` method does have a handful of calls (although less than three, and not in bundled or WMF-deployed repos). As such, proceeding with hard-deprecation. Bug: T186090 Change-Id: I93d503de5515298288852ec4c150959fd688786b --- RELEASE-NOTES-1.31 | 1 + includes/skins/BaseTemplate.php | 7 +++---- includes/skins/MediaWikiI18N.php | 4 ++++ includes/skins/QuickTemplate.php | 24 ++++++++++++------------ 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/RELEASE-NOTES-1.31 b/RELEASE-NOTES-1.31 index ba7b86cbcf..576efe87f9 100644 --- a/RELEASE-NOTES-1.31 +++ b/RELEASE-NOTES-1.31 @@ -196,6 +196,7 @@ changes to languages because of Phabricator reports. used for that. Rather, setRef() existed as memory optimisation for PHP 4. * QuickTemplate::setTranslator() was deprecated in favour of Skin::msg() parameters. * MediaWikiI18N::set() was deprecated in favour of Skin::msg() parameters. +* MediaWikiI18N::translate() was deprecated in favour of Skin::msg() or wfMessage(). * Passing false to ParserOptions::setWrapOutputClass() is deprecated. Use the 'unwrap' transform to ParserOutput::getText() instead. * ParserOutput objects generated using a non-default value for diff --git a/includes/skins/BaseTemplate.php b/includes/skins/BaseTemplate.php index bb1d8d0b9d..08ab86af79 100644 --- a/includes/skins/BaseTemplate.php +++ b/includes/skins/BaseTemplate.php @@ -370,7 +370,7 @@ abstract class BaseTemplate extends QuickTemplate { if ( isset( $item['text'] ) ) { $text = $item['text']; } else { - $text = $this->translator->translate( isset( $item['msg'] ) ? $item['msg'] : $key ); + $text = wfMessage( isset( $item['msg'] ) ? $item['msg'] : $key )->text(); } $html = htmlspecialchars( $text ); @@ -541,8 +541,7 @@ abstract class BaseTemplate extends QuickTemplate { $realAttrs = [ 'type' => 'submit', 'name' => $mode, - 'value' => $this->translator->translate( - $mode == 'go' ? 'searcharticle' : 'searchbutton' ), + 'value' => wfMessage( $mode == 'go' ? 'searcharticle' : 'searchbutton' )->text(), ]; $realAttrs = array_merge( $realAttrs, @@ -568,7 +567,7 @@ abstract class BaseTemplate extends QuickTemplate { 'src' => $attrs['src'], 'alt' => isset( $attrs['alt'] ) ? $attrs['alt'] - : $this->translator->translate( 'searchbutton' ), + : wfMessage( 'searchbutton' )->text(), 'width' => isset( $attrs['width'] ) ? $attrs['width'] : null, 'height' => isset( $attrs['height'] ) ? $attrs['height'] : null, ]; diff --git a/includes/skins/MediaWikiI18N.php b/includes/skins/MediaWikiI18N.php index eeedaada20..970290af91 100644 --- a/includes/skins/MediaWikiI18N.php +++ b/includes/skins/MediaWikiI18N.php @@ -37,7 +37,11 @@ class MediaWikiI18N { $this->context[$varName] = $value; } + /** + * @deprecate since 1.31 Use BaseTemplate::msg(), Skin::msg(), or wfMessage() instead. + */ function translate( $value ) { + wfDeprecated( __METHOD__, '1.31' ); // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23 $value = preg_replace( '/^string:/', '', $value ); diff --git a/includes/skins/QuickTemplate.php b/includes/skins/QuickTemplate.php index 7782e70770..e8466dc11f 100644 --- a/includes/skins/QuickTemplate.php +++ b/includes/skins/QuickTemplate.php @@ -136,18 +136,18 @@ abstract class QuickTemplate { /** * @private - * @param string $str + * @param string $msgKey */ - function msg( $str ) { - echo htmlspecialchars( $this->translator->translate( $str ) ); + function msg( $msgKey ) { + echo htmlspecialchars( wfMessage( $msgKey )->text() ); } /** * @private - * @param string $str + * @param string $msgKey */ - function msgHtml( $str ) { - echo $this->translator->translate( $str ); + function msgHtml( $msgKey ) { + echo wfMessage( $msgKey )->text(); } /** @@ -155,10 +155,10 @@ abstract class QuickTemplate { * @private * @param string $str */ - function msgWiki( $str ) { + function msgWiki( $msgKey ) { global $wgOut; - $text = $this->translator->translate( $str ); + $text = wfMessage( $msgKey )->text(); echo $wgOut->parse( $text ); } @@ -174,12 +174,12 @@ abstract class QuickTemplate { /** * @private * - * @param string $str + * @param string $msgKey * @return bool */ - function haveMsg( $str ) { - $msg = $this->translator->translate( $str ); - return ( $msg != '-' ) && ( $msg != '' ); # ???? + function haveMsg( $msgKey ) { + $msg = wfMessage( $msgKey ); + return $msg->exists() && !$msg->isDisabled(); } /** -- 2.20.1