Zend框架多语言

时间:2011-12-14 14:16:17

标签: php zend-framework localization multilingual

我正在尝试使用多种语言制作我的Zend Framework网站,我希望能够轻松增加网站中的语言数量。

我尝试过本教程,但是我遇到了一些问题: http://blog.hackix.com/series/working-with-zend_translate-and-poedit/

我收到了我的语言文件nl_BE和en_US,更改了我的观看内容echo $this->translate("Restricted section");

首先,我可以通过在Bootstrap.php中更改它来更改默认语言,但经过几次尝试使其无需Bootstrap.php并更改某​​些代码后,即使这样也不再有效。

我做了一个小形式,有两个图像,一个比利时国旗和一个英国国旗,如果我点击比利时国旗我想将语言更改为nl_BE,如果我点击英国国旗,我想将语言改为EN_US。

这是我的表格:

<form action="" method="post">
     <input type="image" src="images/belgium.png" value="nl_BE" name="lang" />
     <input type="image" src="images/brittain.png" value="en_US" name="lang"/>
 </form>

这是我的Bootstrap.php文件,根据我使用的教程:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

protected function _initTranslate() 
    {

    Zend_Loader::loadClass('Zend_Translate');
    Zend_Loader::loadClass('Zend_Registry');


    // Get current registry
    $registry = Zend_Registry::getInstance();
    /**
     * Set application wide source Locale
     * This is usually your source string language;
     * i.e. $this->translate('Hi I am an English String');
     */
    $locale = new Zend_Locale('en_US');    
    Zend_Registry::set('Zend_Locale', $locale);

    $session = new Zend_Session_Namespace('session');
    //$langLocale = isset($session->lang) ? $session->lang : $locale;
    $langLocale = $locale;

    /**
     * Set up and load the translations (all of them!)
     * resources.translate.options.disableNotices = true
     * resources.translate.options.logUntranslated = true
     */
    $translate = new Zend_Translate('gettext', 
        APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', 'auto',
        array(
        'disableNotices' => true,    // This is a very good idea!
        'logUntranslated' => false,  // Change this if you debug
        )
    );
    /**
     * Both of these registry keys are magical and makes
     * ZF 1.7+ do automagical things.
     */
    $registry->set('Zend_Locale', $locale);
    $registry->set('Zend_Translate', $translate);
    return $registry;
 }
}

这是我的LangController.php,Zend_Controller_Plugin_Abstract来处理语言的变化,这个文件放在library / App / Controller / Plugin中。

    <?php
class App_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
 {
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    $registry = Zend_Registry::getInstance();
    // Get our translate object from registry.
    $translate = $registry->get('Zend_Translate');
    $currLocale = $translate->getLocale();
    // Create Session block and save the locale
    $session = new Zend_Session_Namespace('session');

    $lang = $request->getParam('lang','');
    // Register all your "approved" locales below.
    switch($lang) {
        case "nl_BE": 
            $langLocale = 'nl_BE';
            break;   
        case "en_US": 
            $langLocale = 'en_US'; 
            break;
        default:
            /**
             * Get a previously set locale from session or set
             * the current application wide locale (set in 
             * Bootstrap)if not.
             */
            $langLocale = isset($session->lang) ? $session->lang : $currLocale;
    }

    $newLocale = new Zend_Locale();
    $newLocale->setLocale($langLocale);
    $registry->set('Zend_Locale', $newLocale);

    $translate->setLocale($langLocale);
    $session->lang = $langLocale;

    // Save the modified translate back to registry
    $registry->set('Zend_Translate', $translate);
 }
}

有人可以帮助我,告诉我我做错了什么?因为我现在已经尝试了几个小时,我想我真的被卡住了。

0 个答案:

没有答案