forked from wallabag/wallabag
add Twig & refactor poche
This commit is contained in:
95
inc/Twig/Extensions/Gettext/Extractor.php
Normal file
95
inc/Twig/Extensions/Gettext/Extractor.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Twig Gettext utility.
|
||||
*
|
||||
* (c) Саша Стаменковић <umpirsky@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Gettext;
|
||||
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
/**
|
||||
* Extracts translations from twig templates.
|
||||
*
|
||||
* @author Саша Стаменковић <umpirsky@gmail.com>
|
||||
*/
|
||||
class Extractor
|
||||
{
|
||||
/**
|
||||
* @var \Twig_Environment
|
||||
*/
|
||||
protected $environment;
|
||||
|
||||
/**
|
||||
* Template cached file names.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $templates;
|
||||
|
||||
/**
|
||||
* Gettext parameters.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $parameters;
|
||||
|
||||
public function __construct(\Twig_Environment $environment)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
protected function reset()
|
||||
{
|
||||
$this->templates = array();
|
||||
$this->parameters = array();
|
||||
}
|
||||
|
||||
public function addTemplate($path)
|
||||
{
|
||||
$this->environment->loadTemplate($path);
|
||||
$this->templates[] = $this->environment->getCacheFilename($path);
|
||||
}
|
||||
|
||||
public function addGettextParameter($parameter)
|
||||
{
|
||||
$this->parameters[] = $parameter;
|
||||
}
|
||||
|
||||
public function setGettextParameters(array $parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
|
||||
public function extract()
|
||||
{
|
||||
$command = 'xgettext';
|
||||
$command .= ' '.join(' ', $this->parameters);
|
||||
$command .= ' '.join(' ', $this->templates);
|
||||
|
||||
$error = 0;
|
||||
$output = system($command, $error);
|
||||
if (0 !== $error) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'Gettext command "%s" failed with error code %s and output: %s',
|
||||
$command,
|
||||
$error,
|
||||
$output
|
||||
));
|
||||
}
|
||||
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$filesystem = new Filesystem();
|
||||
$filesystem->remove($this->environment->getCache());
|
||||
}
|
||||
}
|
||||
58
inc/Twig/Extensions/Gettext/Loader/Filesystem.php
Normal file
58
inc/Twig/Extensions/Gettext/Loader/Filesystem.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Twig Gettext utility.
|
||||
*
|
||||
* (c) Саша Стаменковић <umpirsky@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Gettext\Loader;
|
||||
|
||||
/**
|
||||
* Loads template from the filesystem.
|
||||
*
|
||||
* @author Саша Стаменковић <umpirsky@gmail.com>
|
||||
*/
|
||||
class Filesystem extends \Twig_Loader_Filesystem
|
||||
{
|
||||
/**
|
||||
* Hacked find template to allow loading templates by absolute path.
|
||||
*
|
||||
* @param string $name template name or absolute path
|
||||
*/
|
||||
protected function findTemplate($name)
|
||||
{
|
||||
// normalize name
|
||||
$name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
|
||||
|
||||
if (isset($this->cache[$name])) {
|
||||
return $this->cache[$name];
|
||||
}
|
||||
|
||||
$this->validateName($name);
|
||||
|
||||
$namespace = '__main__';
|
||||
if (isset($name[0]) && '@' == $name[0]) {
|
||||
if (false === $pos = strpos($name, '/')) {
|
||||
throw new \InvalidArgumentException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
|
||||
}
|
||||
|
||||
$namespace = substr($name, 1, $pos - 1);
|
||||
|
||||
$name = substr($name, $pos + 1);
|
||||
}
|
||||
|
||||
if (!isset($this->paths[$namespace])) {
|
||||
throw new \Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
|
||||
}
|
||||
|
||||
if (is_file($name)) {
|
||||
return $this->cache[$name] = $name;
|
||||
}
|
||||
|
||||
return __DIR__.'/../Test/Fixtures/twig/empty.twig';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Twig Gettext utility.
|
||||
*
|
||||
* (c) Саша Стаменковић <umpirsky@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Gettext\Routing\Generator;
|
||||
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* Dummy url generator.
|
||||
*
|
||||
* @author Саша Стаменковић <umpirsky@gmail.com>
|
||||
*/
|
||||
class UrlGenerator implements UrlGeneratorInterface
|
||||
{
|
||||
protected $context;
|
||||
|
||||
public function generate($name, $parameters = array(), $absolute = false)
|
||||
{
|
||||
}
|
||||
|
||||
public function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
public function setContext(RequestContext $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
}
|
||||
123
inc/Twig/Extensions/Gettext/Test/ExtractorTest.php
Normal file
123
inc/Twig/Extensions/Gettext/Test/ExtractorTest.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Twig Gettext utility.
|
||||
*
|
||||
* (c) Саша Стаменковић <umpirsky@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Gettext\Test;
|
||||
|
||||
use Twig\Gettext\Extractor;
|
||||
use Twig\Gettext\Loader\Filesystem;
|
||||
use Symfony\Component\Translation\Loader\PoFileLoader;
|
||||
|
||||
/**
|
||||
* @author Саша Стаменковић <umpirsky@gmail.com>
|
||||
*/
|
||||
class ExtractorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Twig_Environment
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* @var PoFileLoader
|
||||
*/
|
||||
protected $loader;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->twig = new \Twig_Environment(new Filesystem('/'), array(
|
||||
'cache' => '/tmp/cache/'.uniqid(),
|
||||
'auto_reload' => true
|
||||
));
|
||||
$this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
|
||||
|
||||
$this->loader = new PoFileLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testExtractDataProvider
|
||||
*/
|
||||
public function testExtract(array $templates, array $parameters, array $messages)
|
||||
{
|
||||
$extractor = new Extractor($this->twig);
|
||||
|
||||
foreach ($templates as $template) {
|
||||
$extractor->addTemplate($template);
|
||||
}
|
||||
foreach ($parameters as $parameter) {
|
||||
$extractor->addGettextParameter($parameter);
|
||||
}
|
||||
|
||||
$extractor->extract();
|
||||
|
||||
$catalog = $this->loader->load($this->getPotFile(), null);
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$this->assertTrue(
|
||||
$catalog->has($message),
|
||||
sprintf('Message "%s" not found in catalog.', $message)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testExtractDataProvider()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
__DIR__.'/Fixtures/twig/singular.twig',
|
||||
__DIR__.'/Fixtures/twig/plural.twig',
|
||||
),
|
||||
$this->getGettextParameters(),
|
||||
array(
|
||||
'Hello %name%!',
|
||||
'Hello World!',
|
||||
'Hey %name%, I have one apple.',
|
||||
'Hey %name%, I have %count% apples.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testExtractNoTranslations()
|
||||
{
|
||||
$extractor = new Extractor($this->twig);
|
||||
|
||||
$extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig');
|
||||
$extractor->setGettextParameters($this->getGettextParameters());
|
||||
|
||||
$extractor->extract();
|
||||
|
||||
$catalog = $this->loader->load($this->getPotFile(), null);
|
||||
|
||||
$this->assertEmpty($catalog->all('messages'));
|
||||
}
|
||||
|
||||
private function getPotFile()
|
||||
{
|
||||
return __DIR__.'/Fixtures/messages.pot';
|
||||
}
|
||||
|
||||
private function getGettextParameters()
|
||||
{
|
||||
return array(
|
||||
'--force-po',
|
||||
'-o',
|
||||
$this->getPotFile(),
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
if (file_exists($this->getPotFile())) {
|
||||
unlink($this->getPotFile());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
Nothing to translate here.
|
||||
@ -0,0 +1,5 @@
|
||||
{% trans %}
|
||||
Hey {{ name }}, I have one apple.
|
||||
{% plural apple_count %}
|
||||
Hey {{ name }}, I have {{ count }} apples.
|
||||
{% endtrans %}
|
||||
@ -0,0 +1,9 @@
|
||||
{% trans "Hello World!" %}
|
||||
|
||||
{% trans %}
|
||||
Hello World!
|
||||
{% endtrans %}
|
||||
|
||||
{% trans %}
|
||||
Hello {{ name }}!
|
||||
{% endtrans %}
|
||||
Reference in New Issue
Block a user