From 906af9d44d37c51a741e5c50ead9d175297cd9b6 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 30 Apr 2021 10:31:37 +0200 Subject: [PATCH] add locales in navigation and in route loader --- core/Entity/Site/Navigation.php | 17 +++++++++++++++ core/Form/Site/NavigationType.php | 16 ++++++++++++++ core/Resources/translations/messages.fr.yaml | 2 ++ .../site/navigation_admin/_form.html.twig | 2 +- core/Router/SiteRouteLoader.php | 21 ++++++++++++++++++- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/core/Entity/Site/Navigation.php b/core/Entity/Site/Navigation.php index ec4ba62..79195f3 100644 --- a/core/Entity/Site/Navigation.php +++ b/core/Entity/Site/Navigation.php @@ -44,6 +44,11 @@ class Navigation implements EntityInterface */ private $menus; + /** + * @ORM\Column(type="string", length=10) + */ + private $locale = 'en'; + public function __construct() { $this->menus = new ArrayCollection(); @@ -135,4 +140,16 @@ class Navigation implements EntityInterface { return $this->getCode() ? $this->getCode() : 'navigation_'.$this->getId(); } + + public function getLocale(): ?string + { + return $this->locale; + } + + public function setLocale(string $locale): self + { + $this->locale = $locale; + + return $this; + } } diff --git a/core/Form/Site/NavigationType.php b/core/Form/Site/NavigationType.php index c778a9f..09f551f 100644 --- a/core/Form/Site/NavigationType.php +++ b/core/Form/Site/NavigationType.php @@ -8,6 +8,7 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints\Length; class NavigationType extends AbstractType { @@ -54,6 +55,21 @@ class NavigationType extends AbstractType ], ] ); + + $builder->add( + 'locale', + TextType::class, + [ + 'label' => 'Locale', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + new NotBlank(), + new Length(['min' => 2, 'max' => 10]), + ], + ] + ); } public function configureOptions(OptionsResolver $resolver) diff --git a/core/Resources/translations/messages.fr.yaml b/core/Resources/translations/messages.fr.yaml index 22b6a31..e2d552b 100644 --- a/core/Resources/translations/messages.fr.yaml +++ b/core/Resources/translations/messages.fr.yaml @@ -1,3 +1,4 @@ +"This is a test of translation": "Ceci est un test de traduction" "The code is not valid.": "Le code n'est pas valide." "Double authentication enabled.": "Double authentification activée." "Double authentication disabled.": "Double authentification désactivée." @@ -127,3 +128,4 @@ "Anyway": "Peu importe" "Yes": "Oui" "No": "Non" +"Locale": "Langue" diff --git a/core/Resources/views/site/navigation_admin/_form.html.twig b/core/Resources/views/site/navigation_admin/_form.html.twig index dcb7b82..f684b15 100644 --- a/core/Resources/views/site/navigation_admin/_form.html.twig +++ b/core/Resources/views/site/navigation_admin/_form.html.twig @@ -1,7 +1,7 @@
- {% for item in ['label', 'code', 'domain'] %} + {% for item in ['label', 'domain', 'locale', 'code'] %}
{{ form_row(form[item]) }}
diff --git a/core/Router/SiteRouteLoader.php b/core/Router/SiteRouteLoader.php index 4646528..bbe5f25 100644 --- a/core/Router/SiteRouteLoader.php +++ b/core/Router/SiteRouteLoader.php @@ -32,6 +32,16 @@ class SiteRouteLoader extends Loader $routes = new RouteCollection(); $navigations = $this->navigationQuery->find(); + $uniqueness = []; + + foreach ($navigations as $navigation) { + if (!isset($uniqueness[$navigation->getDomain()])) { + $uniqueness[$navigation->getDomain()] = true; + } else { + $uniqueness[$navigation->getDomain()] = false; + } + } + foreach ($navigations as $navigation) { foreach ($navigation->getMenus() as $menu) { foreach ($menu->getRootNode()->getAllChildren() as $node) { @@ -51,6 +61,7 @@ class SiteRouteLoader extends Loader $defaults = [ '_controller' => $node->getController() ?? PageController::class.'::show', + '_locale' => $navigation->getLocale(), '_node' => $node->getId(), '_menu' => $menu->getId(), '_page' => $node->getPage() ? $node->getPage()->getId() : null, @@ -69,7 +80,15 @@ class SiteRouteLoader extends Loader } } - $route = new Route($node->getUrl(), $defaults, $requirements); + $requirements['_locale'] = $navigation->getLocale(); + + $url = $node->getUrl(); + + if (!$uniqueness[$navigation->getDomain()]) { + $url = sprintf('/%s%s', $navigation->getLocale(), $url); + } + + $route = new Route($url, $defaults, $requirements); $route->setHost($navigation->getDomain()); $routes->add($node->getRouteName(), $route);