Sonata管理员语言环境随管理员用户的语言环境而变化

时间:2014-12-23 04:05:34

标签: symfony admin locale sonata

我使用sonata admin bundle构建了一个i18n管理站点。现在我想用管理员用户的语言环境集更改我的语言环境和翻译。例如,我有两个管理员用户,一个是en(userA),另一个是zh(UserB)。用户的区域设置分别在用户管理仪表板中设置为en / zh。

我的管理服务:

services:
    sonata.admin.post:
        class: Acme\StoreBundle\Admin\PostAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Project", label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - Acme\StoreBundle\Entity\Product
            - ~
        calls:
            - [ setTranslationDomain, [AcmeStoreBundle]]
            - [ setLabelTranslatorStrategy, [ @sonata.admin.label.strategy.native ]]

然后我的资源/翻译/ 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>

然后,我通过UserA登录管理员,消息(产品名称)就可以了。但我通过UserB登录消息仍然是en locale(产品名称)。当然,我可以为userB更改parameters.yml(%locale%)中的全局区域设置,但这对userA不利。

那么,我如何使用不同的管理员用户区域设置更改我的网站的区域设置(消息或翻译)? 提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以扩展登录成功处理程序并在会话中设置用户的区域设置。例如:

# app/config/config.yml
services:
    login_success_handler:
        parent: security.authentication.success_handler
        class:  MyVendor\MyBundle\LoginSuccessHandler

UPDATE:请务必在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);
    }
}

然后你可以create a LocaleListener与Symfony文档中的那个相似或完全相同。唯一的区别是,如果您永远不会在路线中定义_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));

答案 1 :(得分:0)

谢谢杰森。我这样做就是你的提示。但是,似乎没有完全调用login_success_handler。

我的config.yml:

services:
    login_success_handler:
        parent: security.authentication.success_handler
        class:  Acme\StoreBundle\EventListener\LoginSuccessHandler

    acme_locale.locale_listener:
        class: Acme\StoreBundle\EventListener\LocaleListener
        arguments: ["%kernel.default_locale%"]
        tags:
            - { name: kernel.event_subscriber }

src / Acme / StoreBundle / EventListener / LoginSuccessHandler.php

namespace Acme\StoreBundle\EventListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

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 Acme\StoreBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

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)),
        );
    }
}

我有什么问题?感谢。