1

我用奏鸣曲管理包构建了一个 i18n 管理站点。现在我想用管理员用户的语言环境集更改我的语言环境和翻译。比如,我有两个管理员用户,一个是en(userA),另一个是zh(UserB)。用户的语言环境分别在用户管理仪表板中设置为 en/zh。</p>

我的管理服务:</p>

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 ]]

然后我的 Resources/translations/AcmeStoreBundle.en.xliff 和 Resources/translations/AcmeStoreBundle.zh.xliff 就像这样:</p>

<?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(product name) 。当然,我可以在 parameters.yml (%locale%) 中为 userB 更改全局语言环境,但这对 userA 不利。

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

4

2 回答 2

0

谢谢杰森。我按照你的提示做了。但是,login_success_handler 似乎没有被完全调用。</p>

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

我怎么了?谢谢。

于 2014-12-23T08:00:59.957 回答
0

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

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

更新:确保在你的 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));
于 2014-12-23T06:56:48.030 回答