本文介绍了Zend 框架多语言集成步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 zend framework2 的新手,我正在开发一个具有多语言集成功能的网站.请给我一个想法,应该如何配置内置库和翻译文件,以及如何从视图文件中调用它

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

推荐答案

ZF2 已经集成了 I18n 工具.

ZF2 has already integrated I18n tools.

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

文件 *.mo

按照上一步,创建一个文件夹并使用 Poedit 添加您的 en_US.mo(例如)(简单好用)

Files *.mo

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

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

rq:我个人使用会话来存储我的语言环境,但这取决于我是否需要使用语言的 SEO

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!"); ?>

一些探索链接

08-03 18:56