Allow OutputPage::parse() to parse in any langauge, modified wfMsgExt() and wfMessage() accordingly

This commit is contained in:
Alexandre Emsenhuber 2011-01-05 12:24:39 +00:00
parent 5d4200ef8b
commit 1e22a9dbde
3 changed files with 22 additions and 11 deletions

View 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 );
}
}

View 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 );
}
/**

View 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();
}