本文整理汇总了PHP中Symfony\Component\Translation\MessageCatalogue类的典型用法代码示例。如果您正苦于以下问题:PHP MessageCatalogue类的具体用法?PHP MessageCatalogue怎么用?PHP MessageCatalogue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MessageCatalogue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// get bundle directory
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
$bundleTransPath = $foundBundle->getPath() . '/Resources/translations';
$writer = $this->getContainer()->get('translation.writer');
$supportedFormats = $writer->getFormats();
if (!in_array($input->getOption('output-format'), $supportedFormats)) {
$output->writeln('<error>Wrong output format</error>');
$output->writeln('Supported formats are ' . implode(', ', $supportedFormats) . '.');
return 1;
}
$this->orm = $this->getContainer()->get('doctrine.orm.default_entity_manager');
$languages = $this->orm->getRepository('RaindropTranslationBundle:Language')->findAll();
$tokens = $this->orm->getRepository('RaindropTranslationBundle:LanguageToken')->findAll();
foreach ($languages as $language) {
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $language, $foundBundle->getName()));
// create catalogue
$catalogue = new MessageCatalogue($language);
foreach ($tokens as $token) {
$translation = $this->orm->getRepository('RaindropTranslationBundle:LanguageTranslation')->findOneBy(array('language' => $language, 'languageToken' => $token, 'catalogue' => $token->getCatalogue()));
if (!empty($translation)) {
$content = $translation->getTranslation() != '' ? $translation->getTranslation() : null;
$catalogue->set($token->getToken(), $content);
}
}
$writer->writeTranslations($catalogue, $input->getOption('output-format'), array('path' => $bundleTransPath));
}
}
示例2: load
/**
* {@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;
}
示例3: extractFile
/**
* Extracts translation messages from a file to the catalogue.
*
* @param string $file The path to look into
* @param MessageCatalogue $catalogue The catalogue
*/
public function extractFile($file, MessageCatalogue $catalogue)
{
$buffer = NULL;
$parser = new Parser();
$parser->shortNoEscape = TRUE;
foreach ($tokens = $parser->parse(file_get_contents($file)) as $token) {
if ($token->type !== $token::MACRO_TAG || !in_array($token->name, array('_', '/_'), TRUE)) {
if ($buffer !== NULL) {
$buffer .= $token->text;
}
continue;
}
if ($token->name === '/_') {
$catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $buffer, $buffer);
$buffer = NULL;
} elseif ($token->name === '_' && empty($token->value)) {
$buffer = '';
} else {
$args = new MacroTokens($token->value);
$writer = new PhpWriter($args, $token->modifiers);
$message = $writer->write('%node.word');
if (in_array(substr(trim($message), 0, 1), array('"', '\''), TRUE)) {
$message = substr(trim($message), 1, -1);
}
$catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $message, $message);
}
}
}
示例4: dump
/**
* 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;
}
}
示例5: testLinearFormatCatalogue
public function testLinearFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo.bar1' => 'value1', 'foo.bar2' => 'value2'));
$dumper = new YamlFileDumper();
$this->assertStringEqualsFile(__DIR__ . '/../fixtures/messages_linear.yml', $dumper->formatCatalogue($catalogue, 'messages'));
}
示例6: format
/**
* {@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();
}
示例7: execute
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$writer = $this->getContainer()->get('translation.writer');
if (!$this->checkParameters($input, $output, $writer)) {
return 1;
}
// get bundle directory
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
$bundleTransPath = $foundBundle->getPath() . '/Resources/translations';
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));
// create catalogue
$catalogue = new MessageCatalogue($input->getArgument('locale'));
$output->writeln('Parsing templates');
$annotations = $this->getContainer()->get('oro_security.acl.annotation_provider')->getBundleAnnotations(array($foundBundle->getPath()));
/** @var $annotation Acl*/
/** @var $annotations AclAnnotationStorage*/
foreach ($annotations->getAnnotations() as $annotation) {
if ($label = $annotation->getLabel()) {
$catalogue->set($label, $input->getOption('prefix') . $label);
}
}
// load any existing messages from the translation files
$output->writeln('Loading translation files');
$loader = $this->getContainer()->get('translation.loader');
$loader->loadMessages($bundleTransPath, $catalogue);
// show compiled list of messages
if ($input->getOption('dump-messages') === true) {
$this->dumpMessages($input, $output, $catalogue);
}
// save the files
if ($input->getOption('force') === true) {
$this->saveMessages($input, $output, $catalogue, $writer, $bundleTransPath);
}
}
示例8: testDumpWithCustomEncoding
public function testDumpWithCustomEncoding()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => '"bar"'));
$dumper = new JsonFileDumper();
$this->assertStringEqualsFile(__DIR__ . '/../fixtures/resources.dump.json', $dumper->formatCatalogue($catalogue, 'messages', array('json_encoding' => JSON_HEX_QUOT)));
}
示例9: submitFormTranslate
public function submitFormTranslate(Form $form)
{
$values = $form->getValues();
//existuje preklad ?
$translatesLocale = $this->row->related('translate_locale')->where('language_id', $this->webLanguage)->fetch();
if ($translatesLocale) {
if ($values['translate'] != '') {
$translatesLocale->update(array('translate' => $values['translate']));
} else {
$translatesLocale->delete();
}
} else {
$this->row->related('translate_locale')->insert(array('translate' => $values['translate'], 'language_id' => $this->webLanguage));
}
$language = $this->languages->get($this->webLanguage);
$catalogue = new MessageCatalogue($language['translate_locale']);
foreach ($this->model->getAll() as $translate) {
$translatesLocale = $translate->related('translate_locale')->where('language_id', $this->webLanguage)->fetch();
if ($translatesLocale) {
$catalogue->set($translate['text'], $translatesLocale['translate']);
} else {
$catalogue->set($translate['text'], $translate['text']);
}
}
$this->writer->writeTranslations($catalogue, 'neon', ['path' => $this->context->parameters['appDir'] . '/lang/']);
$this->flashMessage($this->translator->trans('translate.translated'));
$this->redirect('this');
}
示例10: load
/**
* {@inheritdoc}
*/
function load($resource, $locale, $domain = 'messages')
{
$catalogue = new MessageCatalogue($locale);
$catalogue->addMessages(require $resource, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例11: format
/**
* {@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();
}
示例12: load
/**
* 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;
}
示例13: getDatabaseCatalogue
private function getDatabaseCatalogue()
{
$databaseCatalogue = new MessageCatalogue(self::FAKE_LOCALE);
$messages = array('baz' => 'Baz is updated !');
$databaseCatalogue->add($messages, 'messages');
return $databaseCatalogue;
}
示例14: testTargetAttributesMetadataIsSetInFile
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');
}
示例15: format
/**
* {@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));
}