public function testDumpStructure()
 {
     $catalogue = new MessageCatalogue();
     $catalogue->setLocale('en');
     $message = new Message('foo.bar.baz');
     $message->addSource(new FileSource('/a/b/c/foo/bar', 1, 2));
     $message->addSource(new FileSource('bar/baz', 1, 2));
     $catalogue->add($message);
     $this->assertEquals($this->getOutput('structure'), $this->dump($catalogue, 'messages'));
 }
 public static function getTranslationMessages()
 {
     $messages = array();
     foreach (self::$choices as $trans) {
         $message = new Message($trans);
         $message->addSource(new FileSource(__FILE__, 13));
         $messages[] = $message;
     }
     return $messages;
 }
 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'));
 }
 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'));
 }
 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'));
 }
 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'));
 }
Ejemplo n.º 7
0
 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'));
 }
Ejemplo n.º 8
0
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     $this->stack[] = $node;
     if ($node instanceof \Twig_Node_Expression_Function) {
         $name = $node->getAttribute('name');
         if (in_array($name, $this->methodNames)) {
             $args = $node->getNode('arguments');
             switch ($name) {
                 case '_n':
                 case '_fn':
                     $id = $args->getNode(0)->getAttribute('value') . '|' . $args->getNode(1)->getAttribute('value');
                     break;
                 default:
                 case '__f':
                 case '__':
                     $id = $args->getNode(0)->getAttribute('value');
                     break;
             }
             // obtain translation domain from composer file
             $composerPath = str_replace($this->file->getRelativePathname(), '', $this->file->getPathname());
             if (isset(self::$domainCache[$composerPath])) {
                 $domain = self::$domainCache[$composerPath];
             } else {
                 $scanner = new Scanner();
                 $scanner->scan(array($composerPath), 1);
                 $metaData = $scanner->getModulesMetaData(true);
                 $domains = array_keys($metaData);
                 if (isset($domains[0])) {
                     $domain = strtolower($domains[0]);
                     // cache result of file lookup
                     self::$domainCache[$composerPath] = $domain;
                 } else {
                     $domain = 'messages';
                 }
             }
             $domainNode = array_search($name, $this->methodNames);
             $domain = $args->hasNode($domainNode) ? $args->getNode($domainNode)->getAttribute('value') : $domain;
             $message = new Message($id, $domain);
             $message->addSource(new FileSource((string) $this->file, $node->getLine()));
             $this->catalogue->add($message);
         }
     }
     return $node;
 }
Ejemplo n.º 9
0
 public function enterNode(\PHPParser_Node $node)
 {
     if (!$node instanceof \PHPParser_Node_Scalar_String) {
         return;
     }
     $id = $node->value;
     if (preg_match('/(\\.\\.|\\.\\.\\.)/', $id)) {
         return;
     }
     if (preg_match('/^http/', $id)) {
         return;
     }
     if (preg_match('/.*\\./', $id)) {
         $domain = 'messages';
         $message = new Message($id, $domain);
         $message->addSource(new FileSource((string) $this->file, $node->getLine()));
         $this->catalogue->add($message);
     }
 }
