-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding Desc filter * Make sue we save Desc when importing * Make the class final * Adding visitors to twig filter * Added tests * cs * Addd author docs
- Loading branch information
Showing
22 changed files
with
639 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%") }} | ||
|
4 changes: 4 additions & 0 deletions
4
Tests/Unit/Twig/Fixture/apply_default_value_compiled.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{ "foo" | ||
~ "bar" | ||
~ "baz" }} |
1 change: 1 addition & 0 deletions
1
Tests/Unit/Twig/Fixture/binary_concat_of_constants_compiled.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ "foobarbaz" }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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(')'); | ||
} | ||
} |
Oops, something went wrong.