I'm new to ZF2, and have tried setting up a locale switcher. The code seems to work, but no matter how I've tried, I can't get the switch to happen. I have localised DB calls, which all work fine, but the default gettext/Zend Translate never changes from the default locale.
My code is as follows:
module.config.php
<?php
return array(
'website' => 'o',
'accepted_locales' => array(
'en_GB' => array(
'country' => 'gb',
'language' => 'en',
'display_name' => 'English',
),
'no_NO' => array(
'country' => 'no',
'language' => 'no',
'display_name' => 'Norsk',
),
),
'default_locale' => 'en_GB',
'controllers' => array(
'invokables' => array(
'Foo\Controller\Bar' => 'Foo\Controller\BarController',
),
),
'router' => array(
'routes' => array(
'oranisation' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller][/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Foo\Controller',
'controller' => 'Bar',
'action' => 'index',
),
),
),
),
),
'translator' => array(
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Organisation' => __DIR__ . '/../view',
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
[...]
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
Custom Abstract Controller - method called for all actions on all controllers in the project
/**
* Determines most appropriate locale by checking browser locale, then using a default if
* browser is unsupported.
*
* @return String
*/
protected function getBestLocale()
{
$locales = $this->getServiceLocator()->get('Config')['accepted_locales'];
if ( isset($_GET['locale']) && in_array($_GET['locale'], array_keys($locales)) ) {
// If locale has been set manually, and is valid, configure the translator
$locale = $_GET['locale'];
} else {
if ( in_array($this->getRequest()->getCookie()->locale, array_keys($locales)) ) {
// If the locale is already set, ensure it's valid, and confirm the current locale
$locale = $this->getRequest()->getCookie()->locale;
} elseif ( in_array(locale_accept_from_http($this->getRequest()->getHeader()), array_keys($locales)) ) {
// If still not set, we will set from the client header
$locale = locale_accept_from_http($this->getRequest()->getHeader());
} else {
// Default
$locale = $this->getServiceLocator()->get('Config')['default_locale'];
}
}
$cookie = new SetCookie('locale', $locale, time()+60*60*24*30, null, 'localhost');
$this->getResponse()->getHeaders()->addHeader($cookie);
Locale::setDefault($locale);
$this->getServiceLocator()->get('Translator')->setLocale($locale);
return $locale;
}
I have translation files (.po/.mo set up for en_GB and no_NO) and I'm calling the following in my views:
<?php echo $this->translate('File list'); ?>
I ALWAYS get the untranslated strings.
There seem to be various ways of implementing this, and the variety is quite confusing - so to be specific, I'm using ZF 2.3.0. What am I missing please? Thanks in advance.