本文整理汇总了PHP中JMS\TranslationBundle\Model\MessageCatalogue::add方法的典型用法代码示例。如果您正苦于以下问题:PHP MessageCatalogue::add方法的具体用法?PHP MessageCatalogue::add怎么用?PHP MessageCatalogue::add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JMS\TranslationBundle\Model\MessageCatalogue
的用法示例。
在下文中一共展示了MessageCatalogue::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: enterNode
/**
* @param \Twig_NodeInterface $node
* @param \Twig_Environment $env
* @return \Twig_NodeInterface
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
$this->stack[] = $node;
if ($node instanceof TransNode) {
$id = $node->getNode('body')->getAttribute('data');
$domain = 'messages';
if (null !== ($domainNode = $node->getNode('domain'))) {
$domain = $domainNode->getAttribute('value');
}
$message = new Message($id, $domain);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
} elseif ($node instanceof \Twig_Node_Expression_Filter) {
$name = $node->getNode('filter')->getAttribute('value');
if ('trans' === $name || 'transchoice' === $name) {
$idNode = $node->getNode('node');
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
return $node;
// FIXME: see below
// throw new \RuntimeException(sprintf('Cannot infer translation id from node "%s". Please refactor to only translate constants.', get_class($idNode)));
}
$id = $idNode->getAttribute('value');
$index = 'trans' === $name ? 1 : 2;
$domain = 'messages';
$arguments = $node->getNode('arguments');
if ($arguments->hasNode($index)) {
$argument = $arguments->getNode($index);
if (!$argument instanceof \Twig_Node_Expression_Constant) {
return $node;
// FIXME: Throw exception if there is some way for the user to turn this off
// on a case-by-case basis, similar to @Ignore in PHP
}
$domain = $argument->getAttribute('value');
}
$message = new Message($id, $domain);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
for ($i = count($this->stack) - 2; $i >= 0; $i -= 1) {
if (!$this->stack[$i] instanceof \Twig_Node_Expression_Filter) {
break;
}
$name = $this->stack[$i]->getNode('filter')->getAttribute('value');
if ('desc' === $name || 'meaning' === $name) {
$arguments = $this->stack[$i]->getNode('arguments');
if (!$arguments->hasNode(0)) {
throw new RuntimeException(sprintf('The "%s" filter requires exactly one argument, the description text.', $name));
}
$text = $arguments->getNode(0);
if (!$text instanceof \Twig_Node_Expression_Constant) {
throw new RuntimeException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name));
}
$message->{'set' . $name}($text->getAttribute('value'));
} elseif ('trans' === $name) {
break;
}
}
$this->catalogue->add($message);
}
}
return $node;
}
示例2: getTranslations
/**
* Get the translations
*
* @return MessageCatalogue
*/
protected function getTranslations()
{
$catalogue = new MessageCatalogue();
$labels = array();
$entities = $this->getEntities();
foreach ($entities as $entity) {
$labels[] = $entity . '.label';
$labels[] = $entity . '.show.title';
$labels[] = $entity . '.edit.title';
$labels[] = $entity . '.list.title';
$labels[] = $entity . '.new.title';
$labels[] = $entity . '.delete.title';
}
//avoid doublons
$uniqueLabels = array_unique($labels);
foreach ($uniqueLabels as $uniqueLabel) {
$message = new Message($uniqueLabel, $this->domain);
$catalogue->add($message);
}
if ($this->customizedFlash) {
$flashLabels = $this->getFlashsLabels($entities);
foreach ($flashLabels as $flashLabel) {
$message = new Message($flashLabel, $this->domain);
$catalogue->add($message);
}
}
return $catalogue;
}
示例3: testPathWithSubPath
public function testPathWithSubPath()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('fr');
$catalogue->add(new Message('foo.bar'));
$catalogue->add(new Message('foo.bar.baz'));
$dumper = $this->getDumper();
$dumper->expects($this->once())->method('dumpStructure')->with(array('foo' => array('bar' => new Message('foo.bar'), 'bar.baz' => new Message('foo.bar.baz'))))->will($this->returnValue('foo'));
$this->assertEquals('foo', $dumper->dump($catalogue, 'messages'));
}
示例4: testLoadWithSymfonyFormat
public function testLoadWithSymfonyFormat()
{
$loader = new XliffLoader();
$expected = new MessageCatalogue();
$expected->setLocale('en');
$expected->add(XliffMessage::create('foo1')->setDesc('foo1')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo2')->setDesc('foo2')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo3')->setDesc('foo3')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo4')->setDesc('foo4')->setLocaleString('bar')->setNew(false));
$this->assertEquals($expected, $loader->load(__DIR__ . '/Symfony/xliff/old_format.xml', 'en'));
}
示例5: testCompareWithMultipleDomains
public function testCompareWithMultipleDomains()
{
$current = new MessageCatalogue();
$current->add(Message::create('foo')->setLocaleString('bar'));
$current->add(Message::create('bar', 'routes')->setLocaleString('baz'));
$new = new MessageCatalogue();
$new->add(new Message('foo'));
$new->add(new Message('bar'));
$expected = new ChangeSet(array(new Message('bar')), array(Message::create('bar', 'routes')->setLocaleString('baz')));
$comparator = new CatalogueComparator();
$this->assertEquals($expected, $comparator->compare($current, $new));
}
示例6: testExtractFormModel
public function testExtractFormModel()
{
$expected = new MessageCatalogue();
$path = __DIR__ . '/Fixture/MyFormModel.php';
$message = new Message('form.label.choice.foo');
$message->addSource(new FileSource($path, 13));
$expected->add($message);
$message = new Message('form.label.choice.bar');
$message->addSource(new FileSource($path, 13));
$expected->add($message);
$this->assertEquals($expected, $this->extract('MyFormModel.php'));
}
示例7: testExtractTemplate
public function testExtractTemplate()
{
$expected = new MessageCatalogue();
$path = __DIR__ . '/Fixture/template.html.php';
$message = new Message('foo.bar');
$message->addSource(new FileSource($path, 1));
$expected->add($message);
$message = new Message('baz', 'moo');
$message->setDesc('Foo Bar');
$message->addSource(new FileSource($path, 3));
$expected->add($message);
$this->assertEquals($expected, $this->extract('template.html.php'));
}
示例8: testExtractValidationMessages
public function testExtractValidationMessages()
{
$fileSourceFactory = $this->getFileSourceFactory();
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/MyEntity.php');
$expected = new MessageCatalogue();
$message = new Message('entity.default');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 15));
$expected->add($message);
$message = new Message('entity.custom-domain', 'custom-domain');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 22));
$expected->add($message);
$this->assertEquals($expected, $this->extract('MyEntity.php'));
}
示例9: testExtract
public function testExtract()
{
$expected = new MessageCatalogue();
$message = new Message('security.authentication_error.foo', 'authentication');
$message->setDesc('%foo% is invalid.');
$message->addSource(new FileSource(__DIR__ . '/Fixture/MyAuthException.php', 31));
$expected->add($message);
$message = new Message('security.authentication_error.bar', 'authentication');
$message->setDesc('An authentication error occurred.');
$message->addSource(new FileSource(__DIR__ . '/Fixture/MyAuthException.php', 35));
$expected->add($message);
$this->assertEquals($expected, $this->extract('MyAuthException.php'));
}
示例10: testExtractTemplate
public function testExtractTemplate()
{
$expected = new MessageCatalogue();
$fileSourceFactory = $this->getFileSourceFactory();
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/template.html.php');
$message = new Message('foo.bar');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 1));
$expected->add($message);
$message = new Message('baz', 'moo');
$message->setDesc('Foo Bar');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 3));
$expected->add($message);
$this->assertEquals($expected, $this->extract('template.html.php'));
}
示例11: testDumpStructureWithMetadata
public function testDumpStructureWithMetadata()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');
$message = new Message('foo.bar.baz');
$message->setDesc('Foo');
$catalogue->add($message);
$message = new Message('foo.bar.moo');
$message->setMeaning('Bar');
$catalogue->add($message);
$message = new Message('foo.baz');
$catalogue->add($message);
$this->assertEquals($this->getOutput('structure_with_metadata'), $this->dump($catalogue, 'messages'));
}
示例12: testCatalogueIsSortedBeforeBeingDumped
public function testCatalogueIsSortedBeforeBeingDumped()
{
$dumper = $this->getMock('JMS\\TranslationBundle\\Translation\\Dumper\\DumperInterface');
$self = $this;
$dumper->expects($this->once())->method('dump')->will($this->returnCallback(function ($v) use($self) {
$self->assertEquals(array('foo.bar', 'foo.bar.baz'), array_keys($v->getDomain('messages')->all()));
}));
$writer = new FileWriter(array('test' => $dumper));
$catalogue = new MessageCatalogue();
$catalogue->setLocale('fr');
$catalogue->add(new Message('foo.bar.baz'));
$catalogue->add(new Message('foo.bar'));
$path = tempnam(sys_get_temp_dir(), 'filewriter');
$writer->write($catalogue, 'messages', $path, 'test');
@unlink($path);
}
示例13: testCdataOutput
public function testCdataOutput()
{
$dumper = $this->getDumper();
$catalogue = new MessageCatalogue();
$catalogue->add(Message::create('foo')->setLocaleString('<bar>')->setDesc('<baz>'));
$expected = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
<file source-language="en" target-language="" datatype="plaintext" original="not.available">
<header>
<tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" resname="foo">
<source><![CDATA[<baz>]]></source>
<target state="new"><![CDATA[<bar>]]></target>
</trans-unit>
</body>
</file>
</xliff>
EOF;
$this->assertEquals($expected, $dumper->dump($catalogue, 'messages'));
}
示例14: write
public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
{
$newCatalogue = new MessageCatalogue();
$newCatalogue->setLocale($catalogue->getLocale());
foreach (array_keys($catalogue->getDomains()) as $catalogueDomainString) {
if ($catalogue->getLocale() !== 'en' && $this->hasEnglishCatalogue($filePath)) {
$englishCatalogue = $this->loadEnglishCatalogue($filePath, $domain, $format);
}
$domainMessageCollection = $catalogue->getDomain($catalogueDomainString);
/** @var Message $message */
foreach ($domainMessageCollection->all() as $message) {
if ($message->getDomain() !== $domain) {
continue;
}
$newMessage = $this->makeXliffMessage($message);
if ($message->getId() === $message->getSourceString()) {
if (isset($englishCatalogue)) {
try {
$newMessage->setDesc($englishCatalogue->get($message->getId(), $message->getDomain())->getLocaleString());
} catch (InvalidArgumentException $e) {
continue;
}
} else {
$newMessage->setDesc($message->getLocaleString());
}
}
$newCatalogue->add($newMessage);
}
}
$this->innerFileWriter->write($newCatalogue, $domain, $filePath, $format);
}
示例15: extract
public function extract()
{
$catalogue = new MessageCatalogue();
$collection = $this->router instanceof I18nRouter ? $this->router->getOriginalRouteCollection() : $this->router->getRouteCollection();
foreach ($collection->all() as $name => $route) {
if ($this->routeExclusionStrategy->shouldExcludeRoute($name, $route)) {
continue;
}
///////////////////////////////////////
// Begin customizations
$meaning = "Route Controller and method: " . $route->getDefault('_controller');
// set a default value
// prefix with zikula module url if requested
if ($route->hasDefault('_zkModule')) {
$zkNoBundlePrefix = $route->getOption('zkNoBundlePrefix');
if (!isset($zkNoBundlePrefix) || !$zkNoBundlePrefix) {
$meaning = "This is a route from the " . $route->getDefault('_zkModule') . "Bundle and will include a translated prefix.";
}
}
// End customizations
///////////////////////////////////////
$message = new Message($name, $this->domain);
$message->setDesc($route->getPath());
if (isset($meaning)) {
$message->setMeaning($meaning);
}
$catalogue->add($message);
}
return $catalogue;
}