Author: dr
Date: Tue Feb 12 17:09:42 2008
New Revision: 7352

Log:
- Added an translation string extracter visitor to introspect which
  translatable strings exist in a template.

Added:
    trunk/TemplateTranslationTiein/src/visitors/
    trunk/TemplateTranslationTiein/src/visitors/string_extracter.php   (with 
props)
    trunk/TemplateTranslationTiein/tests/extracter.php   (with props)
    trunk/TemplateTranslationTiein/tests/test_files/
    trunk/TemplateTranslationTiein/tests/test_files/test.ezt
Modified:
    trunk/TemplateTranslationTiein/design/class_diagram.png
    trunk/TemplateTranslationTiein/src/template_translation_autoload.php
    trunk/TemplateTranslationTiein/tests/suite.php

Modified: trunk/TemplateTranslationTiein/design/class_diagram.png
==============================================================================
Binary files - no diff available.

Modified: trunk/TemplateTranslationTiein/src/template_translation_autoload.php
==============================================================================
--- trunk/TemplateTranslationTiein/src/template_translation_autoload.php 
[iso-8859-1] (original)
+++ trunk/TemplateTranslationTiein/src/template_translation_autoload.php 
[iso-8859-1] Tue Feb 12 17:09:42 2008
@@ -14,5 +14,6 @@
     'ezcTemplateTranslationManagerNotConfiguredException' => 
'TemplateTranslationTiein/exceptions/manager_not_configured.php',
     'ezcTemplateTranslationConfiguration'                 => 
'TemplateTranslationTiein/configuration.php',
     'ezcTemplateTranslationProvider'                      => 
'TemplateTranslationTiein/provider.php',
+    'ezcTemplateTranslationStringExtracter'               => 
'TemplateTranslationTiein/visitors/string_extracter.php',
 );
 ?>

Added: trunk/TemplateTranslationTiein/src/visitors/string_extracter.php
==============================================================================
--- trunk/TemplateTranslationTiein/src/visitors/string_extracter.php (added)
+++ trunk/TemplateTranslationTiein/src/visitors/string_extracter.php 
[iso-8859-1] Tue Feb 12 17:09:42 2008
@@ -1,0 +1,110 @@
+<?php
+/**
+ * File containing the ezcTemplateTranslationStringExtracter class
+ *
+ * @package TemplateTranslation
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @access private
+ */
+/**
+ * A visiter that can be used to extract translatable strings from a template.
+ *
+ * Implements the ezcTemplateTstNodeVisiter interface for visiting the nodes
+ * and generating the appropriate ast nodes for them.
+ *
+ * @package TemplateTranslation
+ * @version //autogen//
+ * @access private
+ */
+class ezcTemplateTranslationStringExtracter extends ezcTemplateTstWalker
+{
+    /**
+     * Contains the active default translation context
+     *
+     * @var string
+     */
+    public $translationContext;
+
+    /**
+     * Contans an array of arrays, where the key is the context, and the value 
an array of ezcTranslationData elements.
+     *
+     * @var array(string=>array(ezcTranslationData)
+     */
+    public $strings;
+
+    /**
+     * Initialize the transformer, after this send this object to the accept() 
method on a node.
+     *
+     * @param ezcTemplateParser $parser The main parser object.
+     */
+    public function __construct( ezcTemplateParser $parser )
+    {
+        $this->parser = $parser;
+        $this->strings = array();
+    }
+
+    /**
+     * visitTranslationTstNode
+     *
+     * @param ezcTemplateTranslationTstNode $node
+     * @return ezcTemplateNopAstNode
+     */
+    public function visitTranslationTstNode( ezcTemplateTranslationTstNode 
$node )
+    {
+        $string = $node->string->accept( $this )->value;
+        $comment = $node->comment ? $node->comment->accept( $this )->value : 
null;
+        $file = realpath( $node->source->stream );
+        $line = $node->string->startCursor->line;
+        $column = $node->string->startCursor->column + 1;
+        // check for the translation context. If we have one, we use it. If we
+        // don't have one, we check whether there is one set through a
+        // tr_context block. If not, we throw an exception.
+        if ( $node->context !== null )
+        {
+            $context = $node->context->accept( $this )->value;
+        }
+        else
+        {
+            if ( $this->translationContext !== null )
+            {
+                $context = $this->translationContext;
+            }
+            else
+            {
+                throw new ezcTemplateParserException( $node->source, 
$node->startCursor, $node->endCursor, 
ezcTemplateSourceToTstErrorMessages::MSG_NO_TRANSLATION_CONTEXT );
+            }
+        }
+
+        $this->strings[$context][] = new ezcTranslationData( $string, $string, 
$comment, ezcTranslationData::UNFINISHED, $file, $line, $column );
+    }
+
+    /**
+     * visitTranslationContextTstNode
+     *
+     * @param ezcTemplateTranslationContextTstNode $node
+     * @return ezcTemplateNopAstNode
+     */
+    public function visitTranslationContextTstNode( 
ezcTemplateTranslationContextTstNode $node )
+    {
+        $this->translationContext = $node->context->value;
+        return new ezcTemplateNopAstNode();
+    }
+
+    /**
+     * Returns an array of translation datamaps indexed by context
+     *
+     * @return array(string=>ezcTranslation)
+     */
+    function getTranslation()
+    {
+        $ret = array();
+        foreach( $this->strings as $context => $data )
+        {
+            $ret[$context] = new ezcTranslation( $data );
+        }
+        return $ret;
+    }
+}
+?>

