我需要实现一个多语言站点。比我需要使用i18n和翻译器,但路由翻译器有一些问题
这是我的module.config.php
{
return [
'router' => [
'router' => [
'router_class' => TranslatorAwareTreeRouteStack::class,
],
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/{contact}',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
]};
}
这是我的Module.php类
public function onBootstrap(MvcEvent $e)
{
/** @var \Zend\Mvc\I18n\Translator $translator */
$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');
$translator->addTranslationFilePattern(
'phpArray',
Resources::getBasePath(),
Resources::getPatternForValidator()
);
AbstractValidator::setDefaultTranslator($translator);
}
启动应用程序时出现此错误
Zend\Router\Exception\RuntimeException: No translator provided in ..vendor\zendframework\zend-router\src\Http\Segment.php on line 372
如何将翻译器传递给句段类?我需要使用其他类进行翻译吗?
答案 0 :(得分:0)
问题是配置。我移动了
'router' => [
'router_class' => \Zend\Mvc\I18n\Router\TranslatorAwareTreeRouteStack::class,
'routes' => [
],
],
在全局配置中,一切正常。