ZF语言和翻译

发布于 2024-12-19 08:07:15 字数 217 浏览 1 评论 0原文

我需要一个良好的工作示例,并在 ZF 内进行语言和翻译。

我的需求如下: 如果未选择语言,则应使用默认值“en”。 (页面顶部有一个语言选择器。) lang 应存储在会话中。 翻译应通过 csv 文件完成。 我希望语言在 url 中不可见,因此如果可能的话,我不需要重新配置路由。

我找到了一些教程,但它们并没有真正为我工作...

任何帮助将不胜感激... 问候 安德里亚

I need a good working example with language and translate within ZF.

My need is folowing:
If no lang is selected, 'en' should be default. (On the top op the page there is a lang selector.)
The lang should be stored in the session.
The translation should be done via csv files.
I want to have the language invisible in the url, so I do not need to reconfigure routes if possible.

I found some tutorials but they are not really working for me...

Any help will be appreciated...
Regards
Andrea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-12-26 08:07:15

我使用这种方式使用数组而不是csv:

application/configs/application.ini

; plugins stuff
pluginPaths.Zle_Application_Resource = "Zle/Application/Resource"

; locale stuff
resources.locale.default = "it_IT"

; cachemanager settings TODO change cache adapter to memcache
resources.cachemanager.translator.frontend.name = Core
resources.cachemanager.translator.frontend.customFrontendNaming = false
resources.cachemanager.translator.frontend.options.lifetime = 7200
resources.cachemanager.translator.frontend.options.automatic_serialization = true
resources.cachemanager.translator.backend.name = File
resources.cachemanager.translator.backend.customBackendNaming = false
resources.cachemanager.translator.backend.options.cache_dir = APPLICATION_PATH "/../data/cache"
resources.cachemanager.translator.frontendBackendAutoload = false

; translation stuff
resources.translate.data = APPLICATION_PATH "/../data/locales"
resources.translate.options.disableNotices = 1
resources.translate.options.scan = 'directory'
resources.translate.log.stream.writerName = "Stream"
resources.translate.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/untranslated.log"
resources.translate.log.stream.writerParams.mode = "a"
resources.translate.cacheEnabled = true

; view stuff
resources.view.encoding = "UTF-8"
resources.view.helperPath.My_View_Helper = "My/View/Helper"

application/plugins/Language.php

class Plugin_Language extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var string session namespace
     */
    const SESSION_NS = 'Plugin_Language';

    /**
     * @var string default language for other users
     */
    const DEFAULT_LOCALE = 'it';

    /**
     * Called before Zend_Controller_Front enters its dispatch loop.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $session = new Zend_Session_Namespace(self::SESSION_NS);
        if (isset($session->language) && Zend_Locale::isLocale($session->language)) {
            // change locale for the application
            $locale = new Zend_Locale($session->language);
            Zend_Registry::set(
                Zend_Application_Resource_Locale::DEFAULT_REGISTRY_KEY,
                $locale
            );
            // change language for the translator
            Zend_Registry::get('Zend_Translate')->setLocale($locale);
        } else {
            /** @var $locale Zend_Locale */
            $locale = Zend_Registry::get('Zend_Locale');
            /** @var $translate Zend_Translate */
            $translate = Zend_Registry::get('Zend_Translate');
            // check if user language is translated
            if (!in_array($locale->getLanguage(), $translate->getList())) {
                // change language for the translator
                $translate->setLocale(self::DEFAULT_LOCALE);
            }
        }
    }
}

application/Bootrasp.php

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APPLICATION_PATH));
    $autoloader->addResourceType('plugin', 'plugins', 'Plugin');
    return $autoloader;
}

application/controllers/LocaleController.php

class LocaleController extends Zend_Controller_Action
{
    /**
     * @var Zend_Session_Namespace
     */
    protected $session;

    public function init()
    {
        $this->session = new Zend_Session_Namespace(
            Plugin_Language::SESSION_NS
        );
    }

    public function itAction()
    {
        $this->session->language = 'it_IT';
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }

    public function enAction()
    {
        $this->session->language = 'en_US';
        $this->_redirect($_SERVER['HTTP_REFERER']);

    }
}

library/My/Application/ Resource/Translate.php

class My_Application_Resource_Translate extends Zend_Application_Resource_Translate
{

