Skip to content

Commit

Permalink
Merge pull request #1523 from SemanticMediaWiki/locl
Browse files Browse the repository at this point in the history
Set a language code for the DV formatter
  • Loading branch information
mwjames committed Apr 21, 2016
2 parents f6d7939 + 74610ea commit f591282
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions includes/SemanticData.php
Expand Up @@ -638,6 +638,7 @@ public function findSubSemanticData( $subobjectName ) {
*/
public function addSubSemanticData( SemanticData $semanticData ) {

$semanticData->setLastModified( $this->getLastModified() );
$this->hash = null;

if ( $this->subContainerDepthCounter > $this->subContainerMaxDepth ) {
Expand Down
23 changes: 23 additions & 0 deletions includes/datavalues/SMW_DataValue.php
Expand Up @@ -101,6 +101,11 @@ abstract class SMWDataValue {
*/
protected $m_outformat = false;

/**
* @var string
*/
private $languageCode = '';

/**
* Used to control the addition of the standard search link.
* @var boolean
Expand Down Expand Up @@ -269,6 +274,24 @@ public function getProperty() {
return $this->m_property;
}

/**
* @since 2.4
*
* @param string $languageCode
*/
public function setLanguageCode( $languageCode ) {
$this->languageCode = $languageCode;
}

/**
* @since 2.4
*
* @return string
*/
public function getLanguageCode() {
return $this->languageCode;
}

/**
* Specify the wiki page to which this value refers. This information is
* used to parse user values such as "#subsection" which only make sense
Expand Down
6 changes: 6 additions & 0 deletions includes/specials/SMW_SpecialBrowse.php
Expand Up @@ -2,6 +2,7 @@

use SMW\DIProperty;
use SMW\UrlEncoder;
use SMW\Localizer;

/**
* @ingroup SMWSpecialPage
Expand Down Expand Up @@ -251,6 +252,11 @@ private function displayData( SMWSemanticData $data, $left = true, $incoming = f
private function displayValue( SMWPropertyValue $property, SMWDataValue $dataValue, $incoming ) {
$linker = smwfGetLinker();

// Allow the DV formatter to access a specific language code
$dataValue->setLanguageCode(
Localizer::getInstance()->getUserLanguage()->getCode()
);

$html = $dataValue->getLongHTMLText( $linker );

if ( $dataValue->getTypeID() === '_wpg' || $dataValue->getTypeID() === '__sob' ) {
Expand Down
6 changes: 6 additions & 0 deletions includes/storage/SMW_ResultArray.php
Expand Up @@ -178,6 +178,12 @@ public function getNextDataValue() {
if ( $this->mPrintRequest->getOutputFormat() ) {
$dv->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
}

// Allow the DV formatter to access a specific language code
$dv->setLanguageCode(
\SMW\Localizer::getInstance()->getUserLanguage()->getCode()
);

return $dv;
}

Expand Down
5 changes: 4 additions & 1 deletion src/DataValues/ValueFormatters/TimeValueFormatter.php
Expand Up @@ -164,7 +164,10 @@ public function getMediaWikiDate() {
*/
public function getCaptionFromDataItem( DITime $dataItem ) {

$extraneousLanguage = Localizer::getInstance()->getExtraneousLanguage();
// If the language code is empty then the content language code is used
$extraneousLanguage = Localizer::getInstance()->getExtraneousLanguage(
$this->dataValue->getLanguageCode()
);

// https://en.wikipedia.org/wiki/Anno_Domini
// "...placing the "AD" abbreviation before the year number ... BC is
Expand Down
11 changes: 9 additions & 2 deletions src/Localizer.php
Expand Up @@ -74,10 +74,17 @@ public function getUserLanguage() {
/**
* @since 2.4
*
* @param string $languageCode
*
* @return ExtraneousLanguage
*/
public function getExtraneousLanguage() {
return $GLOBALS['smwgContLang'];
public function getExtraneousLanguage( $languageCode = '' ) {

if ( $languageCode === '' ) {
$languageCode = $this->getContentLanguage()->getCode();
}

return ExtraneousLanguage::getInstance()->fetchByLanguageCode( $languageCode );
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/Unit/LocalizerTest.php
Expand Up @@ -161,4 +161,19 @@ public function testCanNotGetLanguageCodeOnMissingLanguageCode() {
);
}

public function testExtraneousLanguage() {

$this->assertInstanceOf(
'\SMW\ExtraneousLanguage',
Localizer::getInstance()->getExtraneousLanguage()
);

$this->assertInstanceOf(
'\SMW\ExtraneousLanguage',
Localizer::getInstance()->getExtraneousLanguage( 'en' )
);

Localizer::clear();
}

}

0 comments on commit f591282

Please sign in to comment.