Reputation: 25
Basically I want to be able to redirect the visitor to the current translated page equivalent with a translated URL.
I have both content and url translation in place and all my routes start with the :locale
param.
How can I get the current URL translated ?
ex: On the page http://domain.com/fr/actualites I want to have a link that point to http://domain.com/en/news.
Upvotes: 0
Views: 236
Reputation: 25
I found the solution shortly after asking this question.
First retreive the current route name used :
<?php
$sm = $this->getHelperPluginManager()->getServiceLocator();
$route = $sm->get('Application')->getMvcEvent()->getRouteMatch()->getMatchedRouteName();
?>
And then display URL with the :locale
param changed while asking the helper to keep and translate current URL params:
<?php echo $this->url($route, ['locale' => 'en'], ['force_canonical' => true, 'locale' => 'en_US'], true) ?>
'locale'
is for the /en
in the url'locale'
is to declare the language to use for the route translator'force_canonical' => true
is only here to display the full URL instead of the relative URL.true
is to tell the URL helper to use the current request params to assemble the URLUpvotes: 1