    /**
     * Default key for cache manager
     */
    const DEFAULT_CACHE_KEY = 'translator';

    /**
     * Build a log object used internally by parent class
     *
     * @return void
     */
    protected function buildLog()
    {
        if (isset($this->_options['log'])) {
            if (is_array($this->_options['log'])) {
                $this->_options['log'] = Zend_Log::factory($this->_options['log']);
            } else {
                unset($this->_options['log']);
            }
        }
    }

    /**
     * Return string used for cache manager
     *
     * @return string the key used for cache manager
     */
    protected function getCacheKey()
    {
        return isset($this->_options['cacheKey'])
                ? $this->_options['cacheKey']
                : self::DEFAULT_CACHE_KEY;
    }

    /**
     * Retrieve translate object
     *
     * @throws Zend_Application_Resource_Exception if registry key was used
     *          already but is no instance of Zend_Translate
     * @return Zend_Translate
     */
    public function getTranslate()
    {
        if (null === $this->_translate) {
            $this->buildLog();
            // retrieve cache if requested
            if (isset($this->_options['cacheEnabled'])
                && $this->_options['cacheEnabled']
            ) {
                // check for cachemanager in bootstrap
                if (!$this->getBootstrap()->hasPluginResource('cachemanager')) {
                    throw new Zend_Application_Resource_Exception(
                        "You must configure the cachemanager with "
                        . "the key {$this->getCacheKey()}"
                    );
                }
                // bootstrap the cachemanager and retrieve it
                /** @var $cacheManager Zend_Cache_Manager */
                $cacheManager = $this->getBootstrap()
                    ->bootstrap('cachemanager')
                    ->getResource('cachemanager');
                // check for the given key
                if (!$cacheManager->hasCache($this->getCacheKey())) {
                    throw new Zend_Application_Resource_Exception(
                        "You must configure the cachemanager with "
                        . "the key {$this->getCacheKey()}"
                    );
                }
                // set cache for translator
                Zend_Translate_Adapter::setCache(
                    $cacheManager->getCache($this->getCacheKey())
                );
            }
            // fetch translate object into local variable
            $this->_translate = parent::getTranslate();
        }
        return $this->_translate;
    }
}

我创建这个目录:

/data/cache
/data/locales
/data/locales/it
/data/locales/en
/data/locales/logs

/data/locales/en/Foo.php

/**
 * Return Array Key => Translate EN
 *
 */
return array(
    'SEND' => 'Send',
    'SAVE' => 'Save',
    'EDIT' => 'Edit',
);

/data/locales/it/Foo.php

/**
 * Return Array Key => Translate IT
 *
 */
return array(
    'SEND' => 'Invia',
    'SAVE' => 'Salva',
    'EDIT' => 'Modifica',
);

libray/My/View/Helper/T.php

class Zle_View_Helper_T extends Zend_View_Helper_Translate
{
    /**
     * Shortcut helper to Zend_View_Helper_Translate
     * You can give multiple params or an array of params.
     * If you want to output another locale just set it as last single parameter
     * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
     * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
     *
     * @param string $messageid Id of the message to be translated
     *
     * @return string|Zend_View_Helper_Translate Translated message
     */
    public function t($messageid = null)
    {
        // TODO replace with php 5.3
        $arguments = func_get_args();
        return call_user_func_array(array($this, 'translate'), $arguments);
    }
}

最后使用translate so

:观点:

<span><?=$this->t('SEND')?>:</span>

形式:

$this->addElement('submit', 'submit', array('label' => 'SAVE'));

可能有更好的方法,我已经描述了我的!
我希望检查非常有帮助!

i use this way with use array and not csv:

application/configs/application.ini

; plugins stuff
pluginPaths.Zle_Application_Resource = "Zle/Application/Resource"

; locale stuff
resources.locale.default = "it_IT"

; cachemanager settings TODO change cache adapter to memcache
resources.cachemanager.translator.frontend.name = Core
resources.cachemanager.translator.frontend.customFrontendNaming = false
resources.cachemanager.translator.frontend.options.lifetime = 7200
resources.cachemanager.translator.frontend.options.automatic_serialization = true
resources.cachemanager.translator.backend.name = File
resources.cachemanager.translator.backend.customBackendNaming = false
resources.cachemanager.translator.backend.options.cache_dir = APPLICATION_PATH "/../data/cache"
resources.cachemanager.translator.frontendBackendAutoload = false

