Skip to content

Commit

Permalink
using templating to wrap trans texts; see #7
Browse files Browse the repository at this point in the history
  • Loading branch information
docteurklein committed Jul 25, 2011
1 parent 83d7c69 commit 372e2c8
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 35 deletions.
8 changes: 6 additions & 2 deletions Controller/EditionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;

class EditionController
{
private $translator;
private $translatorHelper;
private $request;
private $templating;

public function __construct(Request $request, Translator $translator, EngineInterface $templating)
public function __construct(Request $request, Translator $translator, TranslatorHelper $translatorHelper, EngineInterface $templating)
{
$this->request = $request;
$this->translator = $translator;
$this->translatorHelper = $translatorHelper;
$this->templating = $templating;
}

Expand All @@ -40,7 +43,8 @@ public function listAction()

return $this->templating->renderResponse('KnpTranslatorBundle:Edition:list.html.twig', array(
'translations' => $translations,
'translator' => $this->translator
'translatorHelper' => $this->translatorHelper,
'translator' => $this->translator,
));
}
}
4 changes: 2 additions & 2 deletions Controller/TranslatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Request $request, Translator $translator, $logger)

public function getAction($id, $domain, $locale)
{
$trans = $this->translator->getTranslatedValue($id, array(), $domain, $locale);
$trans = $this->translator->trans($id, array(), $domain, $locale);

return new Response($trans);
}
Expand All @@ -50,7 +50,7 @@ public function putAction()
}
catch (InvalidTranslationKeyException $e) {
$success = false;
$trans = $this->translator->getTranslatedValue($id, array(), $domain, $locale);
$trans = $this->translator->trans($id, array(), $domain, $locale);
$error = $e->getMessage();
}

Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/Compiler/TranslatorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function process(ContainerBuilder $container)

foreach($translatorRealDefinition->getMethodCalls() as $methodCall) {
$translatorDefinition->addMethodCall($methodCall[0], $methodCall[1]);
// use resources from translator.real to add available locales
if ('addResource' === $methodCall[0]) {
// $methodCall[1][2] is the locale
// @see FrameworkBundle\DependencyInjection\FrameworkExtension::registerTranslatorConfiguration
$translatorDefinition->addMethodCall('addLocale', array($methodCall[1][2]));
}
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/KnpTranslatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class KnpTranslatorExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{

$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
Expand All @@ -31,5 +30,6 @@ public function load(array $configs, ContainerBuilder $container)

// Use the "writer" translator instead of the default one
$container->setAlias('translator', 'translator.writer');
$container->setAlias('templating.helper.translator', 'templating.helper.translator.writer');
}
}
1 change: 1 addition & 0 deletions Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<service id="knplabs_translator.controller.edition" class="Knp\Bundle\TranslatorBundle\Controller\EditionController" scope="request">
<argument type="service" id="request" />
<argument type="service" id="translator.writer" />
<argument type="service" id="templating.helper.translator.writer" />
<argument type="service" id="templating" />
</service>
</services>
Expand Down
10 changes: 10 additions & 0 deletions Resources/config/translation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@
<argument>%knplabs.translator.include_vendor_assets%</argument>
<tag name="kernel.event_listener" event="kernel.response" />
</service>

<service id="templating.helper.translator.writer" class="Knp\Bundle\TranslatorBundle\Templating\Helper\TranslatorHelper">
<tag name="templating.helper" alias="translator" />
<argument type="service" id="translator.writer" />
</service>

<service id="twig.extension.trans" class="Knp\Bundle\TranslatorBundle\Templating\Twig\TranslationExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="templating.helper.translator.writer" />
</service>
</services>
</container>
3 changes: 2 additions & 1 deletion Resources/views/Edition/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
{% for source,target in translations[locale][domain] %}
<tr>
<td class="trans-source">{{ source }}</td>
<td class="trans-target">{{ translator.wrap(source, domain, locale) }}</td>
{% set trans = translator.trans(source, {}, domain, locale) %}
<td class="trans-target">{{ translatorHelper.wrap(source, trans, domain, locale) }}</td>

This comment has been minimized.

Copy link
@stof

stof Jul 25, 2011

Why not using a Twig filter for this ? it would be cleaner

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

i agree! but i don't want to create a twig filter only for this call.

</tr>

{% endfor %}
Expand Down
56 changes: 56 additions & 0 deletions Templating/Helper/TranslatorHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Knp\Bundle\TranslatorBundle\Templating\Helper;

use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper as BaseTranslatorHelper;

/**
* TranslatorHelper.
*
* @author Florian Klein <florian.klein@free.fr>
*/
class TranslatorHelper extends BaseTranslatorHelper
{
/**
* @see TranslatorInterface::trans()
*/
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (!isset($locale)) {
$locale = $this->translator->getLocale();
}

$trans = parent::trans($id, $parameters, $domain, $locale);

return $this->wrap($id, $trans, $domain, $locale);

This comment has been minimized.

Copy link
@stof

stof Jul 25, 2011

do you really want to replace the core templating helper by an helper wrapping the value in all cases ? I think it makes this bundle very intrusive.

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

Yeah I know it's too much intrusive, but it's the only way I found if I want to translate texts without modifying the templates that display them.

The filter solution would force users to pipe every trans call in another extra trans function, which is not wanted.
I want people to trans their texts without modifying any template.

This comment has been minimized.

Copy link
@stof

stof Jul 25, 2011

but not modifying the templates that display them means that it impacts all places where the translation is used, without a way to get rid of it.

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

The wanted usage it to activate this bundle only in dev environment (or other), so that it overrides core services only during translation process.

It's not done yet, but i want to add a switcher in DI extension config, like knp_translator.enabled: true|false

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

@stof, do you think it makes sense?

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

but not modifying the templates that display them means that it impacts all places where the translation is used, without a way to get rid of it.

unfortunately, yes :-/

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 25, 2011

Author Owner

At least moving the wraping stuff in a templating helper made things less intrusive already. see #7

This comment has been minimized.

Copy link
@stof

stof Jul 25, 2011

@docteurklein yeah, I think the configuration to enable it is a good idea to choose if we want it or no

}

/**
* @see TranslatorInterface::transChoice()
*/
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (!isset($locale)) {
$locale = $this->translator->getLocale();
}

$trans = parent::transChoice($id, $number, $parameters, $domain, $locale);

return $this->wrap($id, $trans, $domain, $locale);
}

/**
* Wraps a translated value with [T id="%s" domain="%s" locale="%s"]%s[/T]
* Used to detect in-line edition of translations
*
* @return string
*/
public function wrap($id, $trans, $domain = 'messages', $locale = null)
{
$startTag = sprintf('[T id="%s" domain="%s" locale="%s"]', $id, $domain, $locale);

return sprintf('%s%s%s', $startTag, $trans, '[/T]');
}
}
31 changes: 31 additions & 0 deletions Templating/Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Knp\Bundle\TranslatorBundle\Templating\Twig;

use Symfony\Bridge\Twig\Extension\TranslationExtension as BaseTranslationExtension;
use Knp\Bundle\TranslatorBundle\Templating\Helper\TranslatorHelper;

/**
* Provides integration of the Translation component with Twig.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslationExtension extends BaseTranslationExtension
{
private $translatorHelper;

public function __construct(TranslatorHelper $translatorHelper)
{
$this->translatorHelper = $translatorHelper;
}

public function trans($message, array $arguments = array(), $domain = "messages")
{
return $this->translatorHelper->trans($message, $arguments, $domain);
}

public function transchoice($message, $count, array $arguments = array(), $domain = "messages")
{
return $this->translatorHelper->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain);
}
}
2 changes: 1 addition & 1 deletion Tests/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testYamlUpdate()

$translator->addDumper(new YamlDumper());

$this->assertEquals('foobarbaz', $translator->getTranslatedValue('foo.bar.baz', array(), 'tests', 'en'), 'translation uses initial value');
$this->assertEquals('foobarbaz', $translator->trans('foo.bar.baz', array(), 'tests', 'en'), 'translation uses initial value');

$translator->update('foo.bar.baz', 'foofoofoo', 'tests', 'en');
$updatedEnContent = <<<YAML
Expand Down
28 changes: 0 additions & 28 deletions Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,6 @@ public function all()
return $translations;
}

public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (!isset($locale)) {
$locale = $this->getLocale();
}

return $this->wrap($id, $domain, $locale);
}

/**
* Wraps a translated value with [T id="%s" domain="%s" locale="%s"]%s[/T]
* Used to detect in-line edition of translations
*
* @return string
*/
public function wrap($id, $domain = 'messages', $locale = null)
{
$startTag = sprintf('[T id="%s" domain="%s" locale="%s"]', $id, $domain, $locale);
$trans = $this->getCatalog($locale)->get((string) $id, $domain);

return sprintf('%s%s%s', $startTag, $trans, '[/T]');
}

public function getTranslatedValue($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
return parent::trans($id, $parameters, $domain, $locale);
}

public function isTranslated($id, $domain, $locale)
{
return $id === $this->getCatalog($locale)->get((string) $id, $domain);
Expand Down

0 comments on commit 372e2c8

Please sign in to comment.