r92956 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92955‎ | r92956 | r92957 >
Date:19:03, 23 July 2011
Author:robin
Status:resolved (Comments)
Tags:
Comment:
DifferenceEngine: introduce setDiffLang() to change the language in which the diff text is written.
Translate: set the the diff text according to the source language, on Special:PageTranslation and the translation messages. (Also fix direction of CSS class mw-translate-fuzzy).
Modified paths:
  • /trunk/extensions/Translate/Translate.css (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslation.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageWebImporter.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)
  • /trunk/phase3/includes/diff/DifferenceEngine.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/diff/DifferenceEngine.php
@@ -25,6 +25,7 @@
2626 var $mOldid, $mNewid;
2727 var $mOldtitle, $mNewtitle, $mPagetitle;
2828 var $mOldtext, $mNewtext;
 29+ var $mDiffLang;
2930
3031 /**
3132 * @var Title
@@ -74,6 +75,9 @@
7576 }
7677 wfDebug( "DifferenceEngine old '$old' new '$new' rcid '$rcid'\n" );
7778
 79+ # Default language in which the diff text is written.
 80+ $this->mDiffLang = $this->mTitle->getPageLanguage();
 81+
7882 if ( 'prev' === $new ) {
7983 # Show diff between revision $old and the previous one.
8084 # Get previous one from DB.
@@ -938,10 +942,9 @@
939943 * @return string
940944 */
941945 function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
942 - // shared.css sets diff in interface language/dir,
943 - // but the actual content should be in the page language/dir
944 - $pageLang = $this->mTitle->getPageLanguage();
945 - $tableClass = 'diff diff-contentalign-' . htmlspecialchars( $pageLang->alignStart() );
 946+ // shared.css sets diff in interface language/dir, but the actual content
 947+ // is often in a different language, mostly the page content language/dir
 948+ $tableClass = 'diff diff-contentalign-' . htmlspecialchars( $this->mDiffLang->alignStart() );
946949 $header = "<table class='$tableClass'>";
947950 if ( $diff ) { // Safari/Chrome show broken output if cols not used
948951 $header .= "
@@ -982,6 +985,15 @@
983986 }
984987
985988 /**
 989+ * Set the language in which the diff text is written
 990+ * (Defaults to page content language).
 991+ * @since 1.19
 992+ */
 993+ function setDiffLang( $lang ) {
 994+ $this->mDiffLang = wfGetLangObj( $lang );
 995+ }
 996+
 997+ /**
986998 * Load revision metadata for the specified articles. If newid is 0, then compare
987999 * the old article in oldid to the current article; if oldid is 0, then
9881000 * compare the current article to the immediately previous one (ignoring the
Index: trunk/extensions/Translate/Translate.css
@@ -1,5 +1,6 @@
22 .mw-translate-fuzzy {
33 background-color: #FDD;
 4+ direction: ltr;
45 }
56 .mw-translate-definition-preview {
67 font-family: monospace;
Index: trunk/extensions/Translate/tag/SpecialPageTranslation.php
@@ -347,7 +347,7 @@
348348
349349 /** Displays the sections and changes for the user to review */
350350 public function showPage( TranslatablePage $page, Array $sections ) {
351 - global $wgOut;
 351+ global $wgOut, $wgContLang;
352352
353353 $wgOut->setSubtitle( $this->user->getSkin()->link( $page->getTitle() ) );
354354 TranslateUtils::injectCSS();
@@ -380,6 +380,7 @@
381381
382382 if ( $s->type === 'changed' ) {
383383 $diff = new DifferenceEngine;
 384+ $diff->setDiffLang( $wgContLang );
384385 $diff->setText( $s->getOldText(), $s->getText() );
385386 $text = $diff->getDiff( wfMsgHtml( 'tpt-diff-old' ), wfMsgHtml( 'tpt-diff-new' ) );
386387 $diff->showDiffStyle();
@@ -391,7 +392,9 @@
392393 $text = TranslateUtils::convertWhiteSpaceToHTML( $s->getText() );
393394 }
394395
395 - $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text ) );
 396+ # For changed text, the language is set by $diff->setDiffLang()
 397+ $lang = $s->type === 'changed' ? null : $wgContLang;
 398+ $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text, $lang ) );
396399 }
397400
398401 $deletedSections = $page->getParse()->getDeletedSections();
@@ -401,7 +404,7 @@
402405 foreach ( $deletedSections as $s ) {
403406 $name = wfMsgHtml( 'tpt-section-deleted', htmlspecialchars( $s->id ) );
404407 $text = TranslateUtils::convertWhiteSpaceToHTML( $s->getText() );
405 - $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text ) );
 408+ $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text, $wgContLang ) );
406409 }
407410 }
408411
@@ -415,6 +418,7 @@
416419 $wgOut->wrapWikiMsg( '==$1==', 'tpt-sections-template' );
417420
418421 $diff = new DifferenceEngine;
 422+ $diff->setDiffLang( $wgContLang );
