Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
EZP-26617: Made PlatformUI js & hbt extractors usable on any bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier authored and yannickroger committed Nov 21, 2016
1 parent 7e4df7b commit 892a31d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Resources/config/services.yml
Expand Up @@ -252,14 +252,12 @@ services:
## Translation extractor service
ezsystems.platformui.translation.extractor.handlebars:
class: "%ezsystems.platformui.translation.extractor.handlebars.class%"
arguments: ["@=service('kernel').locateResource('@eZPlatformUIBundle/Resources/public/templates')"]
tags:
- { name: translation.extractor, alias: handlebars }

ezsystems.platformui.translation.extractor.javascript:
class: "%ezsystems.platformui.translation.extractor.javascript.class%"
arguments:
- "@=service('kernel').locateResource('@eZPlatformUIBundle/Resources/public/js')"
- "@=service('kernel').locateResource('@eZPlatformUIBundle/bin/Translation/translation_dumper.js')"
tags:
- { name: translation.extractor, alias: javascript }
10 changes: 3 additions & 7 deletions Tests/Translation/HandleBarsExtractorTest.php
Expand Up @@ -69,9 +69,9 @@ public function getExtractData()
*/
public function testExtractWithFiles($resource)
{
$extractor = new HandleBarsExtractor($resource);
$extractor = new HandleBarsExtractor();
$catalogue = new MessageCatalogue('en');
$extractor->extract('/tmp/whatever', $catalogue);
$extractor->extract($resource, $catalogue);

$this->assertTrue($catalogue->has('test.translation.title', 'testdomain'));
$this->assertEquals(
Expand All @@ -85,10 +85,6 @@ public function testExtractWithFiles($resource)
*/
public function resourceProvider()
{
$directory = __DIR__ . '/../fixtures/extractor/';

return array(
array($directory . 'with_translations.hbt'),
);
return [[__DIR__ . '/../fixtures/extractor/Resources/views']];
}
}
15 changes: 6 additions & 9 deletions Tests/Translation/JavascriptExtractorTest.php
Expand Up @@ -16,11 +16,13 @@ class JavascriptExtractorTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider resourceProvider
*/
public function testExtractWithFiles($resource, $translationDumperPath)
public function testExtractWithFiles($resource)
{
$extractor = new JavascriptExtractor($resource, $translationDumperPath);
$extractor = new JavascriptExtractor(
$translationDumperPath = __DIR__ . '/../../bin/Translation/translation_dumper.js'
);
$catalogue = new MessageCatalogue('en');
$extractor->extract('/tmp/whatever', $catalogue);
$extractor->extract($resource, $catalogue);

$this->assertTrue($catalogue->has('test.translation.result1', 'testdomain'));
$this->assertEquals(
Expand All @@ -36,11 +38,6 @@ public function testExtractWithFiles($resource, $translationDumperPath)
*/
public function resourceProvider()
{
$directory = __DIR__ . '/../fixtures/extractor/';
$translationDumperPath = __DIR__ . '/../../bin/Translation/translation_dumper.js';

return array(
array($directory . 'with_translation.js', $translationDumperPath),
);
return [[__DIR__ . '/../fixtures/extractor/Resources/views']];
}
}
19 changes: 3 additions & 16 deletions Translation/HandleBarsExtractor.php
Expand Up @@ -9,14 +9,13 @@
namespace EzSystems\PlatformUIBundle\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;

/**
* HandleBarsExtractor extracts translation messages from a PlatformUI's .hbt template.
*/
class HandleBarsExtractor extends AbstractFileExtractor implements ExtractorInterface
class HandleBarsExtractor extends PlatformUIExtractor implements ExtractorInterface
{
/**
* Default domain for found messages.
Expand All @@ -32,25 +31,13 @@ class HandleBarsExtractor extends AbstractFileExtractor implements ExtractorInte
*/
private $prefix = '';

/**
* Path of hbt template files.
*
* @var string
*/
private $handleBarsResource;

public function __construct($handleBarsResource)
{
$this->handleBarsResource = $handleBarsResource;
}

/**
* {@inheritdoc}
*/
public function extract($resource, MessageCatalogue $catalogue)
{
// Forcing usage of handlebars resources. What is provided as parameter is only for twig and php files
$files = $this->extractFiles($this->handleBarsResource);
$this->updateResource($resource, 'Resources/public/templates');
$files = $this->extractFiles($resource);

foreach ($files as $file) {
$fileContent = file_get_contents($file->getPathname());
Expand Down
24 changes: 8 additions & 16 deletions Translation/JavascriptExtractor.php
Expand Up @@ -9,7 +9,7 @@
namespace EzSystems\PlatformUIBundle\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
use Symfony\Component\Process\Process;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;

Expand All @@ -18,7 +18,7 @@
*
* Note: this script relies on a nodejs script: `/bin/Translation/translation_dumper.js`
*/
class JavascriptExtractor extends AbstractFileExtractor implements ExtractorInterface
class JavascriptExtractor extends PlatformUIExtractor implements ExtractorInterface
{
/**
* Default domain for found messages.
Expand All @@ -34,23 +34,15 @@ class JavascriptExtractor extends AbstractFileExtractor implements ExtractorInte
*/
private $prefix = '';

/**
* Path of js files.
*
* @var string
*/
private $javascriptResource;

/**
* Path of translation dumper.
*
* @var string
*/
private $translationDumperPath;

public function __construct($javascriptResource, $translationDumperPath)
public function __construct($translationDumperPath)
{
$this->javascriptResource = $javascriptResource;
$this->translationDumperPath = $translationDumperPath;
}

Expand All @@ -59,14 +51,14 @@ public function __construct($javascriptResource, $translationDumperPath)
*/
public function extract($resource, MessageCatalogue $catalogue)
{
// Forcing usage of javascript resources. What is provided as parameter is only for twig and php files
$files = $this->extractFiles($this->javascriptResource);
$this->updateResource($resource, 'Resources/public/js');
$files = $this->extractFiles($resource);

foreach ($files as $file) {
$command = 'node ' . $this->translationDumperPath . ' ' . escapeshellarg($file);
$json = shell_exec($command);
$process = new Process('node ' . $this->translationDumperPath . ' ' . escapeshellarg($file));
$process->run();

$result = json_decode($json);
$result = json_decode($process->getOutput());

if ($result && $result->translationsFound) {
foreach ($result->translationsFound as $translation) {
Expand Down
48 changes: 48 additions & 0 deletions Translation/PlatformUIExtractor.php
@@ -0,0 +1,48 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Translation;

use Symfony\Component\Translation\Extractor\AbstractFileExtractor;

abstract class PlatformUIExtractor extends AbstractFileExtractor
{
private $pathSuffix = 'Resources/views';

/**
* Updates the resource by replacing the path to templates (Resource/views)
* with the path handled by the extractor.
*
* @param string $resource
* @param string $path The path 'Resources/views' should be replaced with
*/
protected function updateResource(&$resource, $path)
{
if (is_string($resource) && strpos($resource, $this->pathSuffix) !== null) {
$resource = $this->replacePath($resource, $path);
} elseif (is_array($resource)) {
$resource = array_map(
function ($value) use ($path) {
if (is_string($value) && strpos($value, $this->pathSuffix) !== null) {
return $this->replacePath($value, $path);
} else {
return $value;
}
},
$resource
);
}
}

/**
* @param string $in
* @param string $path
* @return string
*/
private function replacePath(&$in, $path)
{
return str_replace($this->pathSuffix, $path, $in);
}
}

0 comments on commit 892a31d

Please sign in to comment.