Browse files

JMSTranslationBundle translation extractor for column titles

  • Loading branch information...
1 parent b697002 commit 6b921cb99b3b9f43e85ca3335e68aa6080559723 nurikabe committed Dec 20, 2012
View
4 Resources/config/services.xml
@@ -57,5 +57,9 @@
</call>
</service>
+ <service id="grid.translation_extractor" class="APY\DataGridBundle\Translation\ColumnTitleAnnotationTranslationExtractor" public="false">
+ <tag name="jms_translation.file_visitor" />
+ </service>
+
</services>
</container>
View
15 Resources/doc/columns_configuration/annotations/column_annotation_property.md
@@ -65,10 +65,21 @@ class Product
**Note 2**: With the `values` attributes, if `type1` is found, the grid displays the value `Type 1`.
**Note 3**: If operators are not visible, filtering is performed with the default operator.
-## Translation title abbreviation
+## Title translation
+
+### Abbreviation
You can use abbreviations to define different titles for the column title and the filter label in the external search box.
-Exemple with the $type variable.
+Example with the $type variable.
`type` will only be search for the label in the external search box.
For the column title of the grid `type__abbr` will be search and `type` will be search if the abbrevation isn't defined.
+
+### Automatic translation extraction via JMSTranslationBundle
+
+A translation extractor is included that can discover column titles defined in the annotations. Running
+[JMSTranslationBundle](https://github.com/schmittjoh/JMSTranslationBundle) via the command line will automatically
+discover and catalogue titles as translation keys.
+
+
+
View
76 Translation/ColumnTitleAnnotationTranslationExtractor.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace APY\DataGridBundle\Translation;
+
+use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
+
+use APY\DataGridBundle\Grid\Mapping\Driver\Annotation;
+use APY\DataGridBundle\Grid\Mapping\Metadata\Manager;
+
+use JMS\TranslationBundle\Model\FileSource;
+use JMS\TranslationBundle\Model\Message;
+use JMS\TranslationBundle\Model\MessageCatalogue;
+use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
+
+class ColumnTitleAnnotationTranslationExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
+{
+ private $annotated;
+ private $catalogue;
+ private $parsedClassName;
+
+ public function beforeTraverse(array $nodes) {
+ $this->annotated = false;
+ $this->parsedClassName = null;
+ }
+
+ public function enterNode(\PHPParser_Node $node) {
+ if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
+ // Base namespace
+ $this->parsedClassName = $node->name->toString();
+ }
+ elseif ($node instanceof \PHPParser_Node_Stmt_UseUse) {
+ // Don't worry about classes that don't import the grid mapper
+ if ('APY_DataGridBundle_Grid_Mapping' == $node->name->toString('_')) {
+ $this->annotated = true;
+ }
+ }
+ elseif ($node instanceof \PHPParser_Node_Stmt_Class) {
+ // Append class name to base namespace
+ $this->parsedClassName .= '\\' . $node->name;
+ }
+ }
+
+ public function leaveNode(\PHPParser_Node $node) { }
+ public function afterTraverse(array $nodes) { }
+
+ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) { }
+
+ 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']) && isset($mappedField['title'])) {
+ $message = new Message($mappedField['title']);
+ $message->addSource(new FileSource((string) $file));
+ $catalogue->add($message);
+ }
+ }
+ }
+ }
+
+ public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $node) { }
+}

0 comments on commit 6b921cb

Please sign in to comment.