1
votes

Zend 2 Framework, layout.phtml.

Here i can translate strings with $this->translate('key') but i can't get locale with $this->getLocale(). Why? They're both methods from the same class Translator! All i get is this exception:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\View\HelperPluginManager::get was unable to fetch or create an instance for getLocale' in D:\Dropbox\Projects\www\linksync\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 496

Here's my module.config.php (Application module)

<?php
return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Segment',
                'options' => array(
                    'route'    => '/[:lang]',
                    'constraints' => array(
                        'lang'       => '[a-zA-Z]{2}',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                        'lang'       => 'en',
                    ),
                ),
            ),
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:lang/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:lang[/:controller[/:action[/:id]]]]',
                            'constraints' => array(
                                'lang'       => '[a-zA-Z]{2}',
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id'         => '[0-9]+',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translatorMvc' => 'MvcTranslator',
        ),
        'factories' => array(
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);
1
Most likely there is a locale somewhere, however it is not accessible via $this->getLocale(). Can you provide any reference that shows it should be there?hakre
I can't! All wrote above are my assumptions, because i don't know any other way to access the current locale. Is there another way? I searched, but i couldn't found any useful :\maxdelia
I don't know as well, I can only guess wildly, and I'd say I would first get the translation and then look inside the translation for the locale.hakre
It's what i try to achieve first, but any attempt to retrieve an instance of Translation ends in a null object :\ Anyway, since translation is realized with View Helper, i'll try to write a custom one to get the locale. I'll keep this thread updated, and i'll report any useful workaround.maxdelia

1 Answers

2
votes

The reason is that Translate is a view helper (See Reference) which makes it available via $this->. You will need to use the Zend_Locale object in the view/layout like this:

<?php
$locale = new Zend_Locale();

// Actual locale
print $locale->toString();

// if locale is 'de_AT' then 'de' will be returned as language
print $locale->getLanguage();

// if locale is 'de_AT' then 'AT' will be returned as region
print $locale->getRegion();