ddattee
ddattee

Reputation: 25

Change locale param in current URL in a ZF2 application

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

Answers (1)

ddattee
ddattee

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) ?>
  • The first 'locale' is for the /en in the url
  • The second 'locale' is to declare the language to use for the route translator
  • The 'force_canonical' => true is only here to display the full URL instead of the relative URL.
  • The last true is to tell the URL helper to use the current request params to assemble the URL

Upvotes: 1

Related Questions