Enable language conversion in "alt" and "title" attributes with preg_replace_callback...
authorPhilip Tzou <philip@users.mediawiki.org>
Wed, 28 Jan 2009 07:02:42 +0000 (07:02 +0000)
committerPhilip Tzou <philip@users.mediawiki.org>
Wed, 28 Jan 2009 07:02:42 +0000 (07:02 +0000)
languages/LanguageConverter.php

index aad2963..9c38d2f 100644 (file)
@@ -198,6 +198,25 @@ class LanguageConverter {
 
        }
        
+       /**
+        * caption convert, base on preg_replace_callback
+        *
+        * to convert text in "title" or "alt", like '<img alt="text" ... '
+        * or '<span title="text" ... '
+        *
+        * @return string like ' alt="yyyy"' or ' title="yyyy"'
+        * @private
+        */
+       function captionConvert( $matches ) {
+               // we convert captions except URL
+               $toVariant = $this->getPreferredVariant();
+               $title = $matches[1];
+               $text  = $matches[2];
+               if( !strpos( $text, '://' ) )
+                       $text = $this->translate($text, $toVariant);
+               return " $title=\"$text\"";
+       }
+
        /**
         * dictionary-based conversion
         *
@@ -248,8 +267,13 @@ class LanguageConverter {
 
                $ret = $this->translate($m[0], $toVariant);
                $mstart = $m[1]+strlen($m[0]);
+               
+               // enable convertsion of '<img alt="xxxx" ... ' or '<span title="xxxx" ... '
+               $captionpattern  = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/';
                foreach($matches as $m) {
-                       $ret .= substr($text, $mstart, $m[1]-$mstart);
+                       $mark = substr($text, $mstart, $m[1]-$mstart);
+                       $mark = preg_replace_callback($captionpattern, array(&$this, 'captionConvert'), $mark);
+                       $ret .= $mark;
                        $ret .= $this->translate($m[0], $toVariant);
                        $mstart = $m[1] + strlen($m[0]);
                }