使用Zend Translate进行翻译仅适用于OSX Firefox,而不适用于其他浏览器

时间:2015-06-14 16:08:09

标签: zend-framework cross-browser

我的翻译设置在OSX上的Firefox中完美运行(我的意思是,很好:))。在其他浏览器上,翻译保留为法语,我的默认语言。

我在视图中var_dump:

var_dump($this->translate('Envoyer')):

string 'Send' (length=4)

var_dump($this->translate()->getLocale()):

string 'en' (length=2)

这是我在bootstrap.php中的init_translate:

public function _initTranslate()
{

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


    ////////////////////////////
    // Validator messages translation 
    $language = 'fr';

    // On définit le traducteur à utiliser
    $translate = new Zend_Translate(
            array(
                    'adapter' => 'array',
                    'content' => APPLICATION_PATH . '/../resources/languages', 
                    'locale' => $language,
                    'scan' => Zend_Translate::LOCALE_DIRECTORY
        ));

    // Apply only on validation messages :
    Zend_Validate_Abstract::setDefaultTranslator($translate);


    ///////////////////////////

    // Site translation

    // 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('fr_CA');
    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;


}

我的语言切换器插件(作者:Danny Froberg):

class My_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 "fr":
                $langLocale = 'fr_CA'; break;
            case "en":
                $langLocale = 'en_CA'; 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;
                $langLocale = 'fr_CA'; break;

        }

        $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);
    }
}

我的选择器在视野中:

<a href="#" onclick="dopostLang(this.href, 'fr');">FR</a>
<a href="#" onclick="dopostLang(this.href, 'en');">EN</a>

javascript就是工作(var_dump显示实际的区域设置;&#34; fr&#34;当&#34; FR&#34;点击和&#34; en&#34;当&#34; EN&# 34;点击),所以我不明白我为什么会遇到浏览器问题?

我迷路了,如果你可以看一下并指出我的方向,我会非常感激。

感谢,

1 个答案:

答案 0 :(得分:0)

雅虎!自解决了! (但不知道为什么):))

但是,如果其他人陷入同样的​​愚蠢陷阱:)

首先在.config文件中启用翻译通知:

resources.translate.options.disableNotices = false
resources.translate.options.logUntranslated = false

Secondo,首先按照官方指南,然后尝试别的。出于一个神秘的原因,这一行引起了我的问题:

$translate = new Zend_Translate('gettext',
            APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', 'auto',
            array(
                'disableNotices' => false,    // This is a very good idea!
                'logUntranslated' => true,  // Change this if you debug
            )
        );

我替换了这一行(来自官方文档:http://framework.zend.com/manual/1.12/en/zend.translate.using.html):

$translate = new Zend_Translate(
            array(
                'adapter' => 'gettext',
                'content' => APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages/fr_CA.mo',
                'locale'  => 'fr'
            )
        );
        $translate->addTranslation(
            array(
                'content' => APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages/en_CA.mo',
                'locale'  => 'en'
            )
        );

谢谢:)