add locales in navigation and in route loader

This commit is contained in:
Simon Vieille 2021-04-30 10:31:37 +02:00
parent 4f6e4eaa3f
commit 906af9d44d
5 changed files with 56 additions and 2 deletions

View file

@ -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;
}
}

View file

@ -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)

View file

@ -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"

View file

@ -1,7 +1,7 @@
<div class="row">
<div class="col-12 p-3">
<div class="row">
{% for item in ['label', 'code', 'domain'] %}
{% for item in ['label', 'domain', 'locale', 'code'] %}
<div class="col-12">
{{ form_row(form[item]) }}
</div>

View file

@ -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);