Author: Michel Salib (michelsalib@hotmail.com)
Inheritance: implements Symfony\Component\Translation\Extractor\ExtractorInterface
Example #1
0
 /**
  * @expectedException              \Twig_Error
  * @expectedExceptionMessageRegExp /Unclosed "block" in "extractor(\/|\\)syntax_error\.twig" at line 1/
  */
 public function testExtractSyntaxError()
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
     $twig->addExtension(new TranslationExtension($this->getMock('Symfony\\Component\\Translation\\TranslatorInterface')));
     $extractor = new TwigExtractor($twig);
     $extractor->extract(__DIR__ . '/../Fixtures', new MessageCatalogue('en'));
 }
 /**
  * Prepares and finds all the translated and untranslated string in tempates and controllers
  */
 public function prepare(Application $app, $locale)
 {
     $templatesPath = APP_DIR . '/templates';
     $untranslatedMessagesFile = APP_DIR . '/locales/' . $locale . '_untranslated.yml';
     $extractor = new TwigExtractor($app['twig']);
     /***** All translations *****/
     $catalogueAll = new MessageCatalogue($locale);
     $extractor->extract($templatesPath, $catalogueAll);
     $allMessages = $catalogueAll->all('messages');
     // String from controller, controller provider, etc.
     $finder = new Finder();
     $finder->files()->in(ROOT_DIR . '/src');
     foreach ($finder as $file) {
         $fileMessageStrings = array();
         $filePath = $file->getRealpath();
         $fileContent = file_get_contents($filePath);
         $pregMatch = "#->trans.*\\(\\s*'(.+?)(?=')#m";
         preg_match_all($pregMatch, $fileContent, $matches);
         $matches = $matches[1];
         if ($matches) {
             foreach ($matches as $match) {
                 $fileMessageStrings[] = $match;
             }
         }
         if ($fileMessageStrings) {
             foreach ($fileMessageStrings as $fileMessageString) {
                 if (!isset($allMessages[$fileMessageString])) {
                     $allMessages[$fileMessageString] = $fileMessageString;
                 }
             }
         }
     }
     /***** Already translated *****/
     $app['application.translator']->setLocale($locale, $ignoreUntranslated = true);
     $translatedMessages = $app['translator']->getMessages($locale);
     $translatedMessages = $translatedMessages['messages'];
     /***** Untranslated *****/
     $untranslatedMessages = array();
     if ($allMessages) {
         foreach ($allMessages as $singleMessageKey => $singleMessage) {
             if (!isset($translatedMessages[$singleMessageKey])) {
                 $untranslatedMessages[$singleMessageKey] = $singleMessage;
             }
         }
     }
     if (!empty($untranslatedMessages)) {
         $dumper = new Dumper();
         $yaml = $dumper->dump($untranslatedMessages, 1);
         if (file_exists($untranslatedMessagesFile)) {
             unlink($untranslatedMessagesFile);
         }
         file_put_contents($untranslatedMessagesFile, $yaml);
     }
     return array('allMessages' => $allMessages, 'translatedMessages' => $translatedMessages, 'untranslatedMessages' => $untranslatedMessages);
 }
 /**
  * @dataProvider resourceProvider
  */
 public function testExtractWithFiles($resource)
 {
     $loader = new \Twig_Loader_Array(array());
     $twig = new \Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false, 'autoescape' => false));
     $twig->addExtension(new TranslationExtension($this->getMock('Symfony\\Component\\Translation\\TranslatorInterface')));
     $extractor = new TwigExtractor($twig);
     $catalogue = new MessageCatalogue('en');
     $extractor->extract($resource, $catalogue);
     $this->assertTrue($catalogue->has('Hi!', 'messages'));
     $this->assertEquals('Hi!', $catalogue->get('Hi!', 'messages'));
 }
 /**
  * @dataProvider getExtractData
  */
 public function testExtract($template, $messages)
 {
     $loader = new \Twig_Loader_Array(array());
     $twig = new \Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false, 'autoescape' => false));
     $twig->addExtension(new TranslationExtension($this->getMock('Symfony\\Component\\Translation\\TranslatorInterface')));
     $extractor = new TwigExtractor($twig);
     $extractor->setPrefix('prefix');
     $catalogue = new MessageCatalogue('en');
     $m = new \ReflectionMethod($extractor, 'extractTemplate');
     $m->setAccessible(true);
     $m->invoke($extractor, $template, $catalogue);
     foreach ($messages as $key => $domain) {
         $this->assertTrue($catalogue->has($key, $domain));
         $this->assertEquals('prefix' . $key, $catalogue->get($key, $domain));
     }
 }
Example #5
0
 public function testFilterExtraction()
 {
     // 1.Arrange
     // a node using trans filter : {{ 'new key' | trans({}, 'domain') }}
     $transNode = new \Twig_Node_Expression_Filter(
         new \Twig_Node_Expression_Constant('first key', 0),
         new \Twig_Node_Expression_Constant('trans', 0),
         new \Twig_Node(array(
             1 => new \Twig_Node_Expression_Constant('domain', 0)
         )), array(), 0);
     // a trans block : {% trans from 'domain' %}second key{% endtrans %}
     $transBlock = new TransNode(
         new \Twig_Node(array(), array('data' => 'second key')),
         new \Twig_Node(array(), array('value' => 'domain'))
     );
     // mock the twig environment
     $twig = $this->getMock('Twig_Environment');
     $twig->expects($this->once())
          ->method('tokenize')
          ->will($this->returnValue(new \Twig_TokenStream(array())))
     ;
     $twig->expects($this->once())
          ->method('parse')
          ->will($this->returnValue(
              new \Twig_Node(array(
                  new \Twig_Node_Text('stub text', 0),
                  new \Twig_Node_Print($transNode,0),
                  $transBlock,
              ))
          ))
     ;
     // prepare extractor and catalogue
     $extractor = new TwigExtractor($twig);
     $extractor->setPrefix('prefix');
     $catalogue = new MessageCatalogue('en');
     
     // 2.Act
     $extractor->extract(__DIR__.'/../Fixtures/Resources/views/', $catalogue);
     
     // 3.Assert
     $this->assertTrue($catalogue->has('first key', 'domain'), '->extract() should find at leat "first key" message in the domain "domain"');
     $this->assertTrue($catalogue->has('second key', 'domain'), '->extract() should find at leat "second key" message in the domain "domain"');
     $this->assertEquals(2, count($catalogue->all('domain')), '->extract() should find 2 translations in the domain "domain"');
     $this->assertEquals('prefixfirst key', $catalogue->get('first key', 'domain'), '->extract() should apply "prefix" as prefix');
 }
Example #6
0
 public function __construct(\Twig_Environment $twig, TwitalLoader $twital)
 {
     parent::__construct($twig);
     $this->twital = $twital;
 }