; translation stuff
resources.translate.data = APPLICATION_PATH "/../data/locales"
resources.translate.options.disableNotices = 1
resources.translate.options.scan = 'directory'
resources.translate.log.stream.writerName = "Stream"
resources.translate.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/untranslated.log"
resources.translate.log.stream.writerParams.mode = "a"
resources.translate.cacheEnabled = true

; view stuff
resources.view.encoding = "UTF-8"
resources.view.helperPath.My_View_Helper = "My/View/Helper"

application/plugins/Language.php

class Plugin_Language extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var string session namespace
     */
    const SESSION_NS = 'Plugin_Language';

    /**
     * @var string default language for other users
     */
    const DEFAULT_LOCALE = 'it';

    /**
     * Called before Zend_Controller_Front enters its dispatch loop.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $session = new Zend_Session_Namespace(self::SESSION_NS);
        if (isset($session->language) && Zend_Locale::isLocale($session->language)) {
            // change locale for the application
            $locale = new Zend_Locale($session->language);
            Zend_Registry::set(
                Zend_Application_Resource_Locale::DEFAULT_REGISTRY_KEY,
                $locale
            );
            // change language for the translator
            Zend_Registry::get('Zend_Translate')->setLocale($locale);
        } else {
            /** @var $locale Zend_Locale */
            $locale = Zend_Registry::get('Zend_Locale');
            /** @var $translate Zend_Translate */
            $translate = Zend_Registry::get('Zend_Translate');
            // check if user language is translated
            if (!in_array($locale->getLanguage(), $translate->getList())) {
                // change language for the translator
                $translate->setLocale(self::DEFAULT_LOCALE);
            }
        }
    }
}

application/Bootrasp.php

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APPLICATION_PATH));
    $autoloader->addResourceType('plugin', 'plugins', 'Plugin');
    return $autoloader;
}

application/controllers/LocaleController.php

class LocaleController extends Zend_Controller_Action
{
    /**
     * @var Zend_Session_Namespace
     */
    protected $session;

    public function init()
    {
        $this->session = new Zend_Session_Namespace(
            Plugin_Language::SESSION_NS
        );
    }

    public function itAction()
    {
        $this->session->language = 'it_IT';
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }

    public function enAction()
    {
        $this->session->language = 'en_US';
        $this->_redirect($_SERVER['HTTP_REFERER']);

    }
}

library/My/Application/Resource/Translate.php

class My_Application_Resource_Translate extends Zend_Application_Resource_Translate
{

    /**
     * Default key for cache manager
     */
    const DEFAULT_CACHE_KEY = 'translator';

    /**
     * Build a log object used internally by parent class
     *
     * @return void
     */
    protected function buildLog()
    {
        if (isset($this->_options['log'])) {
            if (is_array($this->_options['log'])) {
                $this->_options['log'] = Zend_Log::factory($this->_options['log']);
            } else {
                unset($this->_options['log']);
            }
        }
    }

    /**
     * Return string used for cache manager
     *
     * @return string the key used for cache manager
     */
    protected function getCacheKey()
    {
        return isset($this->_options['cacheKey'])
                ? $this->_options['cacheKey']
                : self::DEFAULT_CACHE_KEY;
    }

    /**
     * Retrieve translate object
     *
     * @throws Zend_Application_Resource_Exception if registry key was used
     *          already but is no instance of Zend_Translate
     * @return Zend_Translate
     */
    public function getTranslate()
    {
        if (null === $this->_translate) {
            $this->buildLog();
            // retrieve cache if requested
            if (isset($this->_options['cacheEnabled'])
                && $this->_options['cacheEnabled']
            ) {
                // check for cachemanager in bootstrap
                if (!$this->getBootstrap()->hasPluginResource('cachemanager')) {
                    throw new Zend_Application_Resource_Exception(
                        "You must configure the cachemanager with "
                        . "the key {$this->getCacheKey()}"
                    );
                }
                // bootstrap the cachemanager and retrieve it
                /** @var $cacheManager Zend_Cache_Manager */
                $cacheManager = $this->getBootstrap()
                    ->bootstrap('cachemanager')
                    ->getResource('cachemanager');
                // check for the given key
                if (!$cacheManager->hasCache($this->getCacheKey())) {
                    throw new Zend_Application_Resource_Exception(
                        "You must configure the cachemanager with "
                        . "the key {$this->getCacheKey()}"
                    );
                }
                // set cache for translator
                Zend_Translate_Adapter::setCache(
                    $cacheManager->getCache($this->getCacheKey())
                );
            }
            // fetch translate object into local variable
            $this->_translate = parent::getTranslate();
        }
        return $this->_translate;
    }
}