Ejemplo n.º 10
0
 public function testExtractWithSimpleTestFixtures()
 {
     $expected = array();
     $basePath = __DIR__ . '/Fixture/SimpleTest/';
     $fileSourceFactory = new FileSourceFactory('faux');
     // Controller
     $message = new Message('controller.foo');
     $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'Controller/DefaultController.php'), 27));
     $message->setDesc('Foo');
     $expected['controller.foo'] = $message;
     // Form Model
     $expected['form.foo'] = new Message('form.foo');
     $expected['form.bar'] = new Message('form.bar');
     // Templates
     foreach (array('php', 'twig') as $engine) {
         $message = new Message($engine . '.foo');
         $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'Resources/views/' . $engine . '_template.html.' . $engine), 1));
         $expected[$engine . '.foo'] = $message;
         $message = new Message($engine . '.bar');
         $message->setDesc('Bar');
         $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'Resources/views/' . $engine . '_template.html.' . $engine), 3));
         $expected[$engine . '.bar'] = $message;
         $message = new Message($engine . '.baz');
         $message->setMeaning('Baz');
         $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'Resources/views/' . $engine . '_template.html.' . $engine), 5));
         $expected[$engine . '.baz'] = $message;
         $message = new Message($engine . '.foo_bar');
         $message->setDesc('Foo');
         $message->setMeaning('Bar');
         $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'Resources/views/' . $engine . '_template.html.' . $engine), 7));
         $expected[$engine . '.foo_bar'] = $message;
     }
     // File with global namespace.
     $message = new Message('globalnamespace.foo');
     $message->addSource($fileSourceFactory->create(new \SplFileInfo($basePath . 'GlobalNamespace.php'), 27));
     $message->setDesc('Bar');
     $expected['globalnamespace.foo'] = $message;
     $actual = $this->extract(__DIR__ . '/Fixture/SimpleTest')->getDomain('messages')->all();
     asort($expected);
     asort($actual);
     $this->assertEquals($expected, $actual);
 }
 public function testExtract()
 {
     $expected = new MessageCatalogue();
     $path = __DIR__ . '/Fixture/MyFormType.php';
     $message = new Message('bar');
     $message->addSource(new FileSource($path, 36));
     $expected->add($message);
     $message = new Message('form.states.empty_value');
     $message->setDesc('Please select a state');
     $message->addSource(new FileSource($path, 37));
     $expected->add($message);
     $message = new Message('form.label.lastname');
     $message->setDesc('Lastname');
     $message->addSource(new FileSource($path, 33));
     $expected->add($message);
     $message = new Message('form.label.firstname');
     $message->addSource(new FileSource($path, 30));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('MyFormType.php'));
 }
 public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast)
 {
     $this->catalogue = $catalogue;
     // Traverse document to assemble class name
     $traverser = new \PHPParser_NodeTraverser();
     $traverser->addVisitor($this);
     $traverser->traverse($ast);
     if ($this->annotated) {
         // Get annotations for the class
         $annotationDriver = new Annotation(new DoctrineAnnotationReader());
         $manager = new Manager();
         $manager->addDriver($annotationDriver, -1);
         $metadata = $manager->getMetadata($this->parsedClassName);
         // Save messages for title
         foreach ($metadata->getFields() as $field) {
             $mappedField = $metadata->getFieldMapping($field);
             if ((!isset($mappedField['visible']) || $mappedField['visible']) && isset($mappedField['title'])) {
                 $message = new Message($mappedField['title']);
                 $message->addSource(new FileSource((string) $file));
                 $catalogue->add($message);
             }
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * @param string $id
  * @param string $domain
  */
 private function addMessage($id, $domain)
 {
     $message = new Message($id, $domain);
     //        $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
     $trace = debug_backtrace(false);
     if (isset($trace[1]['file'])) {
         $message->addSource(new FileSource($trace[1]['file']));
     }
     $this->catalogue->add($message);
 }
Ejemplo n.º 14
0
 public function testEmbeddedTemplate()
 {
     $expected = new MessageCatalogue();
     $fileSourceFactory = $this->getFileSourceFactory();
     $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/embedded_template.html.twig');
     $message = new Message('foo');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 3));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('embedded_template.html.twig'));
 }
