Siebrand has submitted this change and it was merged. Change subject: Very basic XliffFFS ......................................................................
Very basic XliffFFS Change-Id: Ide12fe3bcc14ed9985ed3d003ba063c8ca9a2e44 --- M _autoload.php A data/xliff-template.xml A ffs/XliffFFS.php A tests/data/minimal.xlf A tests/ffs/XliffFFSTest.php 5 files changed, 216 insertions(+), 0 deletions(-) Approvals: Siebrand: Looks good to me, approved jenkins-bot: Verified diff --git a/_autoload.php b/_autoload.php index 5788ec1..88dc03f 100644 --- a/_autoload.php +++ b/_autoload.php @@ -181,6 +181,7 @@ $wgAutoloadClasses['RubyYamlFFS'] = "$dir/ffs/RubyYamlFFS.php"; $wgAutoloadClasses['ShapadoJsFFS'] = "$dir/ffs/JavaScriptFFS.php"; $wgAutoloadClasses['SimpleFFS'] = "$dir/ffs/FFS.php"; +$wgAutoloadClasses['XliffFFS'] = "$dir/ffs/XliffFFS.php"; $wgAutoloadClasses['YamlFFS'] = "$dir/ffs/YamlFFS.php"; /**@}*/ diff --git a/data/xliff-template.xml b/data/xliff-template.xml new file mode 100644 index 0000000..379a402 --- /dev/null +++ b/data/xliff-template.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<xliff version="1.2"> + <file source-language="EN" datatype="plaintext" original="unknown"> + <body> + </body> + </file> +</xliff> diff --git a/ffs/XliffFFS.php b/ffs/XliffFFS.php new file mode 100644 index 0000000..5098e50 --- /dev/null +++ b/ffs/XliffFFS.php @@ -0,0 +1,140 @@ +<?php +/** + * Partial support for the Xliff translation format. + * + * @file + * @author Niklas Laxström + * @license GPL2+ + */ + +/** + * Partial support for the Xliff translation format. + * @since 2013-04 + * @ingroup FFS + */ +class XliffFFS extends SimpleFFS { + public function readFromVariable( $data, $element = 'target' ) { + + $messages = array(); + $mangler = $this->group->getMangler(); + + $reader = new SimpleXMLElement( $data ); + foreach ( $reader->xpath( '//trans-unit' ) as $item ) { + $source = $item->$element; + + if ( !$source ) { + continue; + } + + $key = (string)$item['id']; + + /* In case there are tags inside the element, preserve + * them. */ + $dom = new DOMDocument( '1.0' ); + $dom->loadXML( $source->asXml() ); + $value = self::getInnerXml( $dom->documentElement ); + + /* This might not be 100% according to the spec, but + * for now if there is explicit approved=no, mark it + * as fuzzy, but don't do that if the attribute is not + * set */ + if ( (string)$source['state'] === 'needs-l10n' ) { + $value = TRANSLATE_FUZZY . $value; + } + + $messages[$key] = $value; + } + + return array( + 'MESSAGES' => $mangler->mangle( $messages ), + ); + } + + public function read( $code ) { + if ( !$this->exists( $code ) ) { + return false; + } + + $filename = $this->group->getSourceFilePath( $code ); + $input = file_get_contents( $filename ); + if ( $input === false ) { + throw new MWException( "Unable to read file $filename." ); + } + + $element = $code === $this->group->getSourceLanguage() ? 'source' : 'target'; + + return $this->readFromVariable( $input, $element ); + } + + /** + * Gets the html inside en element without the element itself. + */ + public static function getInnerXml( DomElement $node ) { + $text = ''; + foreach ( $node->childNodes as $child ) { + $text .= $child->ownerDocument->saveXML( $child ); + } + return $text; + } + + protected function writeReal( MessageCollection $collection ) { + $mangler = $this->group->getMangler(); + + $template = new DomDocument( '1.0' ); + $template->preserveWhiteSpace = false; + $template->formatOutput = true; + + // Try to use the definition file as template + $sourceLanguage = $this->group->getSourceLanguage(); + $sourceFile = $this->group->getSourceFilePath( $sourceLanguage ); + if ( file_exists( $sourceFile ) ) { + $template->load( $sourceFile ); + } else { + // Else use standard template + $template->load( __DIR__ . '/../data/xliff-template.xml' ); + } + + $list = $template->getElementsByTagName( 'body' )->item( 0 ); + $list->nodeValue = null; + + foreach ( $collection as $key => $m ) { + $key = $mangler->unmangle( $key ); + + $value = $m->translation(); + $value = str_replace( TRANSLATE_FUZZY, '', $value ); + + // @todo Support placeholder tags etc. + $source = $template->createDocumentFragment(); + $source->appendXML( htmlspecialchars( $m->definition() ) ); + + $target = $template->createDocumentFragment(); + $target->appendXML( htmlspecialchars( $value ) ); + + $sourceElement = $template->createElement( 'source' ); + $sourceElement->appendChild( $source ); + + $targetElement = $template->createElement( 'target' ); + $targetElement->appendChild( $target ); + if ( $m->getProperty( 'status' ) === 'fuzzy' ) { + $targetElement->setAttribute( 'state', 'needs-l10n' ); + } + if ( $m->getProperty( 'status' ) === 'proofread' ) { + $targetElement->setAttribute( 'state', 'signed-off' ); + } + + $transUnit = $template->createElement( 'trans-unit' ); + $transUnit->setAttribute( 'id', $key ); + $transUnit->appendChild( $sourceElement ); + $transUnit->appendChild( $targetElement ); + + $list->appendChild( $transUnit ); + } + + $template->encoding = 'UTF-8'; + return $template->saveXML(); + } + + public function supportsFuzzy() { + return 'yes'; + } +} diff --git a/tests/data/minimal.xlf b/tests/data/minimal.xlf new file mode 100644 index 0000000..ed6b844 --- /dev/null +++ b/tests/data/minimal.xlf @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<xliff version="1.2"> +<file source-language="EN" datatype="plaintext" original="file.ext"> +<body> +<trans-unit id="1"> +<source>Hello world</source> +<target>Hei maailma</target> +</trans-unit> +<trans-unit id="2"> +<source>Fuzzy message</source> +<target state="needs-l10n">Fuzzy translation</target> +</trans-unit> +<trans-unit id="3"> +<source>This is <g id="1" ctype="bold">bold</g>.</source> +<target>Tämä on <g id="1" ctype="bold">paksu</g>.</target> +</trans-unit> +</body> +</file> +</xliff> diff --git a/tests/ffs/XliffFFSTest.php b/tests/ffs/XliffFFSTest.php new file mode 100644 index 0000000..70943af --- /dev/null +++ b/tests/ffs/XliffFFSTest.php @@ -0,0 +1,49 @@ +<?php +/** + * Tests for XliffFFS + * + * @file + * @author Niklas Laxström + * @license GPL2+ + */ + +class XliffFFSTest extends MediaWikiTestCase { + protected $groupConfiguration = array( + 'BASIC' => array( + 'class' => 'FileBasedMessageGroup', + 'id' => 'test-id', + 'label' => 'Test Label', + 'namespace' => 'NS_MEDIAWIKI', + 'description' => 'Test description', + ), + 'FILES' => array( + 'class' => 'XliffFFS', + 'sourcePattern' => '', + ), + ); + + + public function testParsing() { + $group = MessageGroupBase::factory( $this->groupConfiguration ); + $ffs = new XliffFFS( $group ); + + $file = file_get_contents( __DIR__ . '/../data/minimal.xlf' ); + $parsed = $ffs->readFromVariable( $file, 'target' ); + $expected = array( + '1' => 'Hei maailma', + '2' => TRANSLATE_FUZZY . 'Fuzzy translation', + '3' => 'Tämä on <g id="1" ctype="bold">paksu</g>.', + ); + $expected = array( 'MESSAGES' => $expected ); + $this->assertEquals( $expected, $parsed ); + + $parsed = $ffs->readFromVariable( $file, 'source' ); + $expected = array( + '1' => 'Hello world', + '2' => 'Fuzzy message', + '3' => 'This is <g id="1" ctype="bold">bold</g>.', + ); + $expected = array( 'MESSAGES' => $expected ); + $this->assertEquals( $expected, $parsed ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/57842 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ide12fe3bcc14ed9985ed3d003ba063c8ca9a2e44 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Translate Gerrit-Branch: master Gerrit-Owner: Nikerabbit <niklas.laxst...@gmail.com> Gerrit-Reviewer: Siebrand <siebr...@wikimedia.org> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits