Liangent has uploaded a new change for review.
https://gerrit.wikimedia.org/r/73754
Change subject: LanguageWithConversion translation change
......................................................................
LanguageWithConversion translation change
* Batched translation has been removed. Wikibase is so structured and it
is not so easy to use that in Wikibase.
* A new reverseTranslate() function has been added, trying to work out the
original string in source language.
* A few more tests have been added.
Change-Id: I38298ceb608edd50600b93176b2e88c75b0335e6
---
M lib/includes/LanguageWithConversion.php
M lib/tests/phpunit/LanguageWithConversionTest.php
2 files changed, 33 insertions(+), 48 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/54/73754/1
diff --git a/lib/includes/LanguageWithConversion.php
b/lib/includes/LanguageWithConversion.php
index c8de917..95f0a79 100644
--- a/lib/includes/LanguageWithConversion.php
+++ b/lib/includes/LanguageWithConversion.php
@@ -38,9 +38,6 @@
protected $sourceLanguageCode;
protected $parentLanguage;
- protected $translateCache = array();
- protected $translatePool = array();
-
/**
* Constructor.
*
@@ -206,6 +203,20 @@
}
/**
+ * Try to work out the original data (in source language) from a given
translation output.
+ *
+ * @param $text String
+ * @return String
+ */
+ public function reverseTranslate( $text ) {
+ if ( $this->parentLanguage ) {
+ return
$this->parentLanguage->getConverter()->translate( $text,
$this->getSourceLanguageCode() );
+ } else {
+ return $text;
+ }
+ }
+
+ /**
* Translate data after fetching them.
*
* @param $text String: Data to transform
@@ -213,49 +224,10 @@
*/
public function translate( $text ) {
if ( $this->parentLanguage ) {
- if ( isset( $this->translateCache[$text] ) ) {
- return $this->translateCache[$text];
- } else {
- $this->prepareForTranslate( $text );
- $this->executeTranslate();
- return $this->translateCache[$text];
- }
+ return
$this->parentLanguage->getConverter()->translate( $text,
$this->getLanguageCode() );
} else {
return $text;
}
}
- /**
- * Insert a text snippet which will be translated later.
- *
- * Due to the implementation of language converter, massive
- * calls with short text snippets may introduce big overhead.
- * If it's foreseeable that some text will be translated
- * later, add it here for batched translation.
- *
- * Does nothing if this is not a converted language.
- *
- * @param $text String
- */
- public function prepareForTranslate( $text ) {
- if ( $this->parentLanguage ) {
- $this->translatePool[$text] = true;
- }
- }
-
- /**
- * Really execute translation.
- */
- protected function executeTranslate() {
- if ( $this->parentLanguage && count( $this->translatePool ) ) {
- $pieces = array_keys( $this->translatePool );
- $block = implode( "\0", $pieces );
- $translatedBlock =
$this->parentLanguage->getConverter()->translate(
- $block, $this->language->getCode()
- );
- $translatedPieces = explode( "\0", $translatedBlock );
- $this->translateCache += array_combine( $pieces,
$translatedPieces );
- $this->translatePool = array();
- }
- }
}
diff --git a/lib/tests/phpunit/LanguageWithConversionTest.php
b/lib/tests/phpunit/LanguageWithConversionTest.php
index c13a6ac..90da79e 100644
--- a/lib/tests/phpunit/LanguageWithConversionTest.php
+++ b/lib/tests/phpunit/LanguageWithConversionTest.php
@@ -119,18 +119,31 @@
/**
* @dataProvider provideTranslate
*/
- public function testTranslateBatched( $langCode, $sourceLangCode,
$translations ) {
- $obj = LanguageWithConversion::factory( $langCode,
$sourceLangCode );
- foreach ( $translations as $text => $translatedText ) {
- $obj->prepareForTranslate( $text );
+ public function testReverseTranslate( $langCode, $sourceLangCode,
$translations ) {
+ if ( $sourceLangCode === null ) {
+ $sourceLangCode = $langCode;
+ $langCode = null;
}
+ $obj = LanguageWithConversion::factory( $sourceLangCode,
$langCode );
foreach ( $translations as $text => $translatedText ) {
- $this->assertEquals( $obj->translate( $text ),
$translatedText );
+ $this->assertEquals( $obj->reverseTranslate( $text ),
$translatedText );
}
}
public function provideTranslate() {
return array(
+ array( 'de', null, array(
+ 'foo' => 'foo',
+ 'bar' => 'bar',
+ ) ),
+ array( 'zh', null, array(
+ '測試' => '測試',
+ '测试' => '测试',
+ ) ),
+ array( 'zh-cn', null, array(
+ '測試' => '測試',
+ '测试' => '测试',
+ ) ),
array( 'zh-cn', 'zh-tw', array(
'測試' => '测试',
) ),
--
To view, visit https://gerrit.wikimedia.org/r/73754
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I38298ceb608edd50600b93176b2e88c75b0335e6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Liangent <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits