Allow OutputPage::parse() to parse in any langauge, modified wfMsgExt() and wfMessage...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 5 Jan 2011 12:24:39 +0000 (12:24 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 5 Jan 2011 12:24:39 +0000 (12:24 +0000)
includes/GlobalFunctions.php
includes/Message.php
includes/OutputPage.php

index 2eaf4a0..015c6e8 100644 (file)
@@ -756,12 +756,15 @@ function wfMsgExt( $key, $options ) {
        if( in_array( 'content', $options, true ) ) {
                $forContent = true;
                $langCode = true;
+               $langCodeObj = null;
        } elseif( array_key_exists( 'language', $options ) ) {
                $forContent = false;
                $langCode = wfGetLangObj( $options['language'] );
+               $langCodeObj = $langCode;
        } else {
                $forContent = false;
                $langCode = false;
+               $langCodeObj = null;
        }
 
        $string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false );
@@ -771,9 +774,9 @@ function wfMsgExt( $key, $options ) {
        }
 
        if( in_array( 'parse', $options, true ) ) {
-               $string = $wgOut->parse( $string, true, !$forContent );
+               $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
        } elseif ( in_array( 'parseinline', $options, true ) ) {
-               $string = $wgOut->parse( $string, true, !$forContent );
+               $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
                $m = array();
                if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
                        $string = $m[1];
@@ -782,8 +785,7 @@ function wfMsgExt( $key, $options ) {
                global $wgMessageCache;
                if ( isset( $wgMessageCache ) ) {
                        $string = $wgMessageCache->transform( $string,
-                               !$forContent,
-                               is_object( $langCode ) ? $langCode : null );
+                               !$forContent, $langCodeObj );
                }
        }
 
index 72232ec..97f77c1 100644 (file)
@@ -315,12 +315,8 @@ class Message {
         * @return Wikitext parsed into HTML
         */
        protected function parseText( $string ) {
-               global $wgOut, $wgLang, $wgContLang;
-               if ( $this->language !== $wgLang && $this->language !== $wgContLang ) {
-                       # FIXME: remove this limitation
-                       throw new MWException( 'Can only parse in interface or content language' );
-               }
-               return $wgOut->parse( $string, /*linestart*/true, $this->interface );
+               global $wgOut;
+               return $wgOut->parse( $string, /*linestart*/true, $this->interface, $this->language );
        }
 
        /**
index 581653d..a523f3c 100644 (file)
@@ -1227,24 +1227,37 @@ class OutputPage {
         * @param $interface Boolean: use interface language ($wgLang instead of
         *                   $wgContLang) while parsing language sensitive magic
         *                   words like GRAMMAR and PLURAL
+        * @param $language  Language object: target language object, will override
+        *                   $interface
         * @return String: HTML
         */
-       public function parse( $text, $linestart = true, $interface = false ) {
+       public function parse( $text, $linestart = true, $interface = false, $language = null ) {
                global $wgParser;
+
                if( is_null( $this->getTitle() ) ) {
                        throw new MWException( 'Empty $mTitle in ' . __METHOD__ );
                }
+
                $popts = $this->parserOptions();
                if ( $interface ) {
                        $popts->setInterfaceMessage( true );
                }
+               if ( $language !== null ) {
+                       $oldLang = $popts->setTargetLanguage( $language );
+               }
+
                $parserOutput = $wgParser->parse(
                        $text, $this->getTitle(), $popts,
                        $linestart, true, $this->mRevisionId
                );
+
                if ( $interface ) {
                        $popts->setInterfaceMessage( false );
                }
+               if ( $language !== null ) {
+                       $popts->setTargetLanguage( $oldLang );
+               }
+
                return $parserOutput->getText();
        }