Exemplo n.º 1
0
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar', 'key' => '', 'key.with.cdata' => '<source> & <target>'));
     $catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
     $catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
     $tempDir = sys_get_temp_dir();
     $dumper = new XliffFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources-clean.xlf'), file_get_contents($tempDir . '/messages.en_US.xlf'));
     unlink($tempDir . '/messages.en_US.xlf');
 }
Exemplo n.º 2
0
 public function testGetResultWithMetadata()
 {
     $leftCatalogue = new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b')));
     $leftCatalogue->setMetadata('a', 'foo', 'messages');
     $leftCatalogue->setMetadata('b', 'bar', 'messages');
     $rightCatalogue = new MessageCatalogue('en', array('messages' => array('b' => 'new_b', 'c' => 'new_c')));
     $rightCatalogue->setMetadata('b', 'baz', 'messages');
     $rightCatalogue->setMetadata('c', 'qux', 'messages');
     $diffCatalogue = new MessageCatalogue('en', array('messages' => array('b' => 'old_b', 'c' => 'new_c')));
     $diffCatalogue->setMetadata('b', 'bar', 'messages');
     $diffCatalogue->setMetadata('c', 'qux', 'messages');
     $this->assertEquals($diffCatalogue, $this->createOperation($leftCatalogue, $rightCatalogue)->getResult());
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  *
  * @api
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     if (!stream_is_local($resource)) {
         throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
     }
     if (!file_exists($resource)) {
         throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
     }
     list($xml, $encoding) = $this->parseFile($resource);
     $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
     $catalogue = new MessageCatalogue($locale);
     foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
         $attributes = $translation->attributes();
         if (!(isset($attributes['resname']) || isset($translation->source))) {
             continue;
         }
         $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
         // If the xlf file has another encoding specified, try to convert it because
         // simple_xml will always return utf-8 encoded values
         $target = $this->utf8ToCharset((string) (isset($translation->target) ? $translation->target : $source), $encoding);
         $catalogue->set((string) $source, $target, $domain);
         $metadata = array();
         if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
             $metadata['notes'] = $notes;
         }
         if ($translation->target->attributes()) {
             $metadata['target-attributes'] = $translation->target->attributes();
         }
         $catalogue->setMetadata((string) $source, $metadata, $domain);
     }
     if (class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) {
         $catalogue->addResource(new FileResource($resource));
     }
     return $catalogue;
 }
 /**
  * Load translations and metadata of the trans-unit.
  *
  * {@inheritdoc}
  *
  * @api
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     /* @var MessageCatalogue $catalogue */
     $base_catalogue = parent::load($resource, $locale, $domain);
     $catalogue = new MessageCatalogue($locale);
     $catalogue->addCatalogue($base_catalogue);
     // Process a second pass over the file to collect metadata
     $xml = simplexml_load_file($resource);
     $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
     foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
         // Read the attributes
         $attributes = (array) $translation->attributes();
         $attributes = $attributes['@attributes'];
         if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
             continue;
         }
         $key = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
         $metadata = (array) $attributes;
         // read the notes
         if (isset($translation->note)) {
             $metadata['note'] = (string) $translation->note;
         }
         $catalogue->setMetadata((string) $key, $metadata, $domain);
     }
     return $catalogue;
 }
Exemplo n.º 5
0
    public function testTargetAttributesMetadataIsSetInFile()
    {
        $catalogue = new MessageCatalogue('en_US');
        $catalogue->add(array('foo' => 'bar'));
        $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
        $tempDir = sys_get_temp_dir();
        $dumper = new XliffFileDumper();
        $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
        $content = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
  <file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
    <body>
      <trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
        <source>foo</source>
        <target state="needs-translation">bar</target>
      </trans-unit>
    </body>
  </file>
</xliff>

EOT;
        $this->assertEquals($content, file_get_contents($tempDir . '/messages.en_US.xlf'));
        unlink($tempDir . '/messages.en_US.xlf');
    }
Exemplo n.º 6
0
 public function testFormatCatalogueWithTargetAttributesMetadata()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar'));
     $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
     $dumper = new XliffFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/resources-target-attributes.xlf', $dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR')));
 }
Exemplo n.º 7
0
 public function testDumpWithTargetAttributesMetadata()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar'));
     $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
     $this->tempDir = sys_get_temp_dir();
     $dumper = new XliffFileDumper();
     $dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources-target-attributes.xlf'), file_get_contents($this->tempDir . '/messages.en_US.xlf'));
     unlink($this->tempDir . '/messages.en_US.xlf');
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  *
  * @api
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     if (!stream_is_local($resource)) {
         throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
     }
     if (!file_exists($resource)) {
         throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
     }
     list($xml, $encoding) = $this->parseFile($resource);
     $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
     $catalogue = new MessageCatalogue($locale);
     foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
         $attributes = $translation->attributes();
         if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
             continue;
         }
         $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
         // If the xlf file has another encoding specified, try to convert it because
         // simple_xml will always return utf-8 encoded values
         $target = $this->utf8ToCharset((string) $translation->target, $encoding);
         $catalogue->set((string) $source, $target, $domain);
         if (isset($translation->note)) {
             $notes = array();
             foreach ($translation->note as $xmlNote) {
                 $noteAttributes = $xmlNote->attributes();
                 $note = array('content' => $this->utf8ToCharset((string) $xmlNote, $encoding));
                 if (isset($noteAttributes['priority'])) {
                     $note['priority'] = (int) $noteAttributes['priority'];
                 }
                 if (isset($noteAttributes['from'])) {
                     $note['from'] = (string) $noteAttributes['from'];
                 }
                 $notes[] = $note;
             }
             $catalogue->setMetadata((string) $source, array('notes' => $notes), $domain);
         }
     }
     $catalogue->addResource(new FileResource($resource));
     return $catalogue;
 }
Exemplo n.º 9
0
 public function testMetadataMerge()
 {
     $cat1 = new MessageCatalogue('en');
     $cat1->setMetadata('a', 'b');
     $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
     $cat2 = new MessageCatalogue('en');
     $cat2->setMetadata('b', 'c', 'domain');
     $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
     $cat1->addCatalogue($cat2);
     $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
 }
Exemplo n.º 10
0
 private function filterCatalogue(MessageCatalogue $catalogue, $domain)
 {
     $filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
     if ($messages = $catalogue->all($domain)) {
         $filteredCatalogue->add($messages, $domain);
     }
     foreach ($catalogue->getResources() as $resource) {
         $filteredCatalogue->addResource($resource);
     }
     if ($metadata = $catalogue->getMetadata('', $domain)) {
         foreach ($metadata as $k => $v) {
             $filteredCatalogue->setMetadata($k, $v, $domain);
         }
     }
     return $filteredCatalogue;
 }
Exemplo n.º 11
0
 /**
  * @param \DOMDocument     $dom
  * @param MessageCatalogue $catalogue
  * @param string           $domain
  */
 private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
 {
     $xml = simplexml_import_dom($dom);
     $encoding = strtoupper($dom->encoding);
     $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
     foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
         $source = $segment->source;
         // If the xlf file has another encoding specified, try to convert it because
         // simple_xml will always return utf-8 encoded values
         $target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
         $catalogue->set((string) $source, $target, $domain);
         $metadata = array();
         if (isset($segment->target) && $segment->target->attributes()) {
             $metadata['target-attributes'] = array();
             foreach ($segment->target->attributes() as $key => $value) {
                 $metadata['target-attributes'][$key] = (string) $value;
             }
         }
         $catalogue->setMetadata((string) $source, $metadata, $domain);
     }
 }