419423 $diff->setText( $oldTemplate, $newTemplate );
420424 $text = $diff->getDiff( wfMsgHtml( 'tpt-diff-old' ), wfMsgHtml( 'tpt-diff-new' ) );
421425 $diff->showDiffStyle();
Index: trunk/extensions/Translate/utils/MessageWebImporter.php
@@ -470,12 +470,17 @@
471471 * @param $legend \string Legend as raw html.
472472 * @param $type \string Contents of type class.
473473 * @param $content \string Contents as raw html.
 474+ * @param $lang Language The language in which the text is written.
474475 * @return \string Section element as html.
475476 */
476 - public static function makeSectionElement( $legend, $type, $content ) {
 477+ public static function makeSectionElement( $legend, $type, $content, $lang = null ) {
477478 $containerParams = array( 'class' => "mw-tpt-sp-section mw-tpt-sp-section-type-{$type}" );
478479 $legendParams = array( 'class' => 'mw-tpt-sp-legend' );
479480 $contentParams = array( 'class' => 'mw-tpt-sp-content' );
 481+ if( $lang ) {
 482+ $contentParams['dir'] = wfGetLangObj( $lang )->getDir();
 483+ $contentParams['lang'] = wfGetLangObj( $lang )->getCode();
 484+ }
480485
481486 $items = new TagContainer();
482487 $items[] = new HtmlTag( 'div', new RawHtml( $legend ), $legendParams );
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -902,6 +902,7 @@
903903 }
904904
905905 $diff = new DifferenceEngine;
 906+ $diff->setDiffLang( $this->group->getSourceLanguage() );
906907 $diff->setText( $oldtext, $newtext );
907908 $diff->setReducedLineNumbers();
908909 $diff->showDiffStyle();
@@ -951,6 +952,7 @@
952953 }
953954
954955 $diff = new DifferenceEngine;
 956+ $diff->setDiffLang( $this->group->getSourceLanguage() );
955957 $diff->setText( $oldtext, $newtext );
956958 $diff->setReducedLineNumbers();
957959 $diff->showDiffStyle();

Sign-offs

UserFlagDate
Nikerabbitinspected09:58, 16 August 2011
Nikerabbittested09:58, 16 August 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r92963Follow-up r92956: move inline css to css file, and put the direction:ltr; at ...robin20:48, 23 July 2011
r92966Make r92956 compatible with previous releases.robin21:10, 23 July 2011
r96232Mark $mDiffLang protected, and rename function to setTextLanguage per Nikerab...robin16:09, 4 September 2011

Comments

#Comment by Nikerabbit (talk | contribs)   19:13, 23 July 2011
 .mw-translate-fuzzy {
 	background-color: #FDD;
+	direction: ltr;
 }

???

What does wgContLang do in SpecialPageTranslation?

#Comment by SPQRobin (talk | contribs)   19:45, 23 July 2011

The CSS is because the text in the class mw-translate-fuzzy should follow the user language direction (the "ltr" is flipped).

wgContLang is the language in which the page that should be translated, is written. It is different from the user language, which is the rest of the special page.

#Comment by Nikerabbit (talk | contribs)   20:04, 23 July 2011

Fuzzy sections should be in the same direction as rest of the page text.

I've just removed dozens of assumptions that English is the content language, this adds new assumption that source language equals wiki content language, or did I misunderstand?

#Comment by SPQRobin (talk | contribs)   20:22, 23 July 2011

Ah, I saw .mw-translate-fuzzy being used around the text "Outdated translations are marked like this." at the top, but it's apparently also used for the actual fuzzy sections. I'll fix that.

Wiki content language is not always English, and I think the wiki content language is most likely to be the language in which the translatable pages are written.

#Comment by Nikerabbit (talk | contribs)   20:30, 23 July 2011

Yes, most likely but not necessarily. If you can easily use $group->getSourceLanguage(), use it. If not let it stay like it is.

#Comment by SPQRobin (talk | contribs)   20:42, 23 July 2011

I know, I searched for it but the page translation feature does not depend on a group, afaik, so I didn't find it. Also, the chance that this usage of wgContLang could be a problem is very very small (it just adds dir & lang attributes).

#Comment by Siebrand (talk | contribs)   20:57, 23 July 2011

Please ensure that Translate remains compatible with the latest MediaWiki release.

#Comment by SPQRobin (talk | contribs)   21:15, 23 July 2011

I checked for the existence of setDiffLang in r92966 & r92967, so it is compatible now.

#Comment by Nikerabbit (talk | contribs)   09:58, 16 August 2011

I think mDiffLang should be protected, and I'd rename the function to setTextLanguage().

#Comment by SPQRobin (talk | contribs)   14:32, 16 August 2011

Hmm, I always wonder when a variable should be protected. Most others like mOldtext are neither protected...

#Comment by SPQRobin (talk | contribs)   16:10, 4 September 2011

Done in r96232

#Comment by SPQRobin (talk | contribs)   20:45, 30 August 2011

Siebrand, why did you mark this as fixme? If it is because of Nikerabbit's last comment, I didn't do that because I wasn't sure it's needed.

Status & tagging log