r69192 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69191‎ | r69192 | r69193 >
Date:17:06, 8 July 2010
Author:roberthl
Status:deferred
Tags:
Comment:
Translate:
- Unmangle JavaScript FFS message keys when writing to variable.
- Parse authors list from JavaScript FFS files.
- Small improvements to the readability of JavaScript FFS code.
Modified paths:
  • /trunk/extensions/Translate/FFS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/FFS.php
@@ -380,8 +380,19 @@
381381 abstract protected function footer();
382382
383383 public function readFromVariable( $data ) {
384 - /* Pre-processing */
 384+ /* Parse authors list */
 385+ $authors = preg_replace( "#/\* Translators\:\n(.*?)\n \*/(.*)#s", '$1', $data );
 386+ if( $authors === $data ) {
 387+ $authors = array();
 388+ } else {
 389+ $authors = explode( "\n", $authors );
 390+ for( $i = 0; $i < count( $authors ); $i++ ) {
 391+ $authors[$i] = substr( $authors[$i], 6 );
 392+ }
 393+ }
385394
 395+ /* Pre-processing of messages */
 396+
386397 // Find the start and end of the data section (enclosed in curly braces).
387398 $dataStart = strpos( $data, '{' );
388399 $dataEnd = strrpos( $data, '}' );
@@ -397,7 +408,7 @@
398409 // Strip excess whitespace.
399410 $data = trim( $data );
400411
401 - /* Per-key message */
 412+ /* Per-key message processing */
402413
403414 // Break in to segments.
404415 $data = explode( "\",\n", $data );
@@ -407,11 +418,10 @@
408419 // Add back trailing quote, removed by explosion.
409420 $segment .= '"';
410421
411 - // Concatenate separate strings.
 422+ // Concatenate separated strings.
412423 $segment = explode( '" +', $segment );
413424 for( $i = 0; $i < count( $segment ); $i++ ) {
414 - $segment[$i] = ltrim( $segment[$i] );
415 - $segment[$i] = ltrim( $segment[$i], '"' );
 425+ $segment[$i] = ltrim( ltrim( $segment[$i] ), '"' );
416426 }
417427 $segment = implode( $segment );
418428
@@ -420,37 +430,36 @@
421431
422432 // Break in to key and message.
423433 $segments = explode( ': "', $segment );
424 - $key = $segments[ 0 ];
425 - $value = $segments[1];
426434
427 - // Strip excess whitespace from key and value.
428 - $key = trim( $key );
429 - $value = trim( $value );
 435+ // Strip excess whitespace from key and value, then quotation marks.
 436+ $key = trim( trim( $segments[0] ), '\'"' );
 437+ $value = trim( trim( $segments[1] ), '\'"' );
430438
431 - // Strip quotation marks.
432 - $key = trim( $key, '\'"' );
433 - $value = trim( $value, '\'"' );
434 -
435 - // Unescape any JavaScript and append to message array.
 439+ // Unescape any JavaScript string syntax and append to message array.
436440 $messages[$key] = self::unescapeJsString( $value );
437441 }
438442
439443 $messages = $this->group->getMangler()->mangle( $messages );
440444
441 - // FIXME: authors missing?
442 -
443 - return array( 'MESSAGES' => $messages );
 445+ return array(
 446+ 'AUTHORS' => $authors,
 447+ 'MESSAGES' => $messages
 448+ );
444449 }
445450
446451 public function writeIntoVariable( MessageCollection $collection ) {
447452 $r = $this->header( $collection->code, $collection->getAuthors() );
448453
 454+ $mangler = $this->group->getMangler();
 455+
449456 // Get and write messages.
450457 foreach ( $collection as $message ) {
451 - $key = $this->transformKey( Xml::escapeJsString( $message->key() ) );
452 - $value = Xml::escapeJsString( $message->translation() );
 458+ $key = $mangler->unmangle( $message->key() );
 459+ $key = $this->transformKey( Xml::escapeJsString( $key ) );
453460
454 - $r .= " {$key}: \"{$value}\",\n\n";
 461+ $translation = Xml::escapeJsString( $message->translation() );
 462+
 463+ $r .= " {$key}: \"{$translation}\",\n\n";
455464 }
456465
457466 // Strip last comma, re-add trailing newlines.
@@ -461,19 +470,12 @@
462471 }
463472
464473 protected function authorsList( $authors ) {
465 - if( count( $authors ) > 0 ) {
466 - foreach ( $authors as $author ) {
467 - $authorsList .= " * - $author\n";
468 - }
469 - return <<<EOT
470 -/* Translators:
471 -$authorsList */
 474+ if( count( $authors ) === 0 ) return '';
472475
473 -
474 -EOT;
475 - } else {
476 - return '';
 476+ foreach ( $authors as $author ) {
 477+ $authorsList .= " * - $author\n";
477478 }
 479+ return "/* Translators:\n$authorsList */\n\n";
478480 }
479481
480482 private static function unescapeJsString( $string ) {
@@ -547,14 +549,8 @@
548550 }
549551
550552 protected function header( $code, $authors ) {
551 -
552553 $authorsList = $this->authorsList( $authors );
553 -
554 - return <<<EOT
555 -{$authorsList}var I18n = {
556 -
557 -
558 -EOT;
 554+ return "{$authorsList}var I18n = {\n\n";
559555 }
560556
561557 protected function footer() {

Status & tagging log