Propchange: trunk/TemplateTranslationTiein/src/visitors/string_extracter.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/TemplateTranslationTiein/tests/extracter.php
==============================================================================
--- trunk/TemplateTranslationTiein/tests/extracter.php (added)
+++ trunk/TemplateTranslationTiein/tests/extracter.php [iso-8859-1] Tue Feb 12 
17:09:42 2008
@@ -1,0 +1,42 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package TemplateTranslationTiein
+ * @subpackage Tests
+ */
+
+/**
+ * @package TemplateTranslationTiein
+ * @subpackage Tests
+ */
+class ezcTemplateTranslationExtracterTest extends ezcTestCase
+{
+    function testExtracter()
+    {
+        $file = dirname( __FILE__ ) . '/test_files/test.ezt';
+        $source = new ezcTemplateSourceCode( $file, $file );
+        $source->load();
+
+        $parser = new ezcTemplateParser( $source, new ezcTemplate() );
+        $tst = $parser->parseIntoNodeTree();
+
+        $et = new ezcTemplateTranslationStringExtracter( $parser );
+        $eted = $tst->accept( $et );
+
+        $tr = $et->getTranslation();
+        self::assertEquals( 
+            array( 'een', 'twee', 'drie', 'vier', 'vijf', 'zes', 'zeven', 
'acht', 'negen', 'tien', 'elf' ),
+            array_keys( $this->readAttribute( $tr['test'], 'translationMap' ) 
) 
+        );
+    }
+
+    public static function suite()
+    {
+         return new PHPUnit_Framework_TestSuite( 
'ezcTemplateTranslationExtracterTest' );
+    }
+}
+
+?>

Propchange: trunk/TemplateTranslationTiein/tests/extracter.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/TemplateTranslationTiein/tests/suite.php
==============================================================================
--- trunk/TemplateTranslationTiein/tests/suite.php [iso-8859-1] (original)
+++ trunk/TemplateTranslationTiein/tests/suite.php [iso-8859-1] Tue Feb 12 
17:09:42 2008
@@ -13,6 +13,7 @@
  */
 require_once 'configuration.php';
 require_once 'provider.php';
+require_once 'extracter.php';
 
 /**
  * @package TemplateTranslationTiein
@@ -27,6 +28,7 @@
 
         $this->addTest( ezcTemplateTranslationConfigurationTest::suite() );
         $this->addTest( ezcTemplateTranslationProviderTest::suite() );
+        $this->addTest( ezcTemplateTranslationExtracterTest::suite() );
     }
 
     public static function suite()

Added: trunk/TemplateTranslationTiein/tests/test_files/test.ezt
==============================================================================
--- trunk/TemplateTranslationTiein/tests/test_files/test.ezt (added)
+++ trunk/TemplateTranslationTiein/tests/test_files/test.ezt [iso-8859-1] Tue 
Feb 12 17:09:42 2008
@@ -1,0 +1,40 @@
+{var $twee, $drie, $zes, $zeven, $acht, $elf}
+{tr_context "test"}
+
+{cache_block}
+{tr "een"}
+{/cache_block}
+
+{capture $twee}
+{tr "twee"}
+{/capture}
+
+{switch $drie}
+{case 3}{tr "drie"}{/case}
+{default}{tr "vier"}{/default}
+{/switch}
+
+{cache_block}
+{dynamic}
+{tr "vijf"}
+{/dynamic}
+{/cache_block}
+
+{foreach array() as $zes}
+{tr "zes"}
+{/foreach}
+
+{if $zeven}
+{tr "zeven"}
+{elseif $acht}
+{tr "acht"}
+{else}
+{tr "negen"}
+{/if}
+
+{tr "tien"}
+
+{while $elf}
+{tr "elf"}
+{delimiter}{tr "twaalf"}{/delimiter}
+{/while}


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to