2011-08-28 18:12:35 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Translation\Dumper ;
use Symfony\Component\Translation\MessageCatalogue ;
/**
2011-08-29 18:22:52 +01:00
* XliffFileDumper generates xliff files from a message catalogue .
2011-08-28 18:12:35 +01:00
*
* @ author Michel Salib < michelsalib @ hotmail . com >
*/
2011-08-29 18:22:52 +01:00
class XliffFileDumper extends FileDumper
2011-08-28 18:12:35 +01:00
{
2014-06-29 10:53:14 +01:00
/**
* { @ inheritdoc }
*/
2015-09-18 17:59:19 +01:00
public function formatCatalogue ( MessageCatalogue $messages , $domain , array $options = array ())
2014-06-29 10:53:14 +01:00
{
2015-09-08 11:19:33 +01:00
$xliffVersion = '1.2' ;
if ( array_key_exists ( 'xliff_version' , $options )) {
$xliffVersion = $options [ 'xliff_version' ];
}
2014-06-29 10:53:14 +01:00
if ( array_key_exists ( 'default_locale' , $options )) {
2015-08-14 12:14:57 +01:00
$defaultLocale = $options [ 'default_locale' ];
2014-07-11 10:42:41 +01:00
} else {
2015-08-14 12:14:57 +01:00
$defaultLocale = \Locale :: getDefault ();
2014-06-29 10:53:14 +01:00
}
2015-09-08 11:19:33 +01:00
if ( '1.2' === $xliffVersion ) {
return $this -> dumpXliff1 ( $defaultLocale , $messages , $domain , $options );
}
if ( '2.0' === $xliffVersion ) {
return $this -> dumpXliff2 ( $defaultLocale , $messages , $domain , $options );
}
throw new \InvalidArgumentException ( sprintf ( 'No support implemented for dumping XLIFF version "%s".' , $xliffVersion ));
}
/**
* { @ inheritdoc }
*/
protected function format ( MessageCatalogue $messages , $domain )
{
2017-12-31 04:48:26 +00:00
@ trigger_error ( 'The ' . __METHOD__ . ' method is deprecated since Symfony 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.' , E_USER_DEPRECATED );
2015-09-05 16:43:58 +01:00
2015-09-08 11:19:33 +01:00
return $this -> formatCatalogue ( $messages , $domain );
}
/**
* { @ inheritdoc }
*/
protected function getExtension ()
{
return 'xlf' ;
}
private function dumpXliff1 ( $defaultLocale , MessageCatalogue $messages , $domain , array $options = array ())
{
2015-08-14 12:14:57 +01:00
$toolInfo = array ( 'tool-id' => 'symfony' , 'tool-name' => 'Symfony' );
if ( array_key_exists ( 'tool_info' , $options )) {
$toolInfo = array_merge ( $toolInfo , $options [ 'tool_info' ]);
}
2014-06-29 10:53:14 +01:00
2011-08-28 18:12:35 +01:00
$dom = new \DOMDocument ( '1.0' , 'utf-8' );
$dom -> formatOutput = true ;
2013-03-08 14:35:32 +00:00
2011-08-28 18:12:35 +01:00
$xliff = $dom -> appendChild ( $dom -> createElement ( 'xliff' ));
$xliff -> setAttribute ( 'version' , '1.2' );
$xliff -> setAttribute ( 'xmlns' , 'urn:oasis:names:tc:xliff:document:1.2' );
2013-03-08 14:35:32 +00:00
2011-08-28 18:12:35 +01:00
$xliffFile = $xliff -> appendChild ( $dom -> createElement ( 'file' ));
2015-08-14 12:14:57 +01:00
$xliffFile -> setAttribute ( 'source-language' , str_replace ( '_' , '-' , $defaultLocale ));
2014-07-11 10:42:41 +01:00
$xliffFile -> setAttribute ( 'target-language' , str_replace ( '_' , '-' , $messages -> getLocale ()));
2011-08-28 18:12:35 +01:00
$xliffFile -> setAttribute ( 'datatype' , 'plaintext' );
$xliffFile -> setAttribute ( 'original' , 'file.ext' );
2013-03-08 14:35:32 +00:00
2015-08-14 12:14:57 +01:00
$xliffHead = $xliffFile -> appendChild ( $dom -> createElement ( 'header' ));
$xliffTool = $xliffHead -> appendChild ( $dom -> createElement ( 'tool' ));
foreach ( $toolInfo as $id => $value ) {
$xliffTool -> setAttribute ( $id , $value );
}
2011-08-28 18:12:35 +01:00
$xliffBody = $xliffFile -> appendChild ( $dom -> createElement ( 'body' ));
foreach ( $messages -> all ( $domain ) as $source => $target ) {
2013-03-08 14:35:32 +00:00
$translation = $dom -> createElement ( 'trans-unit' );
$translation -> setAttribute ( 'id' , md5 ( $source ));
$translation -> setAttribute ( 'resname' , $source );
$s = $translation -> appendChild ( $dom -> createElement ( 'source' ));
2011-08-28 18:12:35 +01:00
$s -> appendChild ( $dom -> createTextNode ( $source ));
2013-03-08 14:35:32 +00:00
2014-07-29 20:37:35 +01:00
// Does the target contain characters requiring a CDATA section?
2014-09-16 06:12:27 +01:00
$text = 1 === preg_match ( '/[&<>]/' , $target ) ? $dom -> createCDATASection ( $target ) : $dom -> createTextNode ( $target );
2014-07-29 20:37:35 +01:00
2015-08-04 20:28:45 +01:00
$targetElement = $dom -> createElement ( 'target' );
$metadata = $messages -> getMetadata ( $source , $domain );
if ( $this -> hasMetadataArrayInfo ( 'target-attributes' , $metadata )) {
foreach ( $metadata [ 'target-attributes' ] as $name => $value ) {
$targetElement -> setAttribute ( $name , $value );
}
}
$t = $translation -> appendChild ( $targetElement );
2014-07-29 20:37:35 +01:00
$t -> appendChild ( $text );
2013-03-08 14:35:32 +00:00
2015-08-04 20:28:45 +01:00
if ( $this -> hasMetadataArrayInfo ( 'notes' , $metadata )) {
2014-05-18 15:06:37 +01:00
foreach ( $metadata [ 'notes' ] as $note ) {
if ( ! isset ( $note [ 'content' ])) {
continue ;
}
$n = $translation -> appendChild ( $dom -> createElement ( 'note' ));
$n -> appendChild ( $dom -> createTextNode ( $note [ 'content' ]));
if ( isset ( $note [ 'priority' ])) {
$n -> setAttribute ( 'priority' , $note [ 'priority' ]);
}
if ( isset ( $note [ 'from' ])) {
$n -> setAttribute ( 'from' , $note [ 'from' ]);
}
}
}
2013-03-08 14:35:32 +00:00
$xliffBody -> appendChild ( $translation );
2011-08-28 18:12:35 +01:00
}
return $dom -> saveXML ();
}
2011-10-29 11:05:45 +01:00
2015-09-08 11:19:33 +01:00
private function dumpXliff2 ( $defaultLocale , MessageCatalogue $messages , $domain , array $options = array ())
2015-08-14 12:14:57 +01:00
{
2015-09-08 11:19:33 +01:00
$dom = new \DOMDocument ( '1.0' , 'utf-8' );
$dom -> formatOutput = true ;
2015-08-14 12:14:57 +01:00
2015-09-08 11:19:33 +01:00
$xliff = $dom -> appendChild ( $dom -> createElement ( 'xliff' ));
$xliff -> setAttribute ( 'xmlns' , 'urn:oasis:names:tc:xliff:document:2.0' );
$xliff -> setAttribute ( 'version' , '2.0' );
$xliff -> setAttribute ( 'srcLang' , str_replace ( '_' , '-' , $defaultLocale ));
$xliff -> setAttribute ( 'trgLang' , str_replace ( '_' , '-' , $messages -> getLocale ()));
$xliffFile = $xliff -> appendChild ( $dom -> createElement ( 'file' ));
$xliffFile -> setAttribute ( 'id' , $domain . '.' . $messages -> getLocale ());
foreach ( $messages -> all ( $domain ) as $source => $target ) {
$translation = $dom -> createElement ( 'unit' );
$translation -> setAttribute ( 'id' , md5 ( $source ));
$segment = $translation -> appendChild ( $dom -> createElement ( 'segment' ));
$s = $segment -> appendChild ( $dom -> createElement ( 'source' ));
$s -> appendChild ( $dom -> createTextNode ( $source ));
// Does the target contain characters requiring a CDATA section?
$text = 1 === preg_match ( '/[&<>]/' , $target ) ? $dom -> createCDATASection ( $target ) : $dom -> createTextNode ( $target );
$targetElement = $dom -> createElement ( 'target' );
$metadata = $messages -> getMetadata ( $source , $domain );
if ( $this -> hasMetadataArrayInfo ( 'target-attributes' , $metadata )) {
foreach ( $metadata [ 'target-attributes' ] as $name => $value ) {
$targetElement -> setAttribute ( $name , $value );
}
}
$t = $segment -> appendChild ( $targetElement );
$t -> appendChild ( $text );
$xliffFile -> appendChild ( $translation );
}
return $dom -> saveXML ();
2011-08-29 18:22:52 +01:00
}
2015-08-04 20:28:45 +01:00
/**
* @ param string $key
* @ param array | null $metadata
*
* @ return bool
*/
private function hasMetadataArrayInfo ( $key , $metadata = null )
{
2018-07-05 12:24:53 +01:00
return null !== $metadata && array_key_exists ( $key , $metadata ) && ( $metadata [ $key ] instanceof \Traversable || \is_array ( $metadata [ $key ]));
2015-08-04 20:28:45 +01:00
}
2011-08-28 18:12:35 +01:00
}