aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/umpirsky/twig-gettext-extractor
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/umpirsky/twig-gettext-extractor')
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/.gitignore3
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/.travis.yml10
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/LICENSE19
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/README.md49
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Extractor.php95
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Loader/Filesystem.php58
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Routing/Generator/UrlGenerator.php39
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php123
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/empty.twig1
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/plural.twig5
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/Fixtures/twig/singular.twig9
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/composer.json30
-rw-r--r--vendor/umpirsky/twig-gettext-extractor/phpunit.xml.dist14
-rwxr-xr-xvendor/umpirsky/twig-gettext-extractor/twig-gettext-extractor58
14 files changed, 513 insertions, 0 deletions
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 @@
1vendor
2phpunit.xml
3composer.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 @@
1language: php
2
3before_script:
4 - curl -s http://getcomposer.org/installer | php
5 - php composer.phar install --dev
6
7php:
8 - 5.3
9 - 5.4
10
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 @@
1Copyright (c) Саша Стаменковић <umpirsky@gmail.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy
4of this software and associated documentation files (the "Software"), to deal
5in the Software without restriction, including without limitation the rights
6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7copies of the Software, and to permit persons to whom the Software is furnished
8to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all
11copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19THE 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 @@
1Twig Gettext Extractor [![Build Status](https://secure.travis-ci.org/umpirsky/Twig-Gettext-Extractor.png?branch=master)](http://travis-ci.org/umpirsky/Twig-Gettext-Extractor)
2======================
3
4The Twig Gettext Extractor is [Poedit](http://www.poedit.net/download.php)
5friendly tool which extracts translations from twig templates.
6
7## Installation
8
9The recommended way to install Twig Gettext Extractor is through
10[composer](http://getcomposer.org).
11
12```json
13{
14 "require": {
15 "umpirsky/twig-gettext-extractor": "1.1.*"
16 }
17}
18```
19
20## Setup
21
22By default, Poedit does not have the ability to parse Twig templates.
23This can be resolved by adding an additional parser (Edit > Preferences > Parsers)
24with the following options:
25
26- Language: `Twig`
27- List of extensions: `*.twig`
28- Invocation:
29 - Parser command: `<project>/vendor/bin/twig-gettext-extractor --sort-output --force-po -o %o %C %K -L PHP --files %F`
30 - An item in keyword list: `-k%k`
31 - An item in input file list: `%f`
32 - Source code charset: `--from-code=%c`
33
34<img src="http://i.imgur.com/f9px2.png" />
35
36Now you can update your catalog and Poedit will synchronize it with your twig
37templates.
38
39## Tests
40
41To run the test suite, you need [composer](http://getcomposer.org) and
42[PHPUnit](https://github.com/sebastianbergmann/phpunit).
43
44 $ composer install --dev
45 $ phpunit
46
47## License
48
49Twig 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 @@
1<?php
2
3/**
4 * This file is part of the Twig Gettext utility.
5 *
6 * (c) Саша Стаменковић <umpirsky@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Gettext;
13
14use Symfony\Component\Filesystem\Filesystem;
15
16/**
17 * Extracts translations from twig templates.
18 *
19 * @author Саша Стаменковић <umpirsky@gmail.com>
20 */
21class Extractor
22{
23 /**
24 * @var \Twig_Environment
25 */
26 protected $environment;
27
28 /**
29 * Template cached file names.
30 *
31 * @var string[]
32 */
33 protected $templates;
34
35 /**
36 * Gettext parameters.
37 *
38 * @var string[]
39 */
40 protected $parameters;
41
42 public function __construct(\Twig_Environment $environment)
43 {
44 $this->environment = $environment;
45 $this->reset();
46 }
47
48 protected function reset()
49 {
50 $this->templates = array();
51 $this->parameters = array();
52 }
53
54 public function addTemplate($path)
55 {
56 $this->environment->loadTemplate($path);
57 $this->templates[] = $this->environment->getCacheFilename($path);
58 }
59
60 public function addGettextParameter($parameter)
61 {
62 $this->parameters[] = $parameter;
63 }
64
65 public function setGettextParameters(array $parameters)
66 {
67 $this->parameters = $parameters;
68 }
69
70 public function extract()
71 {
72 $command = 'xgettext';
73 $command .= ' '.join(' ', $this->parameters);
74 $command .= ' '.join(' ', $this->templates);
75
76 $error = 0;
77 $output = system($command, $error);
78 if (0 !== $error) {
79 throw new \RuntimeException(sprintf(
80 'Gettext command "%s" failed with error code %s and output: %s',
81 $command,
82 $error,
83 $output
84 ));
85 }
86
87 $this->reset();
88 }
89
90 public function __destruct()
91 {
92 $filesystem = new Filesystem();
93 $filesystem->remove($this->environment->getCache());
94 }
95}
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 @@
1<?php
2
3/**
4 * This file is part of the Twig Gettext utility.
5 *
6 * (c) Саша Стаменковић <umpirsky@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Gettext\Loader;
13
14/**
15 * Loads template from the filesystem.
16 *
17 * @author Саша Стаменковић <umpirsky@gmail.com>
18 */
19class Filesystem extends \Twig_Loader_Filesystem
20{
21 /**
22 * Hacked find template to allow loading templates by absolute path.
23 *
24 * @param string $name template name or absolute path
25 */
26 protected function findTemplate($name)
27 {
28 // normalize name
29 $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
30
31 if (isset($this->cache[$name])) {
32 return $this->cache[$name];
33 }
34
35 $this->validateName($name);
36
37 $namespace = '__main__';
38 if (isset($name[0]) && '@' == $name[0]) {
39 if (false === $pos = strpos($name, '/')) {
40 throw new \InvalidArgumentException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
41 }
42
43 $namespace = substr($name, 1, $pos - 1);
44
45 $name = substr($name, $pos + 1);
46 }
47
48 if (!isset($this->paths[$namespace])) {
49 throw new \Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
50 }
51
52 if (is_file($name)) {
53 return $this->cache[$name] = $name;
54 }
55
56 return __DIR__.'/../Test/Fixtures/twig/empty.twig';
57 }
58}
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 @@
1<?php
2
3/**
4 * This file is part of the Twig Gettext utility.
5 *
6 * (c) Саша Стаменковић <umpirsky@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Gettext\Routing\Generator;
13
14use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15use Symfony\Component\Routing\RequestContext;
16
17/**
18 * Dummy url generator.
19 *
20 * @author Саша Стаменковић <umpirsky@gmail.com>
21 */
22class UrlGenerator implements UrlGeneratorInterface
23{
24 protected $context;
25
26 public function generate($name, $parameters = array(), $absolute = false)
27 {
28 }
29
30 public function getContext()
31 {
32 return $this->context;
33 }
34
35 public function setContext(RequestContext $context)
36 {
37 $this->context = $context;
38 }
39}
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 @@
1<?php
2
3/**
4 * This file is part of the Twig Gettext utility.
5 *
6 * (c) Саша Стаменковић <umpirsky@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Gettext\Test;
13
14use Twig\Gettext\Extractor;
15use Twig\Gettext\Loader\Filesystem;
16use Symfony\Component\Translation\Loader\PoFileLoader;
17
18/**
19 * @author Саша Стаменковић <umpirsky@gmail.com>
20 */
21class ExtractorTest extends \PHPUnit_Framework_TestCase
22{
23 /**
24 * @var \Twig_Environment
25 */
26 protected $twig;
27
28 /**
29 * @var PoFileLoader
30 */
31 protected $loader;
32
33 protected function setUp()
34 {
35 $this->twig = new \Twig_Environment(new Filesystem('/'), array(
36 'cache' => '/tmp/cache/'.uniqid(),
37 'auto_reload' => true
38 ));
39 $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
40
41 $this->loader = new PoFileLoader();
42 }
43
44 /**
45 * @dataProvider testExtractDataProvider
46 */
47 public function testExtract(array $templates, array $parameters, array $messages)
48 {
49 $extractor = new Extractor($this->twig);
50
51 foreach ($templates as $template) {
52 $extractor->addTemplate($template);
53 }
54 foreach ($parameters as $parameter) {
55 $extractor->addGettextParameter($parameter);
56 }
57
58 $extractor->extract();
59
60 $catalog = $this->loader->load($this->getPotFile(), null);
61
62 foreach ($messages as $message) {
63 $this->assertTrue(
64 $catalog->has($message),
65 sprintf('Message "%s" not found in catalog.', $message)
66 );
67 }
68 }
69
70 public function testExtractDataProvider()
71 {
72 return array(
73 array(
74 array(
75 __DIR__.'/Fixtures/twig/singular.twig',
76 __DIR__.'/Fixtures/twig/plural.twig',
77 ),
78 $this->getGettextParameters(),
79 array(
80 'Hello %name%!',
81 'Hello World!',
82 'Hey %name%, I have one apple.',
83 'Hey %name%, I have %count% apples.',
84 ),
85 ),
86 );
87 }
88
89 public function testExtractNoTranslations()
90 {
91 $extractor = new Extractor($this->twig);
92
93 $extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig');
94 $extractor->setGettextParameters($this->getGettextParameters());
95
96 $extractor->extract();
97
98 $catalog = $this->loader->load($this->getPotFile(), null);
99
100 $this->assertEmpty($catalog->all('messages'));
101 }
102
103 private function getPotFile()
104 {
105 return __DIR__.'/Fixtures/messages.pot';
106 }
107
108 private function getGettextParameters()
109 {
110 return array(
111 '--force-po',
112 '-o',
113 $this->getPotFile(),
114 );
115 }
116
117 protected function tearDown()
118 {
119 if (file_exists($this->getPotFile())) {
120 unlink($this->getPotFile());
121 }
122 }
123}
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 @@
1{% trans %}
2 Hey {{ name }}, I have one apple.
3{% plural apple_count %}
4 Hey {{ name }}, I have {{ count }} apples.
5{% 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 @@
1{% trans "Hello World!" %}
2
3{% trans %}
4 Hello World!
5{% endtrans %}
6
7{% trans %}
8 Hello {{ name }}!
9{% 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 @@
1{
2 "name": "umpirsky/twig-gettext-extractor",
3 "type": "application",
4 "description": "The Twig Gettext Extractor is Poedit friendly tool which extracts translations from twig templates.",
5 "license": "MIT",
6 "authors": [
7 {
8 "name": "Саша Стаменковић",
9 "email": "umpirsky@gmail.com"
10 }
11 ],
12 "require": {
13 "php": ">=5.3.3",
14 "twig/twig": ">=1.2.0,<2.0-dev",
15 "twig/extensions": "1.0.*",
16 "symfony/twig-bridge": ">=2.0,<3.0",
17 "symfony/routing": ">=2.0,<3.0",
18 "symfony/filesystem": ">=2.0,<3.0",
19 "symfony/translation": ">=2.0,<3.0",
20 "symfony/form": ">=2.0,<3.0"
21 },
22 "require-dev": {
23 "symfony/config": "2.1.*"
24 },
25 "minimum-stability": "dev",
26 "autoload": {
27 "psr-0": { "Twig\\Gettext": "." }
28 },
29 "bin": ["twig-gettext-extractor"]
30} \ 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 @@
1<?xml version="1.0" encoding="UTF-8"?>
2
3<phpunit colors="true"
4 convertErrorsToExceptions="true"
5 convertNoticesToExceptions="true"
6 convertWarningsToExceptions="true"
7 bootstrap="./vendor/autoload.php"
8>
9 <testsuites>
10 <testsuite name="Twig Gettext Extractor Test Suite">
11 <directory>./Twig/Gettext/Test/</directory>
12 </testsuite>
13 </testsuites>
14</phpunit>
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 @@
1#!/usr/bin/env php
2<?php
3
4/**
5 * This file is part of the Twig Gettext utility.
6 *
7 * (c) Саша Стаменковић <umpirsky@gmail.com>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13/**
14 * Extracts translations from twig templates.
15 *
16 * @author Саша Стаменковић <umpirsky@gmail.com>
17 */
18
19if (file_exists($a = __DIR__.'/../../autoload.php')) {
20 require_once $a;
21} else {
22 require_once __DIR__.'/vendor/autoload.php';
23}
24
25$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem('/'), array(
26 'cache' => '/tmp/cache/'.uniqid(),
27 'auto_reload' => true
28));
29$twig->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension(
30 new Symfony\Component\Translation\Translator(null)
31));
32$twig->addExtension(new Twig_Extensions_Extension_I18n());
33$twig->addExtension(new Symfony\Bridge\Twig\Extension\RoutingExtension(
34 new Twig\Gettext\Routing\Generator\UrlGenerator()
35));
36$twig->addExtension(new Symfony\Bridge\Twig\Extension\FormExtension(
37 new Symfony\Bridge\Twig\Form\TwigRenderer(
38 new Symfony\Bridge\Twig\Form\TwigRendererEngine()
39 )
40));
41// You can add more extensions here.
42
43array_shift($_SERVER['argv']);
44$addTemplate = false;
45
46$extractor = new Twig\Gettext\Extractor($twig);
47
48foreach ($_SERVER['argv'] as $arg) {
49 if ('--files' == $arg) {
50 $addTemplate = true;
51 } else if ($addTemplate) {
52 $extractor->addTemplate(getcwd().DIRECTORY_SEPARATOR.$arg);
53 } else {
54 $extractor->addGettextParameter($arg);
55 }
56}
57
58$extractor->extract();