Skip to content

Commit

Permalink
Merge pull request #2 from lenybernard/fix/VIC-699-I18N-domain-strategy
Browse files Browse the repository at this point in the history
Fix/vic 699 i18 n domain strategy
  • Loading branch information
Leny BERNARD committed Sep 21, 2015
2 parents d299b69 + f20ce66 commit 4c3a259
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 277 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ language: php
php:
- 5.6

hosts:
- fr.victoire.io
- en.victoire.io

before_script:
- phpenv config-add travis.php.ini
- npm install less
Expand All @@ -17,6 +21,8 @@ before_script:
- php Tests/Functionnal/bin/console --env=test doctrine:schema:create
- php Tests/Functionnal/bin/console --env=test victoire:generate:view
- php Tests/Functionnal/bin/console --env=test assets:install Tests/Functionnal/web
- php Tests/Functionnal/bin/console --env=test fos:js:dump --target="Tests/Functionnal/web/js/fos_js_routes_test.js"
- php Tests/Functionnal/bin/console --env=domain fos:js:dump --target="Tests/Functionnal/web/js/fos_js_routes_domain.js"
- php Tests/Functionnal/bin/console --env=test assetic:dump
- nohup php Tests/Functionnal/bin/console --env=test server:run -r vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/router_prod.php &
# Run selenium server
Expand Down
2 changes: 1 addition & 1 deletion Bundle/CoreBundle/Helper/ViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function buildViewsReferences()
/**
* @param View $view, the view to translatate
* @param $templatename the new name of the view
* @param $loopindex the current loop of iteration in recursion
* @param $locale the target locale to translate view
*
* this methods allow you to add a translation to any view
Expand Down Expand Up @@ -130,6 +129,7 @@ public function cloneView(View $view, $templateName = null)
{
$clonedView = clone $view;
$this->em->refresh($view);
$clonedView->setSlug(null);
$widgetMapClone = $clonedView->getWidgetMap(false);
$arrayMapOfWidgetMap = array();
if (null !== $templateName) {
Expand Down
2 changes: 2 additions & 0 deletions Bundle/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ services:
arguments:
- '@victoire_page.page_helper'
- '@router'
- '@victoire_i18n.locale_resolver'
- '@request_stack'

victoire_core.routing_loader:
class: Victoire\Bundle\CoreBundle\Route\RouteLoader
Expand Down
8 changes: 6 additions & 2 deletions Bundle/CoreBundle/Resources/views/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen" />
{% endstylesheets %}

{% if is_granted('ROLE_VICTOIRE') %}
{% if is_granted('ROLE_VICTOIRE') or app.request.attributes.get('_route') == 'fos_user_security_login' %}
{% stylesheets filter='less, cssrewrite' injector="victoire-edit, alertify-codrops-notification"
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen" />
Expand Down Expand Up @@ -73,7 +73,11 @@
var locale = '{{ app.request.locale }}';
var debug = {{ app.debug ? "true" : "false" }};
</script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
{% if app.environment == "test" or app.environment == "domain" %}
<script src="/js/fos_js_routes_{{ app.environment }}.js"></script>
{% else %}
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
{% endif %}

{% javascripts injector="foot, victoire-ngApp, alertify-codrops-notification" %}
<script type="text/javascript" src="{{ asset_url }}"></script>
Expand Down
48 changes: 44 additions & 4 deletions Bundle/CoreBundle/Twig/Extension/RoutingExtention.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
<?php
namespace Victoire\Bundle\CoreBundle\Twig\Extension;

use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension as BaseRoutingExtension;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Victoire\Bundle\I18nBundle\Resolver\LocaleResolver;
use Victoire\Bundle\PageBundle\Helper\PageHelper;

/**
* class RoutingExtension
*/
class RoutingExtention extends RoutingExtension
class RoutingExtention extends BaseRoutingExtension
{

private $pageHelper;
private $generator;
private $localeResolver;

public function __construct(PageHelper $pageHelper, UrlGeneratorInterface $generator)
/**
* @param PageHelper $pageHelper
* @param UrlGeneratorInterface $generator
* @param LocaleResolver $localeResolver
*/
public function __construct(PageHelper $pageHelper, UrlGeneratorInterface $generator, LocaleResolver $localeResolver, RequestStack $requestStack)
{
$this->pageHelper = $pageHelper;
$this->generator = $generator;
$this->localeResolver = $localeResolver;
$this->request = $requestStack->getCurrentRequest();
parent::__construct($generator);
}

Expand All @@ -36,6 +46,36 @@ public function getPath($name, $parameters = array(), $relative = false)
$name = 'victoire_core_page_show';
}

