public function testExtractController()
 {
     $catalogue = $this->extract('Controller.php');
     $fileSourceFactory = $this->getFileSourceFactory();
     $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/Controller.php');
     $expected = new MessageCatalogue();
     $message = new Message('text.foo_bar');
     $message->setDesc('Foo bar');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 45));
     $expected->add($message);
     $message = new Message('text.sign_up_successful');
     $message->setDesc('Welcome %name%! Thanks for signing up.');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 52));
     $expected->add($message);
     $message = new Message('button.archive');
     $message->setDesc('Archive Message');
     $message->setMeaning('The verb (to archive), describes an action');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 59));
     $expected->add($message);
     $message = new Message('text.irrelevant_doc_comment', 'baz');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 71));
     $expected->add($message);
     $message = new Message('text.array_method_call');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 76));
     $expected->add($message);
     $message = new Message('text.var.assign');
     $message->setDesc('The var %foo% should be assigned.');
     $message->addSource($fileSourceFactory->create($fixtureSplInfo, 82));
     $expected->add($message);
     $this->assertEquals($expected, $catalogue);
 }
 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;
 }
 public function testExtractController()
 {
     $catalogue = $this->extract('Controller.php');
     $path = __DIR__ . '/Fixture/Controller.php';
     $expected = new MessageCatalogue();
     $message = new Message('text.foo_bar');
     $message->setDesc('Foo bar');
     $message->addSource(new FileSource($path, 45));
     $expected->add($message);
     $message = new Message('text.sign_up_successful');
     $message->setDesc('Welcome %name%! Thanks for signing up.');
     $message->addSource(new FileSource($path, 52));
     $expected->add($message);
     $message = new Message('button.archive');
     $message->setDesc('Archive Message');
     $message->setMeaning('The verb (to archive), describes an action');
     $message->addSource(new FileSource($path, 59));
     $expected->add($message);
     $message = new Message('text.irrelevant_doc_comment', 'baz');
     $message->addSource(new FileSource($path, 71));
     $expected->add($message);
     $message = new Message('text.array_method_call');
     $message->addSource(new FileSource($path, 76));
     $expected->add($message);
     $this->assertEquals($expected, $catalogue);
 }
 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'));
 }
示例#5
0
 public function testMerge()
 {
     $message = new Message('foo');
     $message->setDesc('foo');
     $message->setMeaning('foo');
     $message->addSource($s1 = $this->getMock('JMS\\TranslationBundle\\Model\\SourceInterface'));
     $message2 = new Message('foo');
     $message2->setDesc('bar');
     $message2->addSource($s2 = $this->getMock('JMS\\TranslationBundle\\Model\\SourceInterface'));
     $message->merge($message2);
     $this->assertEquals('bar', $message->getDesc());
     $this->assertEquals('foo', $message->getMeaning());
     $this->assertSame(array($s1, $s2), $message->getSources());
 }
示例#6
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 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'));
 }
 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);
 }
 /**
  * @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);
 }
 /**
  * @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);
 }
示例#11
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);
 }
 private function parseItem($item)
 {
     // get doc comment
     $ignore = false;
     $desc = $meaning = null;
     if ($docComment = $item->value->getDocComment()) {
         foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $item->value->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 (!$item->value instanceof \PHPParser_Node_Scalar_String) {
         if ($ignore) {
             return;
         }
         $message = sprintf('Unable to extract translation id for form label from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine());
         if ($this->logger) {
             $this->logger->err($message);
             return;
         }
         throw new RuntimeException($message);
     }
     $message = new Message($item->value->value);
     $message->addSource(new FileSource((string) $this->file, $item->value->getLine()));
     if ($desc) {
         $message->setDesc($desc);
     }
     if ($meaning) {
         $message->setMeaning($meaning);
     }
     $this->catalogue->add($message);
 }