/** * {@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)); } try { $dom = XmlUtils::loadFile($resource); } catch (\InvalidArgumentException $e) { throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e); } $internalErrors = libxml_use_internal_errors(true); libxml_clear_errors(); $xpath = new \DOMXPath($dom); $nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]'); $catalogue = new MessageCatalogue($locale); if ($nodes->length == 1) { $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); foreach ($translations as $translation) { $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue; if (!empty($translationValue)) { $catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, $translationValue, $domain); } $translation = $translation->nextSibling; } $catalogue->addResource(new FileResource($resource)); } libxml_use_internal_errors($internalErrors); return $catalogue; }
/** * {@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; $target = (string) $translation->target; // If the xlf file has another encoding specified, try to convert it because // simple_xml will always return utf-8 encoded values if ('UTF-8' !== $encoding && !empty($encoding)) { if (function_exists('mb_convert_encoding')) { $target = mb_convert_encoding($target, $encoding, 'UTF-8'); } elseif (function_exists('iconv')) { $target = iconv('UTF-8', $encoding, $target); } else { throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).'); } } $catalogue->set((string) $source, $target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} */ function load($resource, $locale, $domain = 'messages') { $catalogue = new MessageCatalogue($locale); $catalogue->addMessages(require $resource, $domain); $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * * {@inheritdoc} * */ 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 (!is_dir($resource)) { throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } try { $rb = new \ResourceBundle($locale, $resource); } catch (\Exception $e) { // HHVM compatibility: constructor throws on invalid resource $rb = null; } if (!$rb) { throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource)); } elseif (intl_is_failure($rb->getErrorCode())) { throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode()); } $messages = $this->flatten($rb); $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); if (class_exists('Symfony\\Component\\Config\\Resource\\DirectoryResource')) { $catalogue->addResource(new DirectoryResource($resource)); } return $catalogue; }
/** * {@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; }
/** * {@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)); } $dom = new \DOMDocument(); $current = libxml_use_internal_errors(true); if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { throw new InvalidResourceException(implode("\n", $this->getXmlErrors())); } $xpath = new \DOMXPath($dom); $nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]'); $catalogue = new MessageCatalogue($locale); if ($nodes->length == 1) { $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); foreach ($translations as $translation) { $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue; if (!empty($translationValue)) { $catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, $translationValue, $domain); } $translation = $translation->nextSibling; } $catalogue->addResource(new FileResource($resource)); } libxml_use_internal_errors($current); return $catalogue; }
public function testLoadOldFormat() { $expected = new MessageCatalogue('en'); $expected->add(array('foo1' => 'bar', 'foo2' => 'bar', 'foo3' => 'bar', 'foo4' => 'bar')); $file = __DIR__ . '/xliff/old_format.xml'; $expected->addResource(new FileResource($file)); $this->assertEquals($expected, $this->getLoader()->load($file, 'en')); }
public function testLoadWithMetadata() { $expected = new MessageCatalogue('en'); $expected->add(array('foo' => 'bar')); $file = $this->getInputFile('with_metadata'); $expected->addResource(new FileResource($file)); $this->assertEquals($expected, $this->load($file)); }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { $xml = $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) { $catalogue->set((string) $translation->source, (string) $translation->target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritDoc} * * @param mixed $resource resource * @param string $locale locale name * @param string $domain message domain * * @return MessageCatalogue */ public function load($resource, $locale, $domain = 'messages') { $repository = $this->getRepository(); $messages = $repository->findBy(array('domain' => $domain, 'locale' => $locale, 'isLocalized' => true)); $catalogue = new MessageCatalogue($locale); array_walk($messages, function ($message) use($catalogue) { $catalogue->set((string) $message->getOriginal(), $message->getTranslated(), $message->getDomain()); }); $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { $rb = new \ResourceBundle($locale, $resource); if (!$rb) { throw new \RuntimeException("cannot load this resource : $resource"); } elseif (intl_is_failure($rb->getErrorCode())) { throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode()); } $messages = $this->flatten($rb); $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); if (is_dir($resource)) { $catalogue->addResource(new DirectoryResource($resource)); } elseif (is_file($resource.'.dat')) { $catalogue->addResource(new FileResource($resource.'.dat')); } return $catalogue; }
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)); } $messages = (require $resource); $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} * * @api */ public function load($resource, $locale, $domain = 'messages') { if (!stream_is_local($resource)) { throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); } $xml = $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) { $catalogue->set((string) $translation->source, (string) $translation->target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { $previous = libxml_use_internal_errors(true); if (false === ($xml = simplexml_load_file($resource))) { libxml_use_internal_errors($previous); $error = libxml_get_last_error(); throw new RuntimeException(sprintf('An error occurred while reading "%s": %s', $resource, $error->message)); } libxml_use_internal_errors($previous); $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); $catalogue = new MessageCatalogue($locale); foreach ($xml->xpath('//xliff:trans-unit') as $translation) { $id = ($resName = (string) $translation->attributes()->resname) ? $resName : (string) $translation->source; $catalogue->set($id, (string) $translation->target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { if (!stream_is_local($resource . '.dat')) { throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); } if (!file_exists($resource . '.dat')) { throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $rb = new \ResourceBundle($locale, $resource); if (!$rb) { throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource)); } elseif (intl_is_failure($rb->getErrorCode())) { throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode()); } $messages = $this->flatten($rb); $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); $catalogue->addResource(new FileResource($resource . '.dat')); return $catalogue; }
/** * {@inheritdoc} * * @api */ public function load($resource, $locale, $domain = 'messages') { $dom = new \DOMDocument(); $current = libxml_use_internal_errors(true); if (!@$dom->load($resource, LIBXML_COMPACT)) { throw new \RuntimeException(implode("\n", $this->getXmlErrors())); } $xpath = new \DOMXPath($dom); $nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]'); $catalogue = new MessageCatalogue($locale); if ($nodes->length == 1) { $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); foreach ($translations as $translation) { $catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue, $domain); $translation = $translation->nextSibling; } $catalogue->addResource(new FileResource($resource)); } libxml_use_internal_errors($current); return $catalogue; }
/** * {@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; }
/** * {@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)); } $xml = $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; $catalogue->set((string) $source, (string) $translation->target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; }
private function loadFallbackCatalogues($locale) { $current = $this->catalogues[$locale]; foreach ($this->computeFallbackLocales($locale) as $fallback) { if (!isset($this->catalogues[$fallback])) { $this->doLoadCatalogue($fallback); } $fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all()); foreach ($this->catalogues[$fallback]->getResources() as $resource) { $fallbackCatalogue->addResource($resource); } $current->addFallbackCatalogue($fallbackCatalogue); $current = $fallbackCatalogue; } }
protected function getCatalogue($locale, $messages, $resources = array()) { $catalogue = new MessageCatalogue($locale); foreach ($messages as $key => $translation) { $catalogue->set($key, $translation); } foreach ($resources as $resource) { $catalogue->addResource($resource); } return $catalogue; }
public function testGetAddResource() { if (!class_exists('Symfony\\Component\\Config\\Loader\\Loader')) { $this->markTestSkipped('The "Config" component is not available'); } $catalogue = new MessageCatalogue('en'); $r = $this->getMock('Symfony\\Component\\Config\\Resource\\ResourceInterface'); $r->expects($this->any())->method('__toString')->will($this->returnValue('r')); $catalogue->addResource($r); $catalogue->addResource($r); $r1 = $this->getMock('Symfony\\Component\\Config\\Resource\\ResourceInterface'); $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1')); $catalogue->addResource($r1); $this->assertEquals(array($r, $r1), $catalogue->getResources()); }
public function testGetAddResource() { $catalogue = new MessageCatalogue('en'); $r = $this->getMock('Symfony\\Component\\Config\\Resource\\ResourceInterface'); $r->expects($this->any())->method('__toString')->will($this->returnValue('r')); $catalogue->addResource($r); $catalogue->addResource($r); $r1 = $this->getMock('Symfony\\Component\\Config\\Resource\\ResourceInterface'); $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1')); $catalogue->addResource($r1); $this->assertEquals(array($r, $r1), $catalogue->getResources()); }
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; }