XliffFFS.php
Go to the documentation of this file.00001 <?php
00015 class XliffFFS extends SimpleFFS {
00016 public static function isValid( $data ) {
00017 $doc = new DomDocument( '1.0' );
00018 $doc->loadXML( $data );
00019
00020 $errors = libxml_get_errors();
00021 if ( $errors ) {
00022 return false;
00023 }
00024
00025 $schema = __DIR__ . '/../data/xliff-core-1.2-transitional.xsd';
00026 if ( !$doc->schemaValidate( $schema ) ) {
00027 return false;
00028 }
00029
00030 return true;
00031 }
00032
00033 public function getFileExtensions() {
00034 return array( '.xlf', '.xliff', '.xml' );
00035 }
00036
00037 public function readFromVariable( $data, $element = 'target' ) {
00038
00039 $messages = array();
00040 $mangler = $this->group->getMangler();
00041
00042 $reader = new SimpleXMLElement( $data );
00043 $reader->registerXPathNamespace(
00044 'xliff',
00045 'urn:oasis:names:tc:xliff:document:1.2'
00046 );
00047
00048 foreach ( $reader->xpath( '//xliff:trans-unit' ) as $item ) {
00049
00050 $source = $item->$element;
00051
00052 if ( !$source ) {
00053 continue;
00054 }
00055
00056 $key = (string)$item['id'];
00057
00058
00059
00060 $dom = new DOMDocument( '1.0' );
00061 $dom->loadXML( $source->asXml() );
00062 $value = self::getInnerXml( $dom->documentElement );
00063
00064
00065
00066
00067
00068 if ( (string)$source['state'] === 'needs-l10n' ) {
00069 $value = TRANSLATE_FUZZY . $value;
00070 }
00071
00072
00073 $value = preg_replace( '/<!\[CDATA\[(.*?)\]\]>/s', '\1', $value );
00074
00075 $messages[$key] = $value;
00076 }
00077
00078 return array(
00079 'MESSAGES' => $mangler->mangle( $messages ),
00080 );
00081 }
00082
00083 public function read( $code ) {
00084 if ( !$this->exists( $code ) ) {
00085 return false;
00086 }
00087
00088 $filename = $this->group->getSourceFilePath( $code );
00089 $input = file_get_contents( $filename );
00090 if ( $input === false ) {
00091 throw new MWException( "Unable to read file $filename." );
00092 }
00093
00094 $element = $code === $this->group->getSourceLanguage() ? 'source' : 'target';
00095
00096 return $this->readFromVariable( $input, $element );
00097 }
00098
00102 public static function getInnerXml( DomElement $node ) {
00103 $text = '';
00104 foreach ( $node->childNodes as $child ) {
00105 $text .= $child->ownerDocument->saveXML( $child );
00106 }
00107
00108 return $text;
00109 }
00110
00111 protected function writeReal( MessageCollection $collection ) {
00112 $mangler = $this->group->getMangler();
00113
00114 $template = new DomDocument( '1.0' );
00115 $template->preserveWhiteSpace = false;
00116 $template->formatOutput = true;
00117
00118
00119 $sourceLanguage = $this->group->getSourceLanguage();
00120 $sourceFile = $this->group->getSourceFilePath( $sourceLanguage );
00121 if ( file_exists( $sourceFile ) ) {
00122 $template->load( $sourceFile );
00123 } else {
00124
00125 $template->load( __DIR__ . '/../data/xliff-template.xml' );
00126 }
00127
00128 $list = $template->getElementsByTagName( 'body' )->item( 0 );
00129 $list->nodeValue = null;
00130
00131 foreach ( $collection as $key => $m ) {
00132 $key = $mangler->unmangle( $key );
00133
00134 $value = $m->translation();
00135 $value = str_replace( TRANSLATE_FUZZY, '', $value );
00136
00137
00138 $source = $template->createDocumentFragment();
00139 $source->appendXML( htmlspecialchars( $m->definition() ) );
00140
00141 $target = $template->createDocumentFragment();
00142 $target->appendXML( htmlspecialchars( $value ) );
00143
00144 $sourceElement = $template->createElement( 'source' );
00145 $sourceElement->appendChild( $source );
00146
00147 $targetElement = $template->createElement( 'target' );
00148 $targetElement->appendChild( $target );
00149 if ( $m->getProperty( 'status' ) === 'fuzzy' ) {
00150 $targetElement->setAttribute( 'state', 'needs-l10n' );
00151 }
00152 if ( $m->getProperty( 'status' ) === 'proofread' ) {
00153 $targetElement->setAttribute( 'state', 'signed-off' );
00154 }
00155
00156 $transUnit = $template->createElement( 'trans-unit' );
00157 $transUnit->setAttribute( 'id', $key );
00158 $transUnit->appendChild( $sourceElement );
00159 $transUnit->appendChild( $targetElement );
00160
00161 $list->appendChild( $transUnit );
00162 }
00163
00164 $template->encoding = 'UTF-8';
00165
00166 return $template->saveXML();
00167 }
00168
00169 public function supportsFuzzy() {
00170 return 'yes';
00171 }
00172 }