r46434 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46433‎ | r46434 | r46435 >
Date:07:02, 28 January 2009
Author:philip
Status:deferred (Comments)
Tags:
Comment:
Enable language conversion in "alt" and "title" attributes with preg_replace_callback. (fixed: rev 46068)
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -199,6 +199,25 @@
200200 }
201201
202202 /**
 203+ * caption convert, base on preg_replace_callback
 204+ *
 205+ * to convert text in "title" or "alt", like '<img alt="text" ... '
 206+ * or '<span title="text" ... '
 207+ *
 208+ * @return string like ' alt="yyyy"' or ' title="yyyy"'
 209+ * @private
 210+ */
 211+ function captionConvert( $matches ) {
 212+ // we convert captions except URL
 213+ $toVariant = $this->getPreferredVariant();
 214+ $title = $matches[1];
 215+ $text = $matches[2];
 216+ if( !strpos( $text, '://' ) )
 217+ $text = $this->translate($text, $toVariant);
 218+ return " $title=\"$text\"";
 219+ }
 220+
 221+ /**
203222 * dictionary-based conversion
204223 *
205224 * @param string $text the text to be converted
@@ -248,8 +267,13 @@
249268
250269 $ret = $this->translate($m[0], $toVariant);
251270 $mstart = $m[1]+strlen($m[0]);
 271+
 272+ // enable convertsion of '<img alt="xxxx" ... ' or '<span title="xxxx" ... '
 273+ $captionpattern = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/';
252274 foreach($matches as $m) {
253 - $ret .= substr($text, $mstart, $m[1]-$mstart);
 275+ $mark = substr($text, $mstart, $m[1]-$mstart);
 276+ $mark = preg_replace_callback($captionpattern, array(&$this, 'captionConvert'), $mark);
 277+ $ret .= $mark;
254278 $ret .= $this->translate($m[0], $toVariant);
255279 $mstart = $m[1] + strlen($m[0]);
256280 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r51112(bug 8873) follow up r46434, update the RELEASE-NOTESshinjiman12:53, 28 May 2009
r51113backporting r51112...shinjiman12:54, 28 May 2009

Comments

#Comment by Werdna (talk | contribs)   22:52, 11 February 2009

Is there a better way to do this? (I don't know the code that well).

Using regexes to parse HTML seems all kinds of awful.

#Comment by Werdna (talk | contribs)   23:10, 11 February 2009

Tweaked in r46528 ("mTables' element must not be left blank, to ensure the translate() function could always return a string.")

Status & tagging log