/** * {@inheritDoc} */ protected function format(MessageCatalogue $messages, $domain) { $m = $messages->all($domain); if ($this->nestLevel > 0) { // build a message tree from the message list, with a max depth // of $this->nestLevel $tree = array(); foreach ($m as $key => $message) { // dots are ignored at the beginning and at the end of a key $key = trim($key, "\t ."); if (strlen($key) > 0) { $codes = explode('.', $key, $this->nestLevel + 1); $node =& $tree; foreach ($codes as $code) { if (strlen($code) > 0) { if (!isset($node)) { $node = array(); } $node =& $node[$code]; } } $node = $message; } } return Yaml::dump($tree, $this->nestLevel + 1); // second parameter at 1 outputs normal line-by-line catalogue } else { return Yaml::dump($m, 1); } }
/** * {@inheritdoc} */ protected function format(MessageCatalogue $messages, $domain) { if (!class_exists('Symfony\\Component\\Yaml\\Yaml')) { throw new \LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.'); } return Yaml::dump($messages->all($domain)); }
/** * Dumps the message catalogue. * * @param MessageCatalogue $messages The message catalogue * @param array $options Options that are used by the dumper */ public function dump(MessageCatalogue $messages, $options = array()) { $this->loadAll = false; $locale = $messages->getLocale(); try { foreach ($messages->getDomains() as $eachDomain) { foreach ($messages->all($eachDomain) as $eachKey => $eachTranslation) { $queryBuilder = $this->entityManager->createQueryBuilder(); $queryBuilder->select('t')->from('MjrLibraryEntitiesBundle:System\\Translation', 't')->where($queryBuilder->expr()->andX($queryBuilder->expr()->eq('t.Identity', '?1'), $queryBuilder->expr()->eq('t.Locale', '?2'))); $query = $this->entityManager->createQuery($queryBuilder->getDQL()); $query->setParameters(array(1 => $eachKey, 2 => $locale)); $result = $query->getArrayResult(); if (count($result) < 1) { $entry = new Translation(); $entry->setLocale($locale); $entry->setIdentity($eachKey); $entry->setTranslation($eachKey); $this->entityManager->persist($entry); $this->entityManager->flush(); } unset($query, $queryBuilder, $entry, $eachKey, $eachTranslation); } } } catch (\Exception $ex) { var_dump($ex); die; } }
/** * Initialize filerev catalogue from cache * or generate cache if not already available */ private function initializeCacheCatalogue() { if (isset($this->catalogue)) { return; } $debug = $this->container->getParameter('grunt.debug'); $cache = new ConfigCache($this->getCatalogueCachePath(), $debug); if (!$cache->isFresh()) { $this->initializeCatalogue(); $content = sprintf(<<<EOF <?php use Symfony\\Component\\Translation\\MessageCatalogue; \$catalogue = new MessageCatalogue(null, %s); return \$catalogue; EOF , var_export($this->catalogue->all(), true)); $cache->write($content, $this->catalogue->getResources()); return; } $this->catalogue = (include $cache); }
/** * {@inheritdoc} */ protected function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('version', '1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile->setAttribute('source-language', $messages->getLocale()); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { $translation = $dom->createElement('trans-unit'); $translation->setAttribute('id', md5($source)); $translation->setAttribute('resname', $source); $s = $translation->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); $t = $translation->appendChild($dom->createElement('target')); $t->appendChild($text); $xliffBody->appendChild($translation); } return $dom->saveXML(); }
public function testReplace() { $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar'))); $catalogue->replace($messages = array('foo1' => 'foo1'), 'domain1'); $this->assertEquals($messages, $catalogue->all('domain1')); }
/** * {@inheritDoc} */ protected function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('version', '1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile->setAttribute('source-language', $messages->getLocale()); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); $xliffBody = $xliffFile->appendChild($dom->createElement('body')); $id = 1; foreach ($messages->all($domain) as $source => $target) { $trans = $dom->createElement('trans-unit'); $trans->setAttribute('id', $id); $s = $trans->appendChild($dom->createElement('source')); $s->appendChild($dom->createTextNode($source)); $t = $trans->appendChild($dom->createElement('target')); $t->appendChild($dom->createTextNode($target)); $xliffBody->appendChild($trans); $id++; } return $dom->saveXML(); }
/** * @param MessageCatalogue $messages * @param string $domain * @return array */ protected function format(MessageCatalogue $messages, $domain) { $data = array(); foreach ($messages->all($domain) as $key => $value) { $data[] = array($key, $value); } return $data; }
/** * {@inheritdoc} */ protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { $output = ''; foreach ($messages->all($domain) as $source => $target) { $escapeTarget = str_replace('"', '\\"', $target); $output .= $source . '="' . $escapeTarget . "\"\n"; } return $output; }
/** * {@inheritDoc} */ public function format(MessageCatalogue $messages, $domain = 'messages') { $output = ''; foreach ($messages->all($domain) as $source => $target) { $output .= sprintf("msgid \"%s\"\n", $this->escape($source)); $output .= sprintf("msgstr \"%s\"\n\n", $this->escape($target)); } return $output; }
/** * {@inheritDoc} */ public function format(MessageCatalogue $messages, $domain = 'messages') { $output = ''; foreach ($messages->all($domain) as $source => $target) { $escapeTarget = str_replace('"', '\\"', $target); $output .= $source . '="' . $escapeTarget . "\"\n"; } return $output; }
/** * {@inheritdoc} */ public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { if (isset($options['json_encoding'])) { $flags = $options['json_encoding']; } else { $flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; } return json_encode($messages->all($domain), $flags); }
/** * * {@inheritdoc} * */ public function format(MessageCatalogue $messages, $domain = 'messages') { $handle = fopen('php://memory', 'rb+'); foreach ($messages->all($domain) as $source => $target) { fputcsv($handle, array($source, $target), $this->delimiter, $this->enclosure); } rewind($handle); $output = stream_get_contents($handle); fclose($handle); return $output; }
public function load($resource = null, $culture = null, $version = 0) { $resourceName = $resource === null ? 'Global * Debug' : $resource . '/' . $culture; $cacheFile = $this->cacheDir . '/translations/resource-' . ($resource === null ? 'global' : $resource) . '-' . $culture . '-v' . $version . '.php'; if (!$this->cache || !file_exists($cacheFile) || filemtime($cacheFile) + self::TTL * 3600 < time()) { try { // Delete Symfony Catalogue $fs = new Filesystem(); $finder = new Finder(); $sfCacheFiles = $finder->in($this->cacheDir . '/translations/')->name('catalogue.' . $culture . '.*'); $fs->remove($sfCacheFiles); } catch (Exception $e) { } $catalogue = new MessageCatalogue($culture); if ($resource === null) { $nodesSql = 'SELECT N.node_name, N.node_type, A.attrib_name, A.attrib_original, NULL AS value_translation FROM translation_node N INNER JOIN translation_attribute A ON N.node_id = A.attrib_node'; $nodesStmt = $this->doctrine->getManager()->getConnection()->prepare($nodesSql); $nodesStmt->execute(); } else { $nodesSql = 'SELECT N.node_name, N.node_type, A.attrib_name, A.attrib_original, V.value_translation FROM translation_node N INNER JOIN translation_attribute A ON N.node_id=A.attrib_node LEFT JOIN translation_value V ON A.attrib_id = V.value_attribute AND (V.value_resource = :resource OR V.value_resource IS NULL)'; $nodesStmt = $this->doctrine->getManager()->getConnection()->prepare($nodesSql); $nodesStmt->execute(['resource' => $resource]); } $nodes = $nodesStmt->fetchAll(); foreach ($nodes as $node) { $id = $node['node_name'] . '.' . $node['attrib_name']; $value = $node['value_translation']; $type = $node['node_type']; if ($value != null) { $catalogue->set($id, $value, $type); } else { if ($resource != null) { if ($this->debug) { $this->logger->warning('Translation for key "' . $id . '" and region "' . $resourceName . '" not found!'); } } if ($this->debug) { $catalogue->set($id, '[--' . $node['attrib_original'] . '--]', $type); } else { $catalogue->set($id, $node['attrib_original'], $type); } } } // Save Cache $cacheContent = '<?php ' . PHP_EOL . 'use Symfony\\Component\\Translation\\MessageCatalogue; ' . PHP_EOL . '$catalogue = new MessageCatalogue(\'' . $culture . '\', ' . var_export($catalogue->all(), true) . '); ' . PHP_EOL . 'return $catalogue;'; if (!is_dir($this->cacheDir . '/translations')) { mkdir($this->cacheDir . '/translations'); } file_put_contents($cacheFile, $cacheContent); } else { $catalogue = (include $cacheFile); } return $catalogue; }
/** * @param MessageCatalogue $messageCatalogue * @param string $language */ private function addMessageCatalogue(MessageCatalogue $messageCatalogue, $language) { if (!$messageCatalogue->all()) { return; } if (isset($this->messageCatalogues[$language])) { $this->messageCatalogues[$language]->addCatalogue($messageCatalogue); } else { $this->messageCatalogues[$language] = $messageCatalogue; } }
/** * {@inheritdoc} */ public function format(MessageCatalogue $messages, $domain = 'messages') { $data = $indexes = $resources = ''; foreach ($messages->all($domain) as $source => $target) { $indexes .= pack('v', strlen($data) + 28); $data .= $source . ""; } $data .= $this->writePadding($data); $keyTop = $this->getPosition($data); foreach ($messages->all($domain) as $source => $target) { $resources .= pack('V', $this->getPosition($data)); $data .= pack('V', strlen($target)) . mb_convert_encoding($target . "", 'UTF-16LE', 'UTF-8') . $this->writePadding($data); } $resOffset = $this->getPosition($data); $data .= pack('v', count($messages)) . $indexes . $this->writePadding($data) . $resources; $bundleTop = $this->getPosition($data); $root = pack('V7', $resOffset + (2 << 28), 6, $keyTop, $bundleTop, $bundleTop, count($messages), 0); $header = pack('vC2v4C12@32', 32, 0xda, 0x27, 20, 0, 0, 2, 0x52, 0x65, 0x73, 0x42, 1, 2, 0, 0, 1, 4, 0, 0); return $header . $root . $data; }
/** * {@inheritDoc} */ public function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $ts = $dom->appendChild($dom->createElement('TS')); $context = $ts->appendChild($dom->createElement('context')); $context->appendChild($dom->createElement('name', $domain)); foreach ($messages->all($domain) as $source => $target) { $message = $context->appendChild($dom->createElement('message')); $message->appendChild($dom->createElement('source', $source)); $message->appendChild($dom->createElement('translation', $target)); } return $dom->saveXML(); }
/** * {@inheritdoc} */ public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { if (!class_exists('Symfony\\Component\\Yaml\\Yaml')) { throw new \LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.'); } $data = $messages->all($domain); if (isset($options['as_tree']) && $options['as_tree']) { $data = ArrayConverter::expandToTree($data); } if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) { return Yaml::dump($data, $inline); } return Yaml::dump($data); }
/** * {@inheritdoc} */ protected function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('version', '1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale)); $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale())); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { $translation = $dom->createElement('trans-unit'); $translation->setAttribute('id', md5($source)); $translation->setAttribute('resname', $source); $s = $translation->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 = $translation->appendChild($targetElement); $t->appendChild($text); if ($this->hasMetadataArrayInfo('notes', $metadata)) { 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']); } } } $xliffBody->appendChild($translation); } return $dom->saveXML(); }
/** * {@inheritDoc} */ public function format(MessageCatalogue $messages, $domain = 'messages') { $output = ''; $newLine = false; foreach ($messages->all($domain) as $source => $target) { if ($newLine) { $output .= "\n"; } else { $newLine = true; } $output .= sprintf('msgid "%s"' . "\n", $this->escape($source)); $output .= sprintf('msgstr "%s"', $this->escape($target)); } return $output; }
/** * {@inheritDoc} */ public function format(MessageCatalogue $messages, $domain = 'messages') { $output = $sources = $targets = $sourceOffsets = $targetOffsets = ''; $offsets = array(); $size = 0; foreach ($messages->all($domain) as $source => $target) { $offsets[] = array_map('strlen', array($sources, $source, $targets, $target)); $sources .= "\0".$source; $targets .= "\0".$target; ++$size; } $header = array( 'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC, 'formatRevision' => 0, 'count' => $size, 'offsetId' => MoFileLoader::MO_HEADER_SIZE, 'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size), 'sizeHashes' => 0, 'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size), ); $sourcesSize = strlen($sources); $sourcesStart = $header['offsetHashes'] + 1; foreach ($offsets as $offset) { $sourceOffsets .= $this->writeLong($offset[1]) . $this->writeLong($offset[0] + $sourcesStart); $targetOffsets .= $this->writeLong($offset[3]) . $this->writeLong($offset[2] + $sourcesStart + $sourcesSize); } $output = implode(array_map(array($this, 'writeLong'), $header)) . $sourceOffsets . $targetOffsets . $sources . $targets ; return $output; }
/** * {@inheritdoc} */ public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { $output = 'msgid ""' . "\n"; $output .= 'msgstr ""' . "\n"; $output .= '"Content-Type: text/plain; charset=UTF-8\\n"' . "\n"; $output .= '"Content-Transfer-Encoding: 8bit\\n"' . "\n"; $output .= '"Language: ' . $messages->getLocale() . '\\n"' . "\n"; $output .= "\n"; $newLine = false; foreach ($messages->all($domain) as $source => $target) { if ($newLine) { $output .= "\n"; } else { $newLine = true; } $output .= sprintf('msgid "%s"' . "\n", $this->escape($source)); $output .= sprintf('msgstr "%s"', $this->escape($target)); } return $output; }
/** * {@inheritdoc} */ protected function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('version', '1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale)); $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale())); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { $translation = $dom->createElement('trans-unit'); $translation->setAttribute('id', md5($source)); $translation->setAttribute('resname', $source); $s = $translation->appendChild($dom->createElement('source')); $s->appendChild($dom->createTextNode($source)); $t = $translation->appendChild($dom->createElement('target')); $t->appendChild($dom->createTextNode($target)); $metadata = $messages->getMetadata($source, $domain); if (null !== $metadata && array_key_exists('notes', $metadata) && is_array($metadata['notes'])) { 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']); } } } $xliffBody->appendChild($translation); } return $dom->saveXML(); }
/** * {@inheritdoc} */ public function dump(MessageCatalogue $messages, $options = array()) { $connection = \Propel::getConnection($this->query->getDbName()); $connection->beginTransaction(); $now = new \DateTime(); $locale = $messages->getLocale(); foreach ($messages->getDomains() as $eachDomain) { foreach ($messages->all($eachDomain) as $eachKey => $eachTranslation) { $query = clone $this->query; $query->filterBy($this->getColumnPhpname('locale'), $locale)->filterBy($this->getColumnPhpname('domain'), $eachDomain)->filterBy($this->getColumnPhpname('key'), $eachKey); $translation = $query->findOneOrCreate($connection); $translation->setByName($this->getColumnPhpname('translation'), (string) $eachTranslation); $translation->setByName($this->getColumnPhpname('updated_at'), $now); $translation->save($connection); } } if (!$connection->commit()) { $connection->rollBack(); throw new \RuntimeException(sprintf('An error occurred while committing the transaction. [%s: %s]', $connection->errorCode(), $connection->errorInfo())); } }
/** * Initialize filerev catalogue from cache * or generate cache if not already available */ protected function initializeCacheCatalogue() { if (isset($this->summary)) { return; } $cache = new ConfigCache($this->getCatalogueCachePath($this->cacheDir), $this->debug); if (!$cache->isFresh()) { $this->initializeCatalogue($this->summaryFile); $content = sprintf(<<<EOF <?php use Symfony\\Component\\Translation\\MessageCatalogue; \$catalogue = new MessageCatalogue(null, %s); return \$catalogue; EOF , var_export($this->summary->all(), true)); $cache->write($content, $this->summary->getResources()); return; } $this->summary = (include $cache); }
/** * {@inheritDoc} */ protected function format(MessageCatalogue $messages, $domain) { return Neon::encode($messages->all($domain), Neon::BLOCK); }
/** * {@inheritdoc} */ protected function format(MessageCatalogue $messages, $domain) { return Yaml::dump($messages->all($domain)); }
/** * * {@inheritdoc} * */ protected function format(MessageCatalogue $messages, $domain) { $output = "<?php\n\nreturn " . var_export($messages->all($domain), true) . ";\n"; return $output; }
private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array()) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $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(); }
/** * * {@inheritdoc} * */ public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { $output = "<?php\n\nreturn " . var_export($messages->all($domain), true) . ";\n"; return $output; }