我需要一个很好的语言示例,并在ZF中进行翻译.
我的需求如下:
如果未选择lang,则应默认为“ en”. (在页面顶部,有一个lang选择器.)
lang应该存储在会话中.
翻译应通过csv文件完成.
我想让该语言在url中不可见,因此,如果可能的话,我不需要重新配置路由.
我找到了一些教程,但是它们对我来说并不是很有效.
任何帮助将不胜感激…
问候
安德里亚
解决方法:
我用这种方法使用数组而不是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"
应用程序/插件/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);
}
}
}
}
应用程序/Bootrasp.php
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APPLICATION_PATH));
$autoloader->addResourceType('plugin', 'plugins', 'Plugin');
return $autoloader;
}
应用程序/控制器/ 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']);
}
}
库/我的/应用程序/资源/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/zh-CN/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 /我的/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);
}
}
最后使用以下翻译:
鉴于:
=$this->t('SEND')?>:
通知:
$this->addElement('submit', 'submit', array('label' => 'SAVE'));
有更好的方法,我已经描述了我的!
我希望退房非常有帮助!
标签:zend-framework,php,zend-translate
来源: https://codeday.me/bug/20191202/2085245.html