r83589 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83588‎ | r83589 | r83590 >
Date:16:24, 9 March 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
Protect some common untranslatable string from being mangled in translation engines
Modified paths:
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -345,7 +345,7 @@
346346
347347 $code = $this->targetLanguage;
348348 $definition = trim( strval( $this->getDefinition() ) );
349 - $definition = str_replace( "\n", "<newline/>", $definition );
 349+ $definition = self::wrapUntranslatable( $definition );
350350
351351 $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName );
352352 $unsupported = $wgMemc->get( $memckey );
@@ -376,7 +376,7 @@
377377
378378 if ( $response->responseStatus === 200 ) {
379379 $text = Sanitizer::decodeCharReferences( $response->responseData->translatedText );
380 - $text = str_replace( "<newline/>", "\n", $text );
 380+ $text = self::unwrapUntranslatable( $text );
381381 $text = $this->suggestionField( $text );
382382 return Html::rawElement( 'div', null, self::legend( $serviceName ) . $text . self::clear() );
383383 } elseif ( $response->responseDetails === 'invalid translation language pair' ) {
@@ -421,7 +421,7 @@
422422
423423 $code = $this->targetLanguage;
424424 $definition = trim( strval( $this->getDefinition() ) );
425 - $definition = str_replace( "\n", "<newline/>", $definition );
 425+ $definition = self::wrapUntranslatable( $definition );
426426
427427 $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName );
428428 $unsupported = $wgMemc->get( $memckey );
@@ -478,11 +478,24 @@
479479 $ret = $req->getContent();
480480 $text = preg_replace( '~<string.*>(.*)</string>~', '\\1', $ret );
481481 $text = Sanitizer::decodeCharReferences( $text );
482 - $text = trim( preg_replace( "~\s*<newline></newline>\s*~", "\n", $text ) );
 482+ $text = self::unwrapUntranslatable( $text );
483483 $text = $this->suggestionField( $text );
484484 return Html::rawElement( 'div', null, self::legend( $serviceName ) . $text . self::clear() );
485485 }
486486
 487+ protected static function wrapUntranslatable( $text ) {
 488+ $text = str_replace( "\n", "!N!", $text );
 489+ $wrap = '<span class="notranslate">\0</span>';
 490+ $text = preg_replace( '~%[^% ]+%|\$\d|{VAR:[^}]+}|{?{(PLURAL|GRAMMAR|GENDER):[^|]+\||%(\d\$)?[sd]~', $wrap, $text );
 491+ return $text;
 492+ }
 493+
 494+ protected static function unwrapUntranslatable( $text ) {
 495+ $text = str_replace( '!N!', "\n", $text );
 496+ $text = preg_replace( '~<span class="notranslate">(.*?)</span>~', '\1', $text );
 497+ return $text;
 498+ }
 499+
487500 protected function getApertiumSuggestion( $serviceName, $config ) {
488501 global $wgMemc;
489502

Status & tagging log