drb0901500211 2011-12-14 14:16
浏览 26

Zend框架多语言

I'm trying to make my Zend Framework website in multiple languages, I want it to be easy to increment the number of languages in my website.

I tried following this tutorial, but I'm having some problems: http://blog.hackix.com/series/working-with-zend_translate-and-poedit/

I got my language files nl_BE and en_US, changed the content in my views by e.g. echo $this->translate("Restricted section");

First, I was able to change the default language by changing it in the Bootstrap.php, but after a couple of tries to make it work without the Bootstrap.php and changing some code, even that doesn't work anymore.

I made a little form, with two images, a Belgian flag and a Brittish flag, if I click the Belgian flag I want to change the language to nl_BE, if I click the Brittish flag, I want to change the language to en_US.

This is my form:

<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>

This is my Bootstrap.php file, according to the tutorial I used:

<?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;
 }
}

This is my LangController.php, the Zend_Controller_Plugin_Abstract to handle the change of language, this file is placed in 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);
 }
}

Can someone please help me, tell me what I'm doing wrong? Because I've been trying for hours now and I think I'm really stuck.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大