r73773 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73772‎ | r73773 | r73774 >
Date:13:29, 26 September 2010
Author:nikerabbit
Status:ok (Comments)
Tags:
Comment:
Allow translation of titles of translatable pages

Added some encapsulation of getting message group for a translatable page
Modified paths:
  • /trunk/extensions/Translate/tag/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/tag/TPParse.php (modified) (history)
  • /trunk/extensions/Translate/tag/TranslatablePage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/tag/TranslatablePage.php
@@ -39,6 +39,8 @@
4040 */
4141 protected $init = false;
4242
 43+ protected $displayTitle = 'Page display title';
 44+
4345 /**
4446 * @param title Title object for the page
4547 */
@@ -146,6 +148,34 @@
147149 // Public functions //
148150
149151 /**
 152+ * Returns MessageGroup id (to be) used for translating this page.
 153+ * @return \string
 154+ */
 155+ public function getMessageGroupId() {
 156+ return 'page|' . $this->getTitle()->getPrefixedText();
 157+ }
 158+
 159+ /**
 160+ * Returns MessageGroup used for translating this page. It may still be empty
 161+ * if the page has not been ever marked.
 162+ * @return \type{WikiPageMessageGroup}
 163+ */
 164+ public function getMessageGroup() {
 165+ return MessageGroups::getGroup( $this->getMessageGroupId() );
 166+ }
 167+
 168+ /**
 169+ * Get translated page title.
 170+ * @param $code \string Language code.
 171+ * @return \string or null
 172+ */
 173+ public function getPageDisplayTitle( $code ) {
 174+ $section = str_replace( ' ', '_', $this->displayTitle );
 175+ $page = $this->getTitle()->getPrefixedDBKey();
 176+ return $this->getMessageGroup()->getMessage( "$page/$section", $code );
 177+ }
 178+
 179+ /**
150180 * Returns a TPParse object which represents the parsed page.
151181 * Throws TPExcetion if the page is malformed as a translatable
152182 * page.
@@ -161,6 +191,13 @@
162192 $text = self::armourNowiki( $nowiki, $text );
163193
164194 $sections = array();
 195+
 196+ // Add section to allow translating the page name
 197+ $displaytitle = new TPSection;
 198+ $displaytitle->id = $this->displayTitle
 199+ $displaytitle->text = $this->getTitle()->getPrefixedText();
 200+ $sections[self::getUniq()] = $displaytitle;
 201+
165202 $tagPlaceHolders = array();
166203
167204 while ( true ) {
@@ -500,7 +537,7 @@
501538 $titles = $this->getTranslationPages();
502539
503540 // Calculate percentages for the available translations
504 - $group = MessageGroups::getGroup( 'page|' . $this->getTitle()->getPrefixedText() );
 541+ $group = $this->getMessageGroup();
505542 if ( !$group instanceof WikiPageMessageGroup ) {
506543 return null;
507544 }
@@ -629,7 +666,6 @@
630667 $codes = Language::getLanguageNames( false );
631668 global $wgTranslateDocumentationLanguageCode, $wgContLang;
632669 unset( $codes[$wgTranslateDocumentationLanguageCode] );
633 - unset( $codes[$wgContLang->getCode()] );
634670
635671 if ( !isset( $codes[$code] ) ) {
636672 return false;
Index: trunk/extensions/Translate/tag/TPParse.php
@@ -76,10 +76,12 @@
7777 $sections = $this->sections;
7878 $highest = 0;
7979 foreach ( array_keys( $this->dbSections ) as $key ) {
 80+ if ( !is_int( $key ) ) continue;
8081 $highest = max( $highest, $key );
8182 }
8283
8384 foreach ( $sections as $_ ) {
 85+ if ( !is_int( $_->id ) ) continue;
8486 $highest = max( $_->id, $highest );
8587 }
8688
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -34,6 +34,10 @@
3535 if ( $page = TranslatablePage::isTranslationPage( $title ) ) {
3636 list( , $code ) = TranslateUtils::figureMessage( $title->getText() );
3737 $parser->getOptions()->setTargetLanguage( Language::factory( $code ) );
 38+ $name = $page->getPageDisplayTitle( $code );
 39+ if ( $name ) {
 40+ $parser->getOutput()->setDisplayTitle( $name );
 41+ }
3842 }
3943
4044 return true;
@@ -394,7 +398,7 @@
395399
396400 if ( $marked && $wgUser->isAllowed( 'translate' ) ) {
397401 $par = array(
398 - 'group' => 'page|' . $title->getPrefixedText(),
 402+ 'group' => $page->getMessageGroupId(),
399403 'language' => $wgLang->getCode(),
400404 'task' => 'view'
401405 );
@@ -462,7 +466,7 @@
463467 $wgOut->wrapWikiMsg( $wrap, array( 'tpt-translation-intro', $url, $titleText, $per ) );
464468
465469 if ( ((int) $per) < 100 ) {
466 - $group = MessageGroups::getGroup( 'page|' . $page->getTitle()->getPrefixedText() );
 470+ $group = $page->getMessageGroup();
467471 $collection = $group->initCollection( $code );
468472 $collection->filter( 'fuzzy', false );
469473 if ( count( $collection ) ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r73775Fix syntax error in r73773.siebrand13:56, 26 September 2010
r78178Transform translated page headers, follow-up r73773nikerabbit13:06, 10 December 2010

Comments

#Comment by Siebrand (talk | contribs)   13:55, 27 October 2010

This fails when using {{SITENAME}} in the translatable unit. Also triggers checkers if the page is in a namespace (f.e. Project:) and the translation is not.

Example: http://translatewiki.net/wiki/Project:About/nl

#Comment by Nikerabbit (talk | contribs)   16:01, 27 October 2010
#Comment by Siebrand (talk | contribs)   19:47, 27 October 2010
  1. I'd like to make the page name for "Project:About" in Dutch "Over SITENAME". Doesn't appear to work.
#Comment by Nikerabbit (talk | contribs)   19:50, 27 October 2010

Oh right it isn't parsed. I'll look if I can have it parsemag'ed

#Comment by Nikerabbit (talk | contribs)   13:06, 10 December 2010

Switching back to new due the follow-up.

Status & tagging log