zend-framework2zend-translate

ZF2 how to change locale in the view


my config is

'translator' => array(
'locale' => 'en_EN',
'translation_file_patterns' => array(
    array(
        'type'     => 'gettext',
        'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

and how i can change locale from my view or controller?


Solution

  • Fetch the Translator instance and invoke the setLocale() method. Example (controller context):

    // Get the translator from the service locator.
    $translator = $this->getServiceLocator()->get('translator');
    $translator->setLocale($myLocale);