Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hbraune committed Sep 23, 2016
2 parents ea6654a + a75bf4a commit ef56e38
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 20 deletions.
7 changes: 7 additions & 0 deletions BrauneDigitalTranslationBaseBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace BrauneDigital\TranslationBaseBundle;

use BrauneDigital\TranslationBaseBundle\DependencyInjection\Compiler\RouterInjectionPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class BrauneDigitalTranslationBaseBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RouterInjectionPass());
}
}
3 changes: 0 additions & 3 deletions Command/ImportLanguagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace BrauneDigital\TranslationBaseBundle\Command;

use Likez\BaseBundle\Entity\OperatorMail;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Likez\BaseBundle\Entity\Mail;

class ImportLanguagesCommand extends ContainerAwareCommand
{
protected function configure()
Expand Down
18 changes: 15 additions & 3 deletions DependencyInjection/BrauneDigitalTranslationBaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@ class BrauneDigitalTranslationBaseExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$config = $this->processConfiguration($configuration, $configs);

$bundles = $container->getParameter('kernel.bundles');


if($config['routing']) {
$loader->load('routing_services.yml');
$container->setParameter('braune_digital.translation_base.use_routing', true);
}

if($config['admin'] && isset($bundles['SonataAdminBundle'])) {
$loader->load('admin.yml');
}
}
}
}
24 changes: 24 additions & 0 deletions DependencyInjection/Compiler/RouterInjectionPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace BrauneDigital\TranslationBaseBundle\DependencyInjection\Compiler;

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

/**
* Class RouterInjectionPass
*
* @package BrauneDigital\TranslationBaseBundle\DependencyInjection\Compiler
*/
class RouterInjectionPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
* Inject the service router as the normal router
*/
public function process(ContainerBuilder $container) {
if($container->hasParameter('braune_digital.translation_base.use_routing') && $container->getParameter('braune_digital.translation_base.use_routing')) {
$container->setAlias('router', 'braune_digital.translation_base.routing.service_router');
}
}
}
19 changes: 18 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BrauneDigital\TranslationBaseBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -18,9 +19,25 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('braunedigital_mail');
$rootNode = $treeBuilder->root('braune_digital_translation_base');

$this->addAdminSection($rootNode);
$this->addRoutingSection($rootNode);

return $treeBuilder;
}

/**
* @param $rootNode
*/
protected function addAdminSection(ArrayNodeDefinition $rootNode) {
$rootNode->children()->booleanNode('admin')->defaultTrue();
}

/**
* @param $rootNode
*/
protected function addRoutingSection(ArrayNodeDefinition $rootNode) {
$rootNode->children()->booleanNode('routing')->defaultFalse();
}
}
20 changes: 9 additions & 11 deletions Form/TranslationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace BrauneDigital\TranslationBaseBundle\Form;

use Application\AppBundle\Services\LocaleOptions;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -44,11 +43,7 @@ public function __construct(RequestStack $requestStack, Container $container) {
$this->container = $container;

// Get current locale
if ($this->request->get('object_locale')) {
$this->currentLocale = array($this->request->get('object_locale'));
} else {
$this->currentLocale = array('en');
}
$this->currentLocale = $this->request->get('object_locale', $this->container->getParameter('locale'));
}

/**
Expand All @@ -72,10 +67,15 @@ public function buildTranslations(FormBuilderInterface $builder) {
}

public function getDisabled(FormBuilderInterface $builder) {
if (!$builder->getData()->getDefaultLanguage()) {
return false;
if(method_exists($builder->getData(), 'getDefaultLanguage')) {

if (!$builder->getData()->getDefaultLanguage()) {
return false;
}
return ($this->currentLocale == $builder->getData()->getDefaultLanguage()->getCode()) ? false : true;
} else {
return $this->currentLocale != $this->container->getParameter('locale');
}
return ($this->currentLocale[0] == $builder->getData()->getDefaultLanguage()->getCode()) ? false : true;
}

/**
Expand All @@ -90,6 +90,4 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['currentTranslation'] = $this->container->get('doctrine')->getRepository('BrauneDigitalTranslationBaseBundle:Language')->findOneBy(array('code' => $this->request->get('object_locale')));

}


}
35 changes: 35 additions & 0 deletions Model/Translatable/TranslatableMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,39 @@ public function setNewTranslations($newTranslations)
{
$this->newTranslations = $newTranslations;
}

/**
* @param $name
* generic translation
* @return mixed
*/
public function translateProperty($name) {
$functionName = 'get' . ucfirst($name);
$value = $this->translate()->$functionName();

if ($value) {
return $value;
} else {
return $this->translate($this->getDefaultLocale())->$functionName();
}
}

/**
* @param $name
* proxy getter for translation values
* @return mixed
*/
public function __get($name) {
return $this->translateProperty($name);
}

/**
* @param $name
* proxy setter for translation values
* @return mixed
*/
public function __set($name, $value) {
$functionName = 'set' . ucfirst($name);
$value = $this->translate()->$functionName($value);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ class EntityAdmin extends TranslationAdmin
This Bundle also provides a basic Language Entity, which can be managed through SonataAdmin as well.
## Todo
Add requirements with versions in `composer.json`.
A caching mechanism is needed for the service Router. The Symfony2 router creates own instances and caches them. The service Router however is not able to do that, but maybe a RouteCollection cache would speed things up ;)
File renamed without changes.
34 changes: 34 additions & 0 deletions Resources/config/routing_services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
parameters:
braune_digital.translation_base.routing.locales: "%locales%"
braune_digital.translation_base.routing.default_locale: "%locale%"

services:
braune_digital.translation_base.routing.generator:
class: "BrauneDigital\\TranslationBaseBundle\\Routing\\LocalizedUrlGenerator"
arguments:
- "@logger"
- "%braune_digital.translation_base.routing.locales%"
- "%braune_digital.translation_base.routing.default_locale%"

braune_digital.translation_base.routing.matcher:
class: "BrauneDigital\\TranslationBaseBundle\\Routing\\LocalizedUrlMatcher"
arguments:
- "%braune_digital.translation_base.routing.locales%"
- "%braune_digital.translation_base.routing.default_locale%"

braune_digital.translation_base.routing.service_router:
class: "BrauneDigital\\TranslationBaseBundle\\Routing\\ServiceRouter"
tags:
- {name: "monolog.logger", channel: "router"}
arguments:
- "@service_container"
- "%router.resource%"
- "@router.request_context"
- "braune_digital.translation_base.routing.generator"
- "braune_digital.translation_base.routing.matcher"

braune_digital.translation_base.routing.loader.yaml:
class: "BrauneDigital\\TranslationBaseBundle\\Routing\\YamlLocalizedLoader"
arguments: ["@file_locator", "%braune_digital.translation_base.routing.locales%"]
tags:
- { name: routing.loader }
2 changes: 1 addition & 1 deletion Resources/views/admin/translation_layout.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "SonataAdminBundle:standard_layout.html.twig" %}
{% extends "SonataAdminBundle::standard_layout.html.twig" %}

{% block sonata_page_content_nav %}
{% if _tab_menu is not empty or _actions is not empty %}
Expand Down
Loading

0 comments on commit ef56e38

Please sign in to comment.