Ejemplo n.º 15
0
 /**
  * Get the catalogue used for the structure tests
  *
  * @return MessageCatalogue
  */
 protected function getStructureCatalogue()
 {
     $catalogue = new MessageCatalogue();
     $catalogue->setLocale('en');
     $message = new Message('foo.bar.baz');
     $message->addSource(new FileSource('/z/order/test', 1, 2));
     $message->addSource(new FileSource('bar/baz', 1, 2));
     $message->addSource(new FileSource('bar/baz', 1, 5));
     $message->addSource(new FileSource('/a/b/c/foo/bar', 1, 2));
     $catalogue->add($message);
     return $catalogue;
 }
 public function testEmbeddedTemplate()
 {
     $expected = new MessageCatalogue();
     $path = __DIR__ . '/Fixture/embedded_template.html.twig';
     $message = new Message('foo');
     $message->addSource(new FileSource($path, 3));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('embedded_template.html.twig'));
 }
 /**
  * @param string $id
  * @param string $source
  * @param null|string $domain
  * @param null|string $desc
  * @param null|string $meaning
  */
 private function addToCatalogue($id, $source, $domain = null, $desc = null, $meaning = null)
 {
     if (null === $domain) {
         $message = new Message($id);
     } else {
         $message = new Message($id, $domain);
     }
     $message->addSource($source);
     if ($desc) {
         $message->setDesc($desc);
     }
     if ($meaning) {
         $message->setMeaning($meaning);
     }
     $this->catalogue->add($message);
 }
 /**
  * @expectedException RuntimeException
  * @expectedExceptionMessage The message 'a' exists with two different descs: 'a' in foo on line 1, and 'b' in bar on line 2
  */
 public function testSetChecksConsistency()
 {
     $col = new MessageCollection();
     $msg = new Message('a');
     $msg->setDesc('a');
     $msg->addSource(new FileSource('foo', 1));
     $msg2 = new Message('a');
     $msg2->setDesc('b');
     $msg2->addSource(new FileSource('bar', 2));
     $col->set($msg);
     $col->set($msg2);
 }
 /**
  * @param string $id
  * @param SourceInterface $source
  * @param string|null $domain
  */
 private function addToCatalogue($id, SourceInterface $source, $domain = null)
 {
     if (null === $domain) {
         $message = new Message($id);
     } else {
         $message = new Message($id, $domain);
     }
     $message->addSource($source);
     $this->catalogue->add($message);
 }
 public function testMergeExisting()
 {
     $scannedMessage = new XliffMessage('foo');
     $scannedMessage->setDesc('foo');
     $existingMessage = new XliffMessage('foo');
     $existingMessage->setLocaleString('bar');
     $existingMessage->addSource(new FileSource('bar'));
     $existingMessage->setApproved(true);
     $existingMessage->setState(XliffMessage::STATE_TRANSLATED);
     $existingMessage2 = new Message('foo');
     $existingMessage2->setLocaleString('bar');
     $existingMessage2->setNew(false);
     $existingMessage2->addSource(new FileSource('bar'));
     $scannedMessage1 = clone $scannedMessage;
     $existingMessage1 = clone $existingMessage;
     $scannedMessage1->mergeExisting($existingMessage1);
     $this->assertEquals('foo', $scannedMessage1->getDesc());
     $this->assertEquals('bar', $scannedMessage1->getLocaleString());
     $this->assertFalse($scannedMessage1->isNew());
     $this->assertEquals(array(), $scannedMessage1->getSources());
     $this->assertFalse($scannedMessage1->isApproved());
     $this->assertEquals(XliffMessage::STATE_NONE, $scannedMessage1->getState());
     $scannedMessage2 = clone $scannedMessage;
     $scannedMessage2->setApproved(true);
     $scannedMessage2->setState(XliffMessage::STATE_TRANSLATED);
     $existingMessage2 = clone $existingMessage;
     $existingMessage2->setDesc('bar');
     $existingMessage2->setApproved(false);
     $existingMessage2->setState(XliffMessage::STATE_NONE);
     $scannedMessage2->mergeExisting($existingMessage2);
     $this->assertEquals('foo', $scannedMessage2->getDesc());
     $this->assertTrue($scannedMessage2->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $scannedMessage2->getState());
     $scannedMessage3 = clone $scannedMessage;
     $existingMessage3 = clone $existingMessage1;
     $scannedMessage3->mergeExisting($existingMessage3);
     $this->assertEquals('foo', $scannedMessage3->getDesc());
     $this->assertEquals('bar', $scannedMessage3->getLocaleString());
     $this->assertFalse($scannedMessage3->isNew());
     $this->assertEquals(array(), $scannedMessage3->getSources());
     $this->assertFalse($scannedMessage3->isApproved());
     $this->assertEquals(XliffMessage::STATE_NONE, $scannedMessage3->getState());
     $scannedMessage4 = clone $scannedMessage;
     $scannedMessage4->setApproved(true);
     $scannedMessage4->setState(XliffMessage::STATE_TRANSLATED);
     $existingMessage4 = clone $existingMessage1;
     $existingMessage4->setDesc('bar');
     $scannedMessage4->mergeExisting($existingMessage4);
     $this->assertEquals('foo', $scannedMessage4->getDesc());
     $this->assertTrue($scannedMessage4->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $scannedMessage4->getState());
 }
 public function testExtractEdit()
 {
     $expected = new MessageCatalogue();
     $path = __DIR__ . '/Fixture/edit.html.twig';
     $message = new Message('header.edit_profile');
     $message->addSource(new FileSource($path, 10));
     $expected->add($message);
     $message = new Message("text.archive");
     $message->setDesc('Archive');
     $message->setMeaning('The verb');
     $message->addSource(new FileSource($path, 13));
     $expected->add($message);
     $message = new Message('button.edit_profile');
     $message->addSource(new FileSource($path, 16));
     $expected->add($message);
     $message = new Message('link.cancel_profile');
     $message->setDesc('Back to Profile');
     $message->addSource(new FileSource($path, 17));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('edit.html.twig'));
 }
