我在Module.php中创建了一个onBootstrap()函数,因此我可以在某些视图助手上全局设置翻译text_domain。除了一个人之外,我几乎都在工作。
这是onBootstrap()函数:
python -m SimpleHTTPServer
我坚持的部分是:
/**
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event)
{
$sapi = php_sapi_name();
if ($sapi != 'cli' || $sapi != 'cli-server') {
$default = 'en-US';
$application = $event->getApplication();
/**
* @var \Zend\Authentication\AuthenticationService $session
*/
$session = $application->getServiceManager()
->get('Zend\Authentication\AuthenticationService');
if ($session->hasIdentity()) {
$locale = $session->getIdentity()->locale;
} else {
$locale = $default;
}
$application->getServiceManager()
->get('MvcTranslator')
->setLocale($locale)
->setFallbackLocale($default);
AbstractValidator::setDefaultTranslator(
$application->getServiceManager()
->get('MvcTranslator')
);
AbstractValidator::setDefaultTranslatorTextDomain('zend');
/**
* Translate Forms
*/
$viewRenderer = $application
->getServiceManager()
->get('ViewRenderer');
$application->getEventManager()
->getSharedManager()
->attach(
'Zend\Mvc\Controller\AbstractActionController',
'dispatch',
function ($event) use
(
$viewRenderer
) {
/**
* @var MvcEvent $event
*/
$controller = $event->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr(
$controllerClass,
0,
strpos(
$controllerClass,
'\\'
)
);
$textDomain = strtolower($moduleNamespace);
$viewRenderer->plugin('translate')
->setTranslatorTextDomain($textDomain);
$viewRenderer->navigation('Zend\Navigation\Navigation')
->setTranslatorTextDomain('employee');
$viewRenderer->formLabel()
->setTranslatorTextDomain($textDomain);
$viewRenderer->formText()
->setTranslatorTextDomain($textDomain);
$viewRenderer->formPassword()
->setTranslatorTextDomain($textDomain);
$viewRenderer->formSubmit()
->setTranslatorTextDomain($textDomain);
},
100
);
}
}
我没有收到任何错误,但它也没有按预期进行翻译。如果我将$viewRenderer->navigation('Zend\Navigation\Navigation')
->setTranslatorTextDomain($textDomain);
更改为不存在的内容,则会出现预期的错误。这样就告诉我它在配置中找到了关键。
我也尝试过,但仍然没有按预期进行翻译:
Zend\Navigation\Navigation
当我在我的观点中称呼它时,它会起作用:
$viewRenderer->plugin('navigation')
->setTranslatorTextDomain($textDomain);
然而,目标是从所有视图中删除该行,并在onBootstrap()函数中设置text_domain。
您可以看作是一项测试,我现在已将$this->navigation('Zend\Navigation\Navigation')
->setTranslatorTextDomain('employee');
硬编码为$textdomain
。
任何想法我能做什么?