Siebrand has uploaded a new change for review. https://gerrit.wikimedia.org/r/70602
Change subject: Basic support for Apache Cocoon i18n format ...................................................................... Basic support for Apache Cocoon i18n format This is a draft. Change-Id: I72491096d45950a90bcf61aab01e9a392b0e9bd4 --- M _autoload.php A ffs/ApacheCocoonXmlFFS.php A tests/ffs/ApacheCocoonXmlFFSTest.php 3 files changed, 146 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate refs/changes/02/70602/1 diff --git a/_autoload.php b/_autoload.php index a8c0280..2b8eeb2 100644 --- a/_autoload.php +++ b/_autoload.php @@ -170,6 +170,7 @@ * @{ */ $wgAutoloadClasses['AndroidXmlFFS'] = "$dir/ffs/AndroidXmlFFS.php"; +$wgAutoloadClasses['ApacheCocoonXmlFFS'] = "$dir/ffs/ApacheCocoonXmlFFS.php"; $wgAutoloadClasses['DtdFFS'] = "$dir/ffs/DtdFFS.php"; $wgAutoloadClasses['FFS'] = "$dir/ffs/FFS.php"; $wgAutoloadClasses['FlatPhpFFS'] = "$dir/ffs/FlatPhpFFS.php"; diff --git a/ffs/ApacheCocoonXmlFFS.php b/ffs/ApacheCocoonXmlFFS.php new file mode 100644 index 0000000..e958a44 --- /dev/null +++ b/ffs/ApacheCocoonXmlFFS.php @@ -0,0 +1,78 @@ +<?php +/** + * Support for XML translation format used by Apache Cocoon. + * + * @file + * @author Siebrand Mazeland + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ + +/** + * Support for Apache Cocoon translation format. + * @since 2013-06-26 + * @ingroup FFS + */ +class ApacheCocoonXmlFFS extends AndroidXmlFFS { + public function readFromVariable( $data ) { + $reader = new SimpleXMLElement( $data ); + + $messages = array(); + $mangler = $this->group->getMangler(); + + foreach ( $reader->string as $string ) { + $key = (string)$string['key']; + $value = stripcslashes( (string)$string ); + + if ( isset( $string['fuzzy'] ) && (string)$string['fuzzy'] === 'true' ) { + $value = TRANSLATE_FUZZY . $value; + } + + $messages[$key] = $value; + } + + return array( + 'AUTHORS' => array(), // @todo + 'MESSAGES' => $mangler->mangle( $messages ), + ); + } + + protected function writeReal( MessageCollection $collection ) { + $langCode = $collection->getLanguage(); + $template = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; + $template .= "<catalogue xml:lang=\"\" xmlns:18n=\"http://apache.org/cocoon/i18n/2.1\"></catalogue>\n"; + + $writer = new SimpleXMLElement( $template ); + $mangler = $this->group->getMangler(); + + $collection->filter( 'hastranslation', false ); + if ( count( $collection ) === 0 ) { + return ''; + } + + /** + * @var $m TMessage + */ + foreach ( $collection as $key => $m ) { + $key = $mangler->unmangle( $key ); + + $value = $m->translation(); + $value = str_replace( TRANSLATE_FUZZY, '', $value ); + + // Kudos to the brilliant person who invented this braindead file format + $string = $writer->addChild( 'string', addcslashes( $value, '"\'' ) ); + $string->addAttribute( 'name', $key ); + + // This is non-standard + if ( $m->hasTag( 'fuzzy' ) ) { + $string->addAttribute( 'fuzzy', 'true' ); + } + } + + // Make the output pretty with DOMDocument + $dom = new DOMDocument( '1.0' ); + $dom->formatOutput = true; + $dom->loadXML( $writer->asXML() ); + + return $dom->saveXML(); + } +} diff --git a/tests/ffs/ApacheCocoonXmlFFSTest.php b/tests/ffs/ApacheCocoonXmlFFSTest.php new file mode 100644 index 0000000..5691106 --- /dev/null +++ b/tests/ffs/ApacheCocoonXmlFFSTest.php @@ -0,0 +1,67 @@ +<?php +/** + * Tests for ApacheCocoonXmlFFS + * + * @file + * @author Siebrand Mazeland + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ + +class ApacheCocoonXmlFFSTest extends AndroidXmlFFSTest { + protected $groupConfiguration = array( + 'BASIC' => array( + 'class' => 'FileBasedMessageGroup', + 'id' => 'test-id', + 'label' => 'Test Label', + 'namespace' => 'NS_MEDIAWIKI', + 'description' => 'Test description', + ), + 'FILES' => array( + 'class' => 'ApacheCocoonXmlFFS', + 'sourcePattern' => '', + ), + ); + + public function testParsing() { + $file = +<<<XML +<?xml version="1.0" encoding="utf-8"?> +<catalogue xml:lang="fi" xmlns:18n="http://apache.org/cocoon/i18n/2.1"> + <message key="wpt_voicerec">Voice recording</message> + <message key="wpt_stillimage" fuzzy="true">Picture</message> +</resources> +XML; + + /** + * @var FileBasedMessageGroup $group + */ + $group = MessageGroupBase::factory( $this->groupConfiguration ); + $ffs = new ApacheCocoonXmlFFS( $group ); + $parsed = $ffs->readFromVariable( $file ); + $expected = array( + 'wpt_voicerec' => 'Voice recording', + 'wpt_stillimage' => '!!FUZZY!!Picture', + ); + $expected = array( 'MESSAGES' => $expected, 'AUTHORS' => array() ); + $this->assertEquals( $expected, $parsed ); + } + + public function testWrite() { + /** + * @var FileBasedMessageGroup $group + */ + $group = MessageGroupBase::factory( $this->groupConfiguration ); + $ffs = new AndroidXmlFFS( $group ); + + $messages = array( + 'ko=26ra' => 'wawe', + 'foobar' => '!!FUZZY!!Kissa kala <koira> "a\'b', + ); + $collection = new MockMessageCollection( $messages ); + + $xml = $ffs->writeIntoVariable( $collection ); + $parsed = $ffs->readFromVariable( $xml ); + $expected = array( 'MESSAGES' => $messages, 'AUTHORS' => array() ); + $this->assertEquals( $expected, $parsed ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/70602 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I72491096d45950a90bcf61aab01e9a392b0e9bd4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Translate Gerrit-Branch: master Gerrit-Owner: Siebrand <siebr...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits