Skip to content

Commit

Permalink
Merge pull request #1794 from cmfcmf/multilingual-extraction
Browse files Browse the repository at this point in the history
Activate multilingual route extraction and translation
  • Loading branch information
Guite committed Jun 21, 2014
2 parents fb7ae34 + 1a6ae7a commit 6ebacde
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/app/autoload.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

Expand All @@ -14,5 +15,6 @@
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
AnnotationReader::addGlobalIgnoredName('type');

return $loader;
7 changes: 7 additions & 0 deletions src/app/config/routing_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ _profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler

# This would need the JMSDiExtraBundle
# http://jmsyst.com/bundles/JMSTranslationBundle/master/webinterface#installation
#JMSTranslationBundle_ui:
# resource: @JMSTranslationBundle/Controller/
# type: annotation
# prefix: /_trans

_main:
resource: routing.yml
3 changes: 3 additions & 0 deletions src/lib/Zikula/Bundle/CoreBundle/CoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zikula\Bundle\CoreBundle;

use Zikula\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrinePass;
use Zikula\Bundle\CoreBundle\DependencyInjection\Compiler\RegisterCoreListenersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
Expand All @@ -13,6 +14,8 @@ class CoreBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new DoctrinePass(), PassConfig::TYPE_OPTIMIZE);

$container->addCompilerPass(new RegisterCoreListenersPass(), PassConfig::TYPE_AFTER_REMOVING);

// todo - see if we can do this only on module install/upgrade - drak
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Zikula\Bundle\CoreBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class DoctrinePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->setAlias('doctrine.entitymanager', 'doctrine.orm.default_entity_manager');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(ContainerBuilder $container)

public function initDoctrine(GenericEvent $event)
{
if ($this->container->has('doctrine.entitymanager')) {
if ($this->container->has('doctrine.event_manager')) {
return;
}

Expand Down Expand Up @@ -74,7 +74,6 @@ public function initDoctrine(GenericEvent $event)
$mysqlStorageEvent = new MySqlGenerateSchemaListener($eventManager);
}

$this->container->setAlias('doctrine.entitymanager', 'doctrine.orm.default_entity_manager');
$this->container->setAlias('doctrine.eventmanager', 'doctrine.event_manager');
}

Expand Down
13 changes: 12 additions & 1 deletion src/lib/util/ServiceUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* ServiceUtil
Expand Down Expand Up @@ -57,6 +58,16 @@ public static function getManager(Zikula_Core $core = null)
return self::$serviceManager;
}

public static function hasContainer()
{
return self::$serviceManager !== null;
}

public static function setContainer(ContainerInterface $container)
{
self::$serviceManager = $container;
}

/**
* Convenience shortcut to get a service.
*
Expand Down Expand Up @@ -138,7 +149,7 @@ public static function loadPersistentServices()
}

foreach ($handlers as $id => $handler) {
self::$serviceManager->registerService(new Zikula_ServiceManager_Service($id, $handler['definition'], $handler['shared']));
self::$serviceManager->registerService($id, $handler['definition'], $handler['shared']);
}
}
}
14 changes: 13 additions & 1 deletion src/system/ZikulaRoutesModule/Entity/RouteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ public function prepareItemActions()
}
}

public function getPathWithBundlePrefix()
/**
* Returns the route's path prepended with the bundle prefix.
*
* @param null $container Can be used to set the container for \ServiceUtil in case it is not already set.
*
* @return string
*/
public function getPathWithBundlePrefix($container = null)
{
if (!isset($this->options['zkNoBundlePrefix']) || !$this->options['zkNoBundlePrefix']) {
$bundle = $this->getBundle();

if (!\ServiceUtil::hasContainer()) {
\ServiceUtil::setContainer($container);
}

$modinfo = \ModUtil::getInfoFromName($bundle);

return "/" . $modinfo["url"] . $this->path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="zikularoutesmodule.routing_installer_listener.class">Zikula\RoutesModule\Routing\InstallerListener</parameter>
<parameter key="zikularoutesmodule.console_command_listener.class">Zikula\RoutesModule\Translation\ConsoleCommandListener</parameter>
<parameter key="jms_i18n_routing.route_exclusion_strategy.class">Zikula\RoutesModule\Translation\DefaultRouteExclusionStrategy</parameter>
</parameters>
<services>
<service id="zikularoutesmodule.routing_installer_listener" class="%zikularoutesmodule.routing_installer_listener.class%">
Expand All @@ -19,9 +21,14 @@
<argument type="service" id="logger" />
</service>

<service id="zikularoutesmodule.console_command_listener" class="%zikularoutesmodule.console_command_listener.class%">
<tag name="kernel.event_subscriber" />
</service>

<service id="zikularoutesmodule.routing_loader" class="Zikula\RoutesModule\Routing\RouteLoader">
<tag name="routing.loader" />
<argument type="service" id="doctrine.orm.entity_manager">em</argument>
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="service_container" />
</service>
</services>
</container>
9 changes: 7 additions & 2 deletions src/system/ZikulaRoutesModule/Routing/RouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Zikula\RoutesModule\Routing\Util as RoutingUtil;
Expand All @@ -21,9 +22,12 @@ class RouteLoader implements LoaderInterface

private $em;

public function __construct(EntityManagerInterface $em)
private $container;

public function __construct(EntityManagerInterface $em, ContainerInterface $container)
{
$this->em = $em;
$this->container = $container;
}

public function load($resource, $type = null)
Expand All @@ -34,6 +38,7 @@ public function load($resource, $type = null)
unset($type);

$routeCollection = new RouteCollection();

try {
$routes = $this->em->getRepository('ZikulaRoutesModule:RouteEntity')->findBy(array('workflowState' => 'approved'), array('group' => 'ASC', 'sort' => 'ASC'));
} catch (DBALException $e) {
Expand All @@ -56,7 +61,7 @@ public function load($resource, $type = null)
$defaults['_zkFunc'] = $func;

$route = new Route(
$dbRoute->getPathWithBundlePrefix(),
!isset($GLOBALS['translation_extract_routes']) ? $dbRoute->getPathWithBundlePrefix($this->container) : $dbRoute->getPath(),
$defaults,
$dbRoute->getRequirements(),
$dbRoute->getOptions(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright Zikula Foundation 2014 - Zikula Application Framework
*
* This work is contributed to the Zikula Foundation under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license GNU/LGPv3 (or at your option any later version).
* @package Zikula
*
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/

namespace Zikula\RoutesModule\Translation;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
* Class ConsoleCommandListener.
*/
class ConsoleCommandListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array (
ConsoleEvents::COMMAND => array('checkBundleForTranslatingRoutes')
);
}

/**
* This function saves the bundle whose routes shall be translated in a global variable to be used in
* Zikula\RoutesModule\Translation\DefaultRouteExclusionStrategy later on.
*
* @param ConsoleCommandEvent $event
*/
public function checkBundleForTranslatingRoutes(ConsoleCommandEvent $event)
{
if ($event->getCommand()->getName() !== 'translation:extract') {
return;
}

$GLOBALS['translation_extract_routes'] = true;

if ($event->getInput()->hasParameterOption('--bundle')) {
$bundle = $event->getInput()->getParameterOption('--bundle');
if ('@' === $bundle[0]) {
$bundle = substr($bundle, 1);
}

$GLOBALS['translation_extract_routes_bundle'] = $bundle;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright Zikula Foundation 2014 - Zikula Application Framework
*
* This work is contributed to the Zikula Foundation under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license GNU/LGPv3 (or at your option any later version).
* @package Zikula
*
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/

namespace Zikula\RoutesModule\Translation;

use JMS\I18nRoutingBundle\Router\DefaultRouteExclusionStrategy as BaseDefaultRouteExclusionStrategy;
use Symfony\Component\Routing\Route;


/**
* Class DefaultRouteExclusionStrategy.
*/
class DefaultRouteExclusionStrategy extends BaseDefaultRouteExclusionStrategy
{
public function shouldExcludeRoute($routeName, Route $route)
{
$exclude = parent::shouldExcludeRoute($routeName, $route);

if (!$exclude && isset($GLOBALS['translation_extract_routes_bundle'])) {
return $route->getDefault('_zkModule') !== $GLOBALS['translation_extract_routes_bundle'];
}

return $exclude;
}
}

0 comments on commit 6ebacde

Please sign in to comment.