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 extract() { $catalogue = new MessageCatalogue(); foreach ($this->i18nLoader->extract($this->router->getRouteCollection()) as $name => $route) { $message = new Message($name, $this->domain); $message->setDesc($route->getPattern()); $catalogue->add($message); } return $catalogue; }
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 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')); }
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')); }
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; } $message = new Message($name, $this->domain); $message->setDesc($route->getPath()); $catalogue->add($message); } return $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')); }
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 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')); }
/** * @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); }
public function testGetLocaleString() { $message = new Message('foo'); $message->setDesc('bar'); $message->setNew(true); $existingMessage = new Message('foo'); $existingMessage->setDesc('bar'); $existingMessage->setNew(false); $this->assertEquals($message->getDesc(), $message->getLocaleString()); $this->assertEquals('', $existingMessage->getLocaleString()); }
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); }
public function testSetChecksConsistencyButAllowsEmptyDescs() { $col = new MessageCollection(); // both message have not desc $msg = new Message('a'); $msg2 = new Message('a'); $col->set($msg); $col->set($msg2); // first message have a desc $msg = new Message('b'); $msg->setDesc('b'); $msg2 = new Message('b'); $col->set($msg); $col->set($msg2); // second message have a desc $msg = new Message('c'); $msg2 = new Message('c'); $msg2->setDesc('c'); $col->set($msg); $col->set($msg2); // non-null empty descs $msg = new Message('d'); $msg->setDesc('d'); $msg2 = new Message('d'); $msg2->setDesc(''); $col->set($msg); $col->set($msg2); }
public function testMergeScanned() { $existingMessage = new XliffMessage('foo'); $existingMessage->setLocaleString('bar'); $existingMessage->addSource(new FileSource('bar')); $existingMessage->setApproved(false); $existingMessage->setState(XliffMessage::STATE_NONE); $scannedMessage = new XliffMessage('foo'); $scannedMessage->setDesc('foo'); $scannedMessage->setApproved(true); $scannedMessage->setState(XliffMessage::STATE_TRANSLATED); $scannedMessage1 = new Message('foo'); $scannedMessage1->setDesc('foo'); $existingMessage1 = clone $existingMessage; $scannedMessage1 = clone $scannedMessage; $existingMessage1->mergeScanned($scannedMessage1); $this->assertEquals('foo', $existingMessage1->getDesc()); $this->assertEquals('bar', $existingMessage1->getLocaleString()); $this->assertFalse($existingMessage1->isNew()); $this->assertEquals(array(), $existingMessage1->getSources()); $this->assertTrue($existingMessage1->isApproved()); $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage1->getState()); $existingMessage2 = clone $existingMessage; $existingMessage2->setDesc('bar'); $existingMessage2->setApproved(true); $existingMessage2->setState(XliffMessage::STATE_TRANSLATED); $scannedMessage2 = clone $scannedMessage; $scannedMessage2->setDesc('foo'); $scannedMessage2->setApproved(false); $scannedMessage2->setState(XliffMessage::STATE_NONE); $existingMessage2->mergeScanned($scannedMessage2); $this->assertEquals('bar', $existingMessage2->getDesc()); $this->assertFalse($existingMessage2->isNew()); $this->assertEquals(array(), $existingMessage2->getSources()); $this->assertTrue($existingMessage2->isApproved()); $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage2->getState()); $existingMessage3 = clone $existingMessage; $scannedMessage3 = clone $scannedMessage1; $existingMessage3->mergeScanned($scannedMessage3); $this->assertEquals('foo', $existingMessage3->getDesc()); $this->assertEquals('bar', $existingMessage3->getLocaleString()); $this->assertFalse($existingMessage3->isNew()); $this->assertEquals(array(), $existingMessage3->getSources()); $this->assertTrue($existingMessage3->isApproved()); $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage3->getState()); $existingMessage4 = clone $existingMessage; $existingMessage4->setDesc('bar'); $existingMessage4->setApproved(true); $existingMessage4->setState(XliffMessage::STATE_TRANSLATED); $scannedMessage4 = clone $scannedMessage1; $scannedMessage4->setDesc('foo'); $existingMessage4->mergeScanned($scannedMessage4); $this->assertEquals('bar', $existingMessage4->getDesc()); $this->assertEquals(array(), $existingMessage4->getSources()); $this->assertTrue($existingMessage4->isApproved()); $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage4->getState()); }
public function testMergeExisting() { $message = new Message('foo'); $message->setDesc('bar'); $existingMessage = new Message('foo'); $existingMessage->setLocaleString('foobar'); $existingMessage->setNew(false); $existingMessage->addSource(new FileSource('foo/bar')); $message->mergeExisting($existingMessage); $this->assertEquals('bar', $message->getDesc()); $this->assertEquals('foobar', $message->getLocaleString()); $this->assertFalse($message->isNew()); $this->assertEquals(array(), $message->getSources()); }
/** * @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 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); }
/** * 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); }
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); }