Zend Framework Multi Language Integration Steps

雨燕双飞 提交于 2019-12-04 15:03:34

问题


I am new to zend framework2 and I am working on a site with multi language integration. Please give me the idea about, how the builtin library and translation file should be configured, and how its can be called from view file


回答1:


ZF2 has already integrated I18n tools.

How to integrate it

module.config.php

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

Files *.mo

Following previous step, create a folder and add your en_US.mo (for example) using Poedit (simple and good application)

Module.php

public function onBootstrap($e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator
      ->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
      ->setFallbackLocale('en_US');
}

rq: Personally I use a session to stock my locale but it depends if I need a SEO using language

    // session container
    $sessionContainer = new Container('locale');

    // test if session language exists
    if(!$sessionContainer->offsetExists('mylocale')){
        // if not use the browser locale
        if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            $sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
        }else{
            $sessionContainer->offsetSet('mylocale', 'en_US');
        }

    }

    // translating system
    $translator = $serviceManager->get('translator');
    $translator ->setLocale($sessionContainer->mylocale)
                ->setFallbackLocale('en_US');

    $mylocale = $sessionContainer->mylocale;

How to use it

In a view, just type that:

 <?php echo $this->translate("Translate that!"); ?>

Some links to explore

  • http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
  • http://framework.zend.com/manual/2.2/en/modules/zend.i18n.translating.html
  • http://framework.zend.com/manual/2.2/en/user-guide/styling-and-translations.html


来源:https://stackoverflow.com/questions/19271950/zend-framework-multi-language-integration-steps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!