Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee

### Added

- Support for `desc` filter in Twig.

### Changed

- Twig extension `TranslationExtension` was renamed to `EditInPlaceExtension`

## 0.5.0

### Added

- Symfony 4 support
- New `--cache` option on the `translation:download` allowing to clear the cache automatically if the downloaded translations have changed.
- Support for Yandex translator
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/edit_in_place.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:

php_translation.edit_in_place.extension.trans:
public: false
class: Translation\Bundle\Twig\TranslationExtension
class: Translation\Bundle\Twig\EditInPlaceExtension
arguments:
- '@php_translator.edit_in_place.xtrans_html_translator'
calls:
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/extractors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ services:
public: false

php_translation.extractor.twig.visitor.twig:
class: Translation\Bundle\Twig\DummyTwigVisitor
class: Translation\Bundle\Twig\Visitor\DummyTwigVisitor
factory: ["@php_translation.extractor.twig.factory", create]
tags:
- { name: 'php_translation.visitor', type: 'twig' }
5 changes: 3 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ services:
arguments: []

php_translation.twig_extension:
class: Translation\Extractor\Twig\TranslationExtension
class: Translation\Bundle\Twig\TranslationExtension
arguments: ['@translator', "%kernel.debug%"]
tags:
- { name: twig.extension }
- { name: twig.extension }
3 changes: 3 additions & 0 deletions Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, S

$meta = $this->getMetadata($catalogue, $key, $domain);
$meta->addCategory('file-source', sprintf('%s:%s', substr($sourceLocation->getPath(), $trimLength), $sourceLocation->getLine()));
if (isset($sourceLocation->getContext()['desc'])) {
$meta->addCategory('desc', $sourceLocation->getContext()['desc']);
}
$this->setMetadata($catalogue, $key, $domain, $meta);
}
}
Expand Down
35 changes: 35 additions & 0 deletions Tests/Unit/Twig/BaseTwigTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Unit\Twig;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
use Translation\Bundle\Twig\TranslationExtension;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class BaseTwigTestCase extends TestCase
{
final protected function parse($file, $debug = false)
{
$content = file_get_contents(__DIR__.'/Fixture/'.$file);

$env = new \Twig_Environment(new \Twig_Loader_Array([]));
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$env->addExtension(new TranslationExtension($translator, $debug));

return $env->parse($env->tokenize(new \Twig_Source($content, null)))->getNode('body');
}
}
26 changes: 26 additions & 0 deletions Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Unit\Twig;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DefaultApplyingNodeVisitorTest extends BaseTwigTestCase
{
public function testApply()
{
$this->assertEquals(
$this->parse('apply_default_value_compiled.html.twig', true),
$this->parse('apply_default_value.html.twig', true)
);
}
}
4 changes: 4 additions & 0 deletions Tests/Unit/Twig/Fixture/apply_default_value.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ "form.label.firstname"|trans|desc("Firstname") }}

{{ "foo.%bar%"|trans({"%bar%": "baz"})|desc("Foo %bar%") }}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ "form.label.firstname"|trans == "form.label.firstname" ? "Firstname" : "form.label.firstname"|trans }}

{{ "foo.%bar%"|trans({}) == "foo.%bar%" ? "Foo %bar%"|replace({"%bar%": "baz"}) : "foo.%bar%"|trans({"%bar%": "baz"}) }}

3 changes: 3 additions & 0 deletions Tests/Unit/Twig/Fixture/binary_concat_of_constants.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ "foo"
~ "bar"
~ "baz" }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ "foobarbaz" }}
21 changes: 21 additions & 0 deletions Tests/Unit/Twig/Fixture/simple_template.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ "text.foo"|trans|desc("Foo Bar")|meaning("Some Meaning")}}

{{ "text.bar"|trans|desc("Foo") }}

{{ "text.baz"|trans|meaning("Bar") }}

{{ "text.foo_bar"|trans({}, "foo") }}

{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %}

{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %}

{{ "foo.bar" | trans }}

{{ "foo.bar2" | transchoice(5) }}

{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }}

{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}

{% trans %}text.default_domain{% endtrans %}
21 changes: 21 additions & 0 deletions Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ "text.foo"|trans }}

{{ "text.bar"|trans }}

{{ "text.baz"|trans }}

{{ "text.foo_bar"|trans({}, "foo") }}

{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %}

{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %}

{{ "foo.bar" | trans }}

{{ "foo.bar2" | transchoice(5) }}

{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }}

{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}

{% trans %}text.default_domain{% endtrans %}
26 changes: 26 additions & 0 deletions Tests/Unit/Twig/NormalizingNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Unit\Twig;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class NormalizingNodeVisitorTest extends BaseTwigTestCase
{
public function testBinaryConcatOfConstants()
{
$this->assertEquals(
$this->parse('binary_concat_of_constants_compiled.html.twig'),
$this->parse('binary_concat_of_constants.html.twig')
);
}
}
26 changes: 26 additions & 0 deletions Tests/Unit/Twig/RemovingNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Unit\Twig;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class RemovingNodeVisitorTest extends BaseTwigTestCase
{
public function testRemovalWithSimpleTemplate()
{
$expected = $this->parse('simple_template_compiled.html.twig');
$actual = $this->parse('simple_template.html.twig');

$this->assertEquals($expected, $actual);
}
}
79 changes: 79 additions & 0 deletions Twig/EditInPlaceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Twig;

use Symfony\Component\HttpFoundation\RequestStack;
use Translation\Bundle\EditInPlace\ActivatorInterface;

/**
* Override the `trans` functions `is_safe` option to allow HTML output from the
* translator. This extension is used by for the EditInPlace feature.
*
* @author Damien Alexandre <dalexandre@jolicode.com>
*/
final class EditInPlaceExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension
{
/**
* @var ActivatorInterface
*/
private $activator;

/**
* @var RequestStack
*/
private $requestStack;

/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
];
}

/**
* Escape output if the EditInPlace is disabled.
*
* @return array
*/
public function isSafe($node)
{
return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : [];
}

/**
* @param ActivatorInterface $activator
*/
public function setActivator(ActivatorInterface $activator)
{
$this->activator = $activator;
}

/**
* @param RequestStack $requestStack
*/
public function setRequestStack(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return self::class;
}
}
43 changes: 43 additions & 0 deletions Twig/Node/Transchoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Twig\Node;

class Transchoice extends \Twig_Node_Expression
{
public function __construct(\Twig_Node_Expression_Array $arguments, $lineno)
{
parent::__construct(['arguments' => $arguments], [], $lineno);
}

public function compile(\Twig_Compiler $compiler)
{
$compiler->raw(
sprintf(
'$this->env->getExtension(\'%s\')->%s(',
'Translation\Bundle\Twig\TranslationExtension',
'transchoiceWithDefault'
)
);

$first = true;
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
if (!$first) {
$compiler->raw(', ');
}
$first = false;

$compiler->subcompile($pair['value']);
}

$compiler->raw(')');
}
}
Loading