I create this directory:

/data/cache
/data/locales
/data/locales/it
/data/locales/en
/data/locales/logs

/data/locales/en/Foo.php

/**
 * Return Array Key => Translate EN
 *
 */
return array(
    'SEND' => 'Send',
    'SAVE' => 'Save',
    'EDIT' => 'Edit',
);

/data/locales/it/Foo.php

/**
 * Return Array Key => Translate IT
 *
 */
return array(
    'SEND' => 'Invia',
    'SAVE' => 'Salva',
    'EDIT' => 'Modifica',
);

libray/My/View/Helper/T.php

class Zle_View_Helper_T extends Zend_View_Helper_Translate
{
    /**
     * Shortcut helper to Zend_View_Helper_Translate
     * You can give multiple params or an array of params.
     * If you want to output another locale just set it as last single parameter
     * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
     * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
     *
     * @param string $messageid Id of the message to be translated
     *
     * @return string|Zend_View_Helper_Translate Translated message
     */
    public function t($messageid = null)
    {
        // TODO replace with php 5.3
        $arguments = func_get_args();
        return call_user_func_array(array($this, 'translate'), $arguments);
    }
}

and finally use translate so:

in view:

<span><?=$this->t('SEND')?>:</span>

in form:

$this->addElement('submit', 'submit', array('label' => 'SAVE'));

there are probably better ways, I have described my!
I hope to check out was very helpful!

淡莣 2024-12-26 08:07:15

我用以下解决方案解决了:

在 application.ini 中,我添加了

resources.frontController.plugins.LangSelector = "SC_Controller_Plugin_LangSelector"

在同一文件夹中,我使用 csv 文件、en.csv、fr.csv 和 de.csv 创建了一个文件夹 lang。

在boostrap中我初始化了翻译器

 protected function _initTranslate() {
    // 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');

    /**
     * Set up and load the translations (all of them!)
     * resources.translate.options.disableNotices = true
     * resources.translate.options.logUntranslated = true
     */
    $translate = new Zend_Translate('csv',
                    APPLICATION_PATH . '/configs/lang', 'auto',
                    array(
                        'disableNotices' => true, // This is a very good idea!
                        'logUntranslated' => false, // Change this if you debug
                        'scan' => Zend_Translate::LOCALE_FILENAME
                    )
    );

    /**
     * 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;
}

插件

class SC_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('sessionSC');

    $lang = $request->getParam('lang', '');
    // Register all your "approved" locales below.
    switch ($lang) {
        case "de":
            $langLocale = 'de_DE';
            break;
        case "fr":
            $langLocale = 'fr_FR';
            break;
        case "en":
            $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);
}

}

希望这是一个好的解决方案,欢迎任何评论
安德里亚

I solved with following solution:

In the application.ini I added

resources.frontController.plugins.LangSelector = "SC_Controller_Plugin_LangSelector"

In the same folder I created a folder lang with my csv files, en.csv, fr.csv and de.csv.

In the boostrap I initialized the translator

 protected function _initTranslate() {
    // 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');

    /**
     * Set up and load the translations (all of them!)
     * resources.translate.options.disableNotices = true
     * resources.translate.options.logUntranslated = true
     */
    $translate = new Zend_Translate('csv',
                    APPLICATION_PATH . '/configs/lang', 'auto',
                    array(
                        'disableNotices' => true, // This is a very good idea!
                        'logUntranslated' => false, // Change this if you debug
                        'scan' => Zend_Translate::LOCALE_FILENAME
                    )
    );

    /**
     * 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;
}

The plugin

class SC_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('sessionSC');

    $lang = $request->getParam('lang', '');
    // Register all your "approved" locales below.
    switch ($lang) {
        case "de":
            $langLocale = 'de_DE';
            break;
        case "fr":
            $langLocale = 'fr_FR';
            break;
        case "en":
            $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);
}

}

Hope this is a good solution, any comment is welcome
Andrea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文