From 1e22a9dbde1be2b57f1df1bd2ce0a343c88dbaea Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 5 Jan 2011 12:24:39 +0000 Subject: [PATCH] Allow OutputPage::parse() to parse in any langauge, modified wfMsgExt() and wfMessage() accordingly --- includes/GlobalFunctions.php | 10 ++++++---- includes/Message.php | 8 ++------ includes/OutputPage.php | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 2eaf4a0ba7..015c6e81cf 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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( '/^

(.*)\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 ); } } diff --git a/includes/Message.php b/includes/Message.php index 72232ec97d..97f77c1aa6 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -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 ); } /** diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 581653d072..a523f3cef9 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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(); } -- 2.20.1