Open weierophinney opened 4 years ago
@akram82Travel I was looking for it in ZendFramework documentation and I can't find any example. We need update the documentation.
What I have found is: https://stackoverflow.com/questions/25019100/php-zf2-translator-language-switch and, I guess, it should work. Implementation in the constructor probably is not the best idea - I would go with some listener on routing to inject proper locale to translator. Hope it helps until we write better docs for that use case.
Or maybe you - when you resolve your problem - would like to improve the docs and write cookbook how to do it? Thanks!
Originally posted by @michalbundyra at https://github.com/zendframework/zend-mvc-i18n/issues/19#issuecomment-527360232
Did you find any solution for this? I am stuck in exactly the same place as you. The solution provide by webimpress is for zf2 which is different. I would appreciate any help in here. Thanks
Originally posted by @beycome at https://github.com/zendframework/zend-mvc-i18n/issues/19#issuecomment-531015838
I will create a cookbook recipe for this topic which includes a listener like the following:
namespace Application\Listener;
use Locale;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
class LocaleListener extends AbstractListenerAggregate
{
private const REGEX_LOCALE = '#^(?P<locale>[a-z]{2,3}([-_][a-zA-Z]{2}|))#';
private const DEFAULT_LOCALE = 'de_DE';
/**
* @inheritDoc
*/
public function attach(EventManagerInterface $events, $priority = 1) : void
{
$this->listeners[] = $events->attach(
MvcEvent::EVENT_DISPATCH,
[
$this,
'setLocale',
]
);
}
public function setLocale(MvcEvent $event) : void
{
// Get and check the route match object
$routeMatch = $event->getRouteMatch();
if (! $routeMatch) {
$this->setDefaultLocale();
return;
}
// Get and check the parameter for current locale
$locale = $routeMatch->getParam('locale');
if (! $locale
|| ! preg_match(self::REGEX_LOCALE, $locale, $matches)
) {
$this->setDefaultLocale();
return;
}
Locale::setDefault(Locale::canonicalize($matches['locale']));
}
private function setDefaultLocale() : void
{
Locale::setDefault(self::DEFAULT_LOCALE);
}
}
The listener can be initialized in the method onBootstrap
of a module class and the parameter locale
can be added to the route(s).
Originally posted by @froschdesign at https://github.com/zendframework/zend-mvc-i18n/issues/19#issuecomment-531924657
Hi everyone, I successfully installed the zend-mvc-i18n in my ZF3 project and when I change the default language manually the message change. But i'm stuck with this: I use this two URL to change on fly the language between en_US and fr_FR
<?= $this->url($this->route, array('lang' => 'en_US'));?>
<?= $this->url($this->route, array('lang' => 'fr_FR'));?>
but it doesn't work et used also a javascript function:also it's not working.
Help please !
Originally posted by @akram82Travel at https://github.com/zendframework/zend-mvc-i18n/issues/19