return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
$prefix = '';
//if locale is passed (and different) and i18n strategy is "domain"
if (!empty($parameters['_locale']) && $parameters['_locale'] != $this->request->getLocale() && $this->localeResolver->localePattern === LocaleResolver::PATTERN_DOMAIN) {
$prefix = $this->getPrefix($parameters['_locale']);
//if we set a prefix, we don't want an absolute path
if ($prefix) {
$relative = true;
}
}

return $prefix.$this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
* If victoire_i18n.locale_pattern == domain, then we force the url rewrite with a valid host
* @param $locale
* @return null
*/
protected function getPrefix($locale)
{
foreach ($this->localeResolver->getDomainConfig() as $_domain => $_locale) {
if ($_locale === $locale) {
$urlPrefix = sprintf('%s://%s', $this->request->getScheme(), $_domain);
if ($this->request->getPort()) {
$urlPrefix .= ':'.$this->request->getPort();
}
return $urlPrefix;
}
}

return null;
}
}
10 changes: 7 additions & 3 deletions Bundle/I18nBundle/Form/PageTranslateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Translation\TranslatorInterface;
use Victoire\Bundle\CoreBundle\Entity\View;

/**
Expand All @@ -19,10 +20,11 @@ class PageTranslateType extends AbstractType
/**
* Constructor
*/
public function __construct($availableLocales, RequestStack $requestStack)
public function __construct($availableLocales, RequestStack $requestStack, TranslatorInterface $translator)
{
$this->availableLocales = $availableLocales;
$this->currentLocale = $requestStack->getCurrentRequest()->getLocale();
$this->translator = $translator;
}

/**
Expand All @@ -33,7 +35,9 @@ public function __construct($availableLocales, RequestStack $requestStack)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name');
->add('name', null, array(
'label' => 'form.view.type.name.label',
));

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$view = $event->getData();
Expand Down Expand Up @@ -67,7 +71,7 @@ protected function getAvailableLocales(View $view)

foreach ($this->availableLocales as $localeVal) {
if ($i18n->getTranslation($localeVal) === null) {
$choices[$localeVal] = 'victoire.i18n.viewType.locale.'.$localeVal;
$choices[$localeVal] = $this->translator->trans('victoire.i18n.viewType.locale.'.$localeVal, array(), 'victoire');
}
}

Expand Down
77 changes: 0 additions & 77 deletions Bundle/I18nBundle/Listener/LocaleListener.php

This file was deleted.

37 changes: 32 additions & 5 deletions Bundle/I18nBundle/Resolver/LocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ class LocaleResolver
protected $localeDomainConfig;
protected $availableLocales;
public $defaultLocale;
public $defaultDomain;

/**
* Constructor
*
* @param string $localePattern What is the strategy to resolve locale
* @param string $localeDomainConfig The locale domain config
* @param array $localeDomainConfig The locale domain config
* @param string $defaultLocale The default local app
* @param string $availableLocales The list of available locales
*/
public function __construct($localePattern, $localeDomainConfig, $defaultLocale, $availableLocales)
public function __construct($localePattern, array $localeDomainConfig, $defaultLocale, $availableLocales)
{
$this->localePattern = $localePattern;
$this->localeDomainConfig = $localeDomainConfig;
$this->defaultLocale = $defaultLocale;
$this->availableLocales = $availableLocales;

foreach ($this->localeDomainConfig as $_domain => $_locale) {
if ($_locale == $this->defaultLocale) {
$this->defaultDomain = $_domain;
break;
}
}
}

/**
Expand Down Expand Up @@ -70,10 +78,11 @@ public function resolveFromDomain(Request $request)
return $this->localeDomainConfig[$httpHost];
}

throw new \Exception(sprintf(
'Host "%s" is not defined in your locale_pattern_table in app/config/victoire_core.yml (%s available)',
error_log(sprintf(
'Host "%s" is not defined in your locale_pattern_table in app/config/victoire_core.yml (%s available), using default locale (%s) instead',
$httpHost,
implode(',', $this->localeDomainConfig)
implode(',', $this->localeDomainConfig),
$this->defaultLocale
));

}
Expand All @@ -94,4 +103,22 @@ public function resolveDomainForLocale($locale)

return $this->defaultLocale;
}

/**
* Return available locales
* @return array
*/
public function getAvailableLocales()
{
return $this->availableLocales;
}

/**
* return domain config
* @return array
*/
public function getDomainConfig()
{
return $this->localeDomainConfig;
}
}
1 change: 1 addition & 0 deletions Bundle/I18nBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ services:
arguments:
- "%victoire_i18n.available_locales%"
- "@request_stack"
- "@translator"
tags:
- { name: form.type, alias: victoire_view_translate_type }
Loading

0 comments on commit 4c3a259

Please sign in to comment.