Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/97778
Change subject: (bug #56685) allow localized output of decimal values.
......................................................................
(bug #56685) allow localized output of decimal values.
Change-Id: Ie0ea0d5b921b3f4254bd0239a5ab4df69bd28fc3
---
M DataValuesCommon/DataValuesCommon.classes.php
M DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
A DataValuesCommon/src/ValueFormatters/Localizer.php
M DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
4 files changed, 75 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues
refs/changes/78/97778/1
diff --git a/DataValuesCommon/DataValuesCommon.classes.php
b/DataValuesCommon/DataValuesCommon.classes.php
index d0f17ea..5cdc071 100644
--- a/DataValuesCommon/DataValuesCommon.classes.php
+++ b/DataValuesCommon/DataValuesCommon.classes.php
@@ -11,6 +11,7 @@
'ValueFormatters\StringFormatter' =>
'src/ValueFormatters/StringFormatter.php',
'ValueFormatters\TimeIsoFormatter' =>
'src/ValueFormatters/TimeIsoFormatter.php',
'ValueFormatters\TimeFormatter' =>
'src/ValueFormatters/TimeFormatter.php',
+ 'ValueFormatters\Localizer' => 'src/ValueFormatters/Localizer.php',
'ValueFormatters\Test\ValueFormatterFactoryTest' =>
'tests/ValueFormatters/ValueFormatterFactoryTest.php',
diff --git a/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
b/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
index 41a3cc5..330df74 100644
--- a/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
+++ b/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
@@ -4,6 +4,7 @@
use DataValues\DecimalValue;
use InvalidArgumentException;
+use Language;
/**
* Formatter for decimal values
@@ -22,10 +23,21 @@
*/
const OPT_FORCE_SIGN = 'forceSign';
- public function __construct( FormatterOptions $options ) {
+ /**
+ * @var Localizer
+ */
+ protected $localizer;
+
+ /**
+ * @param FormatterOptions $options
+ * @param Localizer|null $localizer
+ */
+ public function __construct( FormatterOptions $options, Localizer
$localizer = null ) {
$options->defaultOption( self::OPT_FORCE_SIGN, false );
parent::__construct( $options );
+
+ $this->localizer = $localizer;
}
/**
@@ -43,7 +55,6 @@
throw new InvalidArgumentException( 'DataValue is not a
DecimalValue.' );
}
- // TODO: Implement localization of decimal numbers!
// TODO: Implement optional rounding/padding
$decimal = $dataValue->getValue();
@@ -52,6 +63,12 @@
$decimal = ltrim( $decimal, '+' );
}
+ if ( $this->localizer ) {
+ // apply number localization
+ $language = $this->getOption( ValueFormatter::OPT_LANG
);
+ $decimal = $this->localizer->localize( $decimal,
$language, $this->options );
+ }
+
return $decimal;
}
diff --git a/DataValuesCommon/src/ValueFormatters/Localizer.php
b/DataValuesCommon/src/ValueFormatters/Localizer.php
new file mode 100644
index 0000000..bd8fd36
--- /dev/null
+++ b/DataValuesCommon/src/ValueFormatters/Localizer.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace ValueFormatters;
+use InvalidArgumentException;
+
+/**
+ * Interface defining a service for localizing a string based on a language
code.
+ * This may for instance be used to re-format a numeric string according to
+ * the rules of a given locale.
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup ValueFormatters
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+interface Localizer {
+
+ /**
+ * Localizes a given string.
+ *
+ * Implementations are free to specify expectations
+ * as to for format of the string provided, and throw an exception if
the
+ * string does not conform to these expectations.
+ *
+ * @since 0.1
+ *
+ * @param string $string
+ * @param string $language
+ * @param FormatterOptions $options
+ *
+ * @return string
+ * @throws InvalidArgumentException
+ */
+ public function localize( $string, $language, FormatterOptions $options
);
+}
\ No newline at end of file
diff --git a/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
b/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
index 4eb7aa7..edeb1d9 100644
--- a/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
+++ b/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
@@ -5,6 +5,7 @@
use DataValues\DecimalValue;
use ValueFormatters\DecimalFormatter;
use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
use Wikibase\Lib\Serializers\SerializationOptions;
/**
@@ -64,4 +65,20 @@
return 'ValueFormatters\DecimalFormatter';
}
+ public function testLocalization() {
+ $localizer = $this->getMock( 'ValueFormatters\Localizer' );
+
+ $localizer->expects( $this->once() )
+ ->method( 'localize' )
+ ->will( $this->returnCallback( function ( $number,
$language ) {
+ return "$language:$number";
+ } ) );
+
+ $options = new FormatterOptions( array(
ValueFormatter::OPT_LANG => 'en' ) );
+ $value = new DecimalValue( '+12345' );
+ $formatter = new DecimalFormatter( $options, $localizer );
+
+ $this->assertEquals( 'en:12345', $formatter->format( $value ) );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/97778
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0ea0d5b921b3f4254bd0239a5ab4df69bd28fc3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits