我用Sonata管理包构建了一个i18n管理站点。现在我想使用管理员用户的语言设置更改我的区域设置和翻译。例如,我有两个管理员用户,一个是en(userA),另一个是zh(UserB)。用户的语言环境分别在用户管理仪表板中设置 en/zh。
我的管理服务 :
services:
sonata.admin.post:
class: AcmeStoreBundleAdminPostAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Content", label: "Project", label_translator_strategy: sonata.admin.label.strategy.underscore }
arguments:
- ~
- AcmeStoreBundleEntityProduct
- ~
calls:
- [ setTranslationDomain, [AcmeStoreBundle]]
- [ setLabelTranslatorStrategy, [ @sonata.admin.label.strategy.native ]]
然后我的 Resources/translations/AcmeStoreBundle.en.xliff 和 Resources/translations/AcmeStoreBundle.zh.xliff 就像这样:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>label.product.name</source>
<target>Product Name</target> ##---> zh is diffrent here!!!
</trans-unit>
</body>
</file>
</xliff>
然后,我通过用户A登录管理员,消息(产品名称)正常。但是我通过用户B登录,该消息仍然是区域设置(产品名称)。 当然,我可以在 parameters.yml (%locale%) 中为用户 B 更改全局语言环境,但这对用户 A 不利。
那么,如何使用不同管理员的用户区域设置更改我网站的区域设置(消息或翻译)?提前谢谢。
您可以扩展登录成功处理程序并在会话中设置用户的区域设置。例如:
# app/config/config.yml
services:
login_success_handler:
parent: security.authentication.success_handler
class: MyVendorMyBundleLoginSuccessHandler
更新:确保在您的 security.yml 中指向此侦听器:
# app/config/security.yml
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: login
check_path: login_check
success_handler: login_success_handler
然后添加登录成功处理程序类:
class LoginSuccessHandler extends DefaultAuthenticationSuccessHandler
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$locale = $token->getUser()->getLocale()
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
return parent::onAuthenticationSuccess($request, $token);
}
}
然后,您可以创建一个与Symfony文档中相似或完全相同的LocaleListener。 唯一的区别是,如果您永远不会在路由中定义_locale
,则可以更改:
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
到只是
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
谢谢杰森。我这样做是你的提示。但是,login_success_handler似乎并不完全称呼。
我的配置.yml:
services:
login_success_handler:
parent: security.authentication.success_handler
class: AcmeStoreBundleEventListenerLoginSuccessHandler
acme_locale.locale_listener:
class: AcmeStoreBundleEventListenerLocaleListener
arguments: ["%kernel.default_locale%"]
tags:
- { name: kernel.event_subscriber }
以及 src/Acme/StoreBundle/EventListener/LoginSuccessHandler.php
namespace AcmeStoreBundleEventListener;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
class LoginSuccessHandler extends DefaultAuthenticationSuccessHandler
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$locale = $token->getUser()->getLocale();
file_put_contents('/tmp/login.log', $locale, FILE_APPEND); ## I can't find the log file
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
}
}
和src/Acme/StoreBundle/EventListener/LocaleListener.php
namespace AcmeStoreBundleEventListener;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
我怎么了?谢谢。