我需要帮助获取请求区域设置代码。
我的理论是在URL中定义区域设置:
`http://localhost/en/services/web
for locale en(英文)
OR
`http://localhost/fr/services/web
for locale fr(French)
and then extract the locale from the URL and have the routes still work
该值将替换it
变量中的$lang
以下是Application Module Class
中的当前代码:
public function onBootstrap(MvcEvent $e)
{
$lang = 'it'; // this needs to reflect current request locale instead
$request = $e->getApplication()->getServiceManager()->get('Request');
$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');
$translator->addTranslationFile("phparray",Pluto::path('language',"$lang.php"));
$viewHelperManager = $e->getApplication()->getServiceManager()->get('ViewHelperManager');
$viewHelperManager->get('translate')->setTranslator($translator);
}
My solution would be to have the variable $lang populated with the request locale
和剩余的网址部分与路由
我想这也会对我的路由进行一些修改。
答案 0 :(得分:0)
晚上好!
您使用的是ZF2还是3?我邀请您查看https://github.com/juriansluiman/SlmLocale,它正是您正在尝试实现的目标(甚至更多),以适当的方式。几个月前我曾答应努力让它与ZF3兼容但暂时找不到时间,所以我稍后会对此进行调查,但如果这符合您的需要,请在ZF2中使用它:)
答案 1 :(得分:0)
您想要的确切方面是(如果您不想在应用中添加新模块):
对于ZF3:https://gist.github.com/Itach1Uchixa/6ec75b8f2af47a0b63e3f52fa0285a24
对于ZF2:https://gist.github.com/Itach1Uchixa/4840b004be2f2dcded19ec516e8aa6f1
ZF2的配置示例:
1步。将以下行添加到service_manager配置
'HttpRouter' => 'Application\Factory\LocalizedTreeRouteFactory'
2步。向您的活动经理注册听众
Application\Listener\RouteListener
3步。您的翻译器配置必须具有必须类似
的区域设置键'translator' => array(
// this will be default locale of route
'locale' => 'en_US',
// key must be locale that you want
'locales' => array(
'en_US' => 'English',
'fr' => 'French',
'ru_RU' => 'Russian'
),
),
步骤3是可选的,您可以更改路由工厂以使用其他配置 上面的配置将使您路由:
/your/route or en_US/your/route for English
/fr/your/route for French
/ru_RU/your/route for Russian