From 4f5b44bd3bd490309eb2ba7b44df4769816ba729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 3 Aug 2013 19:26:54 +0200 Subject: twig implementation --- vendor/umpirsky/twig-gettext-extractor/.gitignore | 3 + vendor/umpirsky/twig-gettext-extractor/.travis.yml | 10 ++ vendor/umpirsky/twig-gettext-extractor/LICENSE | 19 ++++ vendor/umpirsky/twig-gettext-extractor/README.md | 49 ++++++++ .../Twig/Gettext/Extractor.php | 95 ++++++++++++++++ .../Twig/Gettext/Loader/Filesystem.php | 58 ++++++++++ .../Gettext/Routing/Generator/UrlGenerator.php | 39 +++++++ .../Twig/Gettext/Test/ExtractorTest.php | 123 +++++++++++++++++++++ .../Twig/Gettext/Test/Fixtures/twig/empty.twig | 1 + .../Twig/Gettext/Test/Fixtures/twig/plural.twig | 5 + .../Twig/Gettext/Test/Fixtures/twig/singular.twig | 9 ++ .../umpirsky/twig-gettext-extractor/composer.json | 30 +++++ .../twig-gettext-extractor/phpunit.xml.dist | 14 +++ .../twig-gettext-extractor/twig-gettext-extractor | 58 ++++++++++ 14 files changed, 513 insertions(+) create mode 100644 vendor/umpirsky/twig-gettext-extractor/.gitignore create mode 100644 vendor/umpirsky/twig-gettext-extractor/.travis.yml create mode 100644 vendor/umpirsky/twig-gettext-extractor/LICENSE create mode 100644 vendor/umpirsky/twig-gettext-extractor/README.md create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Extractor.php create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Loader/Filesystem.php create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Routing/Generator/UrlGenerator.php create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/empty.twig create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/plural.twig create mode 100644 vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/singular.twig create mode 100644 vendor/umpirsky/twig-gettext-extractor/composer.json create mode 100644 vendor/umpirsky/twig-gettext-extractor/phpunit.xml.dist create mode 100755 vendor/umpirsky/twig-gettext-extractor/twig-gettext-extractor (limited to 'vendor/umpirsky') diff --git a/vendor/umpirsky/twig-gettext-extractor/.gitignore b/vendor/umpirsky/twig-gettext-extractor/.gitignore new file mode 100644 index 00000000..61381e45 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/.gitignore @@ -0,0 +1,3 @@ +vendor +phpunit.xml +composer.lock diff --git a/vendor/umpirsky/twig-gettext-extractor/.travis.yml b/vendor/umpirsky/twig-gettext-extractor/.travis.yml new file mode 100644 index 00000000..0c9bce01 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/.travis.yml @@ -0,0 +1,10 @@ +language: php + +before_script: + - curl -s http://getcomposer.org/installer | php + - php composer.phar install --dev + +php: + - 5.3 + - 5.4 + diff --git a/vendor/umpirsky/twig-gettext-extractor/LICENSE b/vendor/umpirsky/twig-gettext-extractor/LICENSE new file mode 100644 index 00000000..df9dd107 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) Саша Стаменковић + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/vendor/umpirsky/twig-gettext-extractor/README.md b/vendor/umpirsky/twig-gettext-extractor/README.md new file mode 100644 index 00000000..34eff88c --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/README.md @@ -0,0 +1,49 @@ +Twig Gettext Extractor [![Build Status](https://secure.travis-ci.org/umpirsky/Twig-Gettext-Extractor.png?branch=master)](http://travis-ci.org/umpirsky/Twig-Gettext-Extractor) +====================== + +The Twig Gettext Extractor is [Poedit](http://www.poedit.net/download.php) +friendly tool which extracts translations from twig templates. + +## Installation + +The recommended way to install Twig Gettext Extractor is through +[composer](http://getcomposer.org). + +```json +{ + "require": { + "umpirsky/twig-gettext-extractor": "1.1.*" + } +} +``` + +## Setup + +By default, Poedit does not have the ability to parse Twig templates. +This can be resolved by adding an additional parser (Edit > Preferences > Parsers) +with the following options: + +- Language: `Twig` +- List of extensions: `*.twig` +- Invocation: + - Parser command: `/vendor/bin/twig-gettext-extractor --sort-output --force-po -o %o %C %K -L PHP --files %F` + - An item in keyword list: `-k%k` + - An item in input file list: `%f` + - Source code charset: `--from-code=%c` + + + +Now you can update your catalog and Poedit will synchronize it with your twig +templates. + +## Tests + +To run the test suite, you need [composer](http://getcomposer.org) and +[PHPUnit](https://github.com/sebastianbergmann/phpunit). + + $ composer install --dev + $ phpunit + +## License + +Twig Gettext Extractor is licensed under the MIT license. diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Extractor.php b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Extractor.php new file mode 100644 index 00000000..e7fa1af2 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Extractor.php @@ -0,0 +1,95 @@ + + * + * 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 Саша Стаменковић + */ +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()); + } +} diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Loader/Filesystem.php b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Loader/Filesystem.php new file mode 100644 index 00000000..b011b032 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Loader/Filesystem.php @@ -0,0 +1,58 @@ + + * + * 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 Саша Стаменковић + */ +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'; + } +} diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Routing/Generator/UrlGenerator.php b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Routing/Generator/UrlGenerator.php new file mode 100644 index 00000000..9e3431bd --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Routing/Generator/UrlGenerator.php @@ -0,0 +1,39 @@ + + * + * 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 Саша Стаменковић + */ +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; + } +} diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php new file mode 100644 index 00000000..d467835f --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php @@ -0,0 +1,123 @@ + + * + * 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 Саша Стаменковић + */ +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()); + } + } +} diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/empty.twig b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/empty.twig new file mode 100644 index 00000000..05f0d26a --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/empty.twig @@ -0,0 +1 @@ +Nothing to translate here. diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/plural.twig b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/plural.twig new file mode 100644 index 00000000..f9754ff4 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/plural.twig @@ -0,0 +1,5 @@ +{% trans %} + Hey {{ name }}, I have one apple. +{% plural apple_count %} + Hey {{ name }}, I have {{ count }} apples. +{% endtrans %} diff --git a/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/singular.twig b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/singular.twig new file mode 100644 index 00000000..d757cf90 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/singular.twig @@ -0,0 +1,9 @@ +{% trans "Hello World!" %} + +{% trans %} + Hello World! +{% endtrans %} + +{% trans %} + Hello {{ name }}! +{% endtrans %} diff --git a/vendor/umpirsky/twig-gettext-extractor/composer.json b/vendor/umpirsky/twig-gettext-extractor/composer.json new file mode 100644 index 00000000..7cda5f73 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/composer.json @@ -0,0 +1,30 @@ +{ + "name": "umpirsky/twig-gettext-extractor", + "type": "application", + "description": "The Twig Gettext Extractor is Poedit friendly tool which extracts translations from twig templates.", + "license": "MIT", + "authors": [ + { + "name": "Саша Стаменковић", + "email": "umpirsky@gmail.com" + } + ], + "require": { + "php": ">=5.3.3", + "twig/twig": ">=1.2.0,<2.0-dev", + "twig/extensions": "1.0.*", + "symfony/twig-bridge": ">=2.0,<3.0", + "symfony/routing": ">=2.0,<3.0", + "symfony/filesystem": ">=2.0,<3.0", + "symfony/translation": ">=2.0,<3.0", + "symfony/form": ">=2.0,<3.0" + }, + "require-dev": { + "symfony/config": "2.1.*" + }, + "minimum-stability": "dev", + "autoload": { + "psr-0": { "Twig\\Gettext": "." } + }, + "bin": ["twig-gettext-extractor"] +} \ No newline at end of file diff --git a/vendor/umpirsky/twig-gettext-extractor/phpunit.xml.dist b/vendor/umpirsky/twig-gettext-extractor/phpunit.xml.dist new file mode 100644 index 00000000..56fdc6b0 --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/phpunit.xml.dist @@ -0,0 +1,14 @@ + + + + + + ./Twig/Gettext/Test/ + + + diff --git a/vendor/umpirsky/twig-gettext-extractor/twig-gettext-extractor b/vendor/umpirsky/twig-gettext-extractor/twig-gettext-extractor new file mode 100755 index 00000000..6cc97c1d --- /dev/null +++ b/vendor/umpirsky/twig-gettext-extractor/twig-gettext-extractor @@ -0,0 +1,58 @@ +#!/usr/bin/env php + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Extracts translations from twig templates. + * + * @author Саша Стаменковић + */ + +if (file_exists($a = __DIR__.'/../../autoload.php')) { + require_once $a; +} else { + require_once __DIR__.'/vendor/autoload.php'; +} + +$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem('/'), array( + 'cache' => '/tmp/cache/'.uniqid(), + 'auto_reload' => true +)); +$twig->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension( + new Symfony\Component\Translation\Translator(null) +)); +$twig->addExtension(new Twig_Extensions_Extension_I18n()); +$twig->addExtension(new Symfony\Bridge\Twig\Extension\RoutingExtension( + new Twig\Gettext\Routing\Generator\UrlGenerator() +)); +$twig->addExtension(new Symfony\Bridge\Twig\Extension\FormExtension( + new Symfony\Bridge\Twig\Form\TwigRenderer( + new Symfony\Bridge\Twig\Form\TwigRendererEngine() + ) +)); +// You can add more extensions here. + +array_shift($_SERVER['argv']); +$addTemplate = false; + +$extractor = new Twig\Gettext\Extractor($twig); + +foreach ($_SERVER['argv'] as $arg) { + if ('--files' == $arg) { + $addTemplate = true; + } else if ($addTemplate) { + $extractor->addTemplate(getcwd().DIRECTORY_SEPARATOR.$arg); + } else { + $extractor->addGettextParameter($arg); + } +} + +$extractor->extract(); -- cgit v1.2.3