Ejemplo n.º 22
0
 public function enterNode(\PHPParser_Node $node)
 {
     /**
      * determine domain from namespace of files.
      * Finder appears to start with root level files so Namespace is correct for remaining files
      */
     if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
         if (isset($node->name)) {
             if (array_key_exists($node->name->toString(), $this->bundles)) {
                 $this->domain = strtolower($this->bundles[$node->name->toString()]);
             }
             return;
         } else {
             foreach ($node->stmts as $node) {
                 $this->enterNode($node);
             }
             return;
         }
     }
     if (!$node instanceof \PHPParser_Node_Expr_MethodCall || !is_string($node->name) || !in_array(strtolower($node->name), $this->methodNames)) {
         $this->previousNode = $node;
         return;
     }
     $ignore = false;
     $desc = $meaning = null;
     if (null !== ($docComment = $this->getDocCommentForNode($node))) {
         foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
             if ($annot instanceof Ignore) {
                 $ignore = true;
             } else {
                 if ($annot instanceof Desc) {
                     $desc = $annot->text;
                 } else {
                     if ($annot instanceof Meaning) {
                         $meaning = $annot->text;
                     }
                 }
             }
         }
     }
     if (!$node->args[0]->value instanceof \PHPParser_Node_Scalar_String) {
         if ($ignore) {
             return;
         }
         $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
         if ($this->logger) {
             $this->logger->err($message);
             return;
         }
         throw new RuntimeException($message);
     }
     $id = $node->args[0]->value->value;
     if (in_array(strtolower($node->name), array('_n', '_fn'), true)) {
         // concatenate pluralized strings from zikula functions
         $id = $node->args[0]->value->value . '|' . $node->args[1]->value->value;
     }
     // determine location of domain
     $domainIndex = array_search(strtolower($node->name), $this->methodNames);
     if (isset($node->args[$domainIndex])) {
         if (!$node->args[$domainIndex]->value instanceof \PHPParser_Node_Scalar_String) {
             if ($ignore) {
                 return;
             }
             $message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
             if ($this->logger) {
                 $this->logger->err($message);
                 return;
             }
             throw new RuntimeException($message);
         }
         $domain = $node->args[$domainIndex]->value->value;
     } else {
         $domain = !empty($this->domain) ? $this->domain : 'messages';
     }
     $message = new Message($id, $domain);
     $message->setDesc($desc);
     $message->setMeaning($meaning);
     $message->addSource(new FileSource((string) $this->file, $node->getLine()));
     $this->catalogue->add($message);
 }
Ejemplo n.º 23
0
 /**
  * @param string $id
  * @param string $domain
  */
 private function addMessage($id, $domain)
 {
     $message = new Message($id, $domain);
     $trace = debug_backtrace(false);
     if (isset($trace[1]['file'])) {
         $message->addSource(new FileSource($trace[1]['file']));
     }
     $this->catalogue->add($message);
 }
 public function testAttrArrayForm()
 {
     $expected = new MessageCatalogue();
     $fileSourceFactory = $this->getFileSourceFactory();
     $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/MyAttrArrayType.php');
     $message = new Message('form.label.firstname');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 31));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('MyAttrArrayType.php'));
 }
 /**
  * @param Node $node
  */
 public function enterNode(Node $node)
 {
     if (!$node instanceof Node\Stmt\Throw_ || !$node->expr instanceof Node\Expr\New_) {
         $this->previousNode = $node;
         return;
     }
     $exceptionClass = $node->expr->class->parts[0];
     if (!in_array(strtolower($exceptionClass), array_map('strtolower', $this->exceptionsToExtractFrom))) {
         $this->previousNode = $node;
         return;
     }
     $node = $node->expr;
     $ignore = false;
     $desc = $meaning = null;
     if (null !== ($docComment = $this->getDocCommentForNode($node))) {
         if ($docComment instanceof Doc) {
             $docComment = $docComment->getText();
         }
         foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
             if ($annot instanceof Ignore) {
                 $ignore = true;
             } elseif ($annot instanceof Desc) {
                 $desc = $annot->text;
             } elseif ($annot instanceof Meaning) {
                 $meaning = $annot->text;
             }
         }
     }
     if (!$node->args[0]->value instanceof String_) {
         if ($ignore) {
             return;
         }
         $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
         if ($this->logger) {
             $this->logger->error($message);
             return;
         }
         throw new RuntimeException($message);
     }
     $id = $node->args[0]->value->value;
     $message = new Message($id, $this->defaultDomain);
     $message->setDesc($desc);
     $message->setMeaning($meaning);
     $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
     $this->catalogue->add($message);
 }
 public function hasSource()
 {
     $message = new Message('foo');
     $s2 = $this->getMock('JMS\\TranslationBundle\\Model\\SourceInterface');
     $s1 = $this->getMock('JMS\\TranslationBundle\\Model\\SourceInterface');
     $s1->expects($this->once())->method('equals')->with($s2)->will($this->returnValue(true));
     $message->addSource($s1);
     $this->assertTrue($message->hasSource($s2));
 }
 public function enterNode(\PHPParser_Node $node)
 {
     if (!$node instanceof \PHPParser_Node_Expr_MethodCall || !is_string($node->name) || 'trans' !== strtolower($node->name) && 'transchoice' !== strtolower($node->name)) {
         $this->previousNode = $node;
         return;
     }
     $ignore = false;
     $desc = $meaning = null;
     if (null !== ($docComment = $this->getDocCommentForNode($node))) {
         foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
             if ($annot instanceof Ignore) {
                 $ignore = true;
             } else {
                 if ($annot instanceof Desc) {
                     $desc = $annot->text;
                 } else {
                     if ($annot instanceof Meaning) {
                         $meaning = $annot->text;
                     }
                 }
             }
         }
     }
     if ($ignore) {
         return;
     }
     if (!$node->args[0]->value instanceof \PHPParser_Node_Scalar_String) {
         $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
         if ($this->logger) {
             $this->logger->err($message);
             return;
         }
         throw new RuntimeException($message);
     }
     $id = $node->args[0]->value->value;
     $index = 'trans' === strtolower($node->name) ? 2 : 3;
     if (isset($node->args[$index])) {
         if (!$node->args[$index]->value instanceof \PHPParser_Node_Scalar_String) {
             $message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
             if ($this->logger) {
                 $this->logger->err($message);
                 return;
             }
             throw new RuntimeException($message);
         }
         $domain = $node->args[$index]->value->value;
     } else {
         $domain = 'messages';
     }
     $message = new Message($id, $domain);
     $message->setDesc($desc);
     $message->setMeaning($meaning);
     $message->addSource(new FileSource((string) $this->file, $node->getLine()));
     $this->catalogue->add($message);
 }
Ejemplo n.º 28
0
 /**
  * @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;
 }
Ejemplo n.º 29
0
 /**
  * This test is used to check if translation from subscriber classes and even closures
  * are correctly extracted
  */
 public function testExtractWithWithSubscriberAndListener()
 {
     $expected = new MessageCatalogue();
     $path = __DIR__ . '/Fixture/MyFormTypeWithSubscriberAndListener.php';
     $pathSubscriber = __DIR__ . '/Fixture/MyFormSubscriber.php';
     $message = new Message('form.label.lastname');
     $message->setDesc('Lastname');
     $message->addSource(new FileSource($path, 36));
     $expected->add($message);
     $message = new Message('form.label.firstname');
     $message->addSource(new FileSource($path, 33));
     $expected->add($message);
     $message = new Message('form.label.password');
     $message->addSource(new FileSource($pathSubscriber, 37));
     $expected->add($message);
     $message = new Message('form.label.password_repeated');
     $message->setDesc('Repeat password');
     $message->addSource(new FileSource($pathSubscriber, 40));
     $expected->add($message);
     $message = new Message('form.label.zip', 'address');
     $message->setDesc('ZIP');
     $message->addSource(new FileSource($path, 51));
     $expected->add($message);
     $message = new Message('form.error.password_mismatch', 'validators');
     $message->setDesc('The entered passwords do not match');
     $message->addSource(new FileSource($pathSubscriber, 42));
     $expected->add($message);
     $catalogue = $this->extract('MyFormTypeWithSubscriberAndListener.php');
     //Merge with the subscriber catalogue
     $catalogue->merge($this->extract('MyFormSubscriber.php'));
     $this->assertEquals($expected, $catalogue);
 }
 public function testAttrArrayForm()
 {
     $expected = new MessageCatalogue();
     $path = __DIR__ . '/Fixture/MyAttrArrayType.php';
     $message = new Message('form.label.firstname');
     $message->addSource(new FileSource($path, 31));
     $expected->add($message);
     $this->assertEquals($expected, $this->extract('MyAttrArrayType.php'));
 }