r46068 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46067‎ | r46068 | r46069 >
Date:07:56, 23 January 2009
Author:philip
Status:reverted (Comments)
Tags:
Comment:
Enable language conversion
in "alt" and "title" attributes.
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -199,6 +199,24 @@
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+ * @param string $title the "title" or "alt" text
 209+ * @param string $text the text to be converted
 210+ * @param string $toVariant the target language code
 211+ * @return string like ' alt="yyyy"' or ' title="yyyy"'
 212+ */
 213+ protected function captionConvert( $title, $text, $toVariant ) {
 214+ // we convert captions except URL
 215+ if( !strpos( $text, '://' ) )
 216+ $text = $this->translate($text, $toVariant);
 217+ return " $title=\"$text\"";
 218+ }
 219+
 220+ /**
203221 * dictionary-based conversion
204222 *
205223 * @param string $text the text to be converted
@@ -248,8 +266,14 @@
249267
250268 $ret = $this->translate($m[0], $toVariant);
251269 $mstart = $m[1]+strlen($m[0]);
 270+
 271+ // enable convertsion of '<img alt="xxxx" ... ' or '<span title="xxxx" ... '
 272+ $captionpattern = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/e';
 273+ $replacement = "\$this->captionConvert('\\1', '\\2', \$toVariant)";
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($captionpattern, $replacement, $mark);
 277+ $ret .= $mark;
254278 $ret .= $this->translate($m[0], $toVariant);
255279 $mstart = $m[1] + strlen($m[0]);
256280 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r46400Revert r46068 " Enable language conversion in "alt" and "title" attributes."...brion23:12, 27 January 2009

Comments

#Comment by Brion VIBBER (talk | contribs)   22:15, 26 January 2009

Use of eval is strongly discouraged, on penalty of death. :)

Needs to be redone using preg_replace_callback

#Comment by Brion VIBBER (talk | contribs)   23:13, 27 January 2009

Reverted in r46400 pending fix.

#Comment by PhiLiP (talk | contribs)   07:03, 28 January 2009

Fixed in r46434.

Status & tagging log