symfony 2根据存储在数据库中的用户首选项设置区域设置 [英] symfony 2 set locale based on user preferences stored in DB

查看:45
本文介绍了symfony 2根据存储在数据库中的用户首选项设置区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据存储在数据库中的当前用户的偏好设置语言环境.

I am trying to set the locale based on the current user's preferences which are stored in the DB.

因此,我们的User类具有getPreferredLanguage,它返回语言环境标识("en","fr_FR"等).

Our User class therefore has a getPreferredLanguage which returns a locale identify ('en', 'fr_FR', etc.).

我考虑了以下方法:

  • 注册一个订阅KernelEvents :: REQUEST事件的区域设置"侦听器服务.
  • 此服务可以访问安全上下文(通过其构造函数)
  • 此服务的onKernelRequest方法尝试从安全上下文中获取用户,获取用户的首选语言环境,并将其设置为请求的语言环境.

不幸的是,这不起作用.调用区域设置"侦听器服务的onRequestEvent方法时,安全上下文没有令牌.似乎上下文侦听器是在很晚的阶段(优先级为0)被调用的,不可能告诉我的区域设置"侦听器在安全上下文之前运行.

Unfortunately, this doesn't work. When the "locale" listener service's onRequestEvent method is invoked, the security context does not have a token. It seems that the context listener is invoked at a very late stage (with a priority of 0), and it is impossible to tell my "locale" listener to run before the security context.

有人知道如何解决此问题,或建议另一种方法吗?

Does anyone know how to fix this approach, or suggest another one?

推荐答案

您可能对我在此答案中发布的区域设置侦听器感兴趣:

You may be interested in the locale listener, which I posted in this answer: Symfony2 locale detection: not considering _locale in session

如果用户在个人资料中更改其语言,则没问题.如果您使用的是FOSUserBundle(主用户),则可以加入个人资料编辑成功事件.否则,如果您使用的是自制系统,则在配置文件控制器中.这是FOSUserBundle的示例:

If a user changes his language in the profile, it's no problem. You can hook into profile edit success event if you're are using FOSUserBundle (master). Otherwise in your profile controller, if you're using a self made system. Here is a example for FOSUserBundle:

<?php
namespace Acme\UserBundle\EventListener;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ChangeLanguageListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
                FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
        );
    }

    public function onProfileEditSuccess(FormEvent $event)
    {
        $request = $event->getRequest();
        $session = $request->getSession();
        $form = $event->getForm();
        $user = $form->getData();
        $lang = $user->getLanguage();

        $session->set('_locale', $lang);
        $request->setLocale($lang);
    }
}

和services.yml

and in the services.yml

services:
    acme.change_language:
        class: Acme\UserBundle\EventListener\ChangeLanguageListener
        tags:
            - { name: kernel.event_subscriber }

在多个浏览器中进行多个会话没有问题,因为每个新会话都需要一个新的登录名.好的,不要更改语言,因为只会更新当前会话.但是您可以修改LanguageListener以支持此功能.
管理员更改语言的情况也可以忽略不计.

for multiple sessions in multiple browser is no problem, as every new session requires a new login. Hmm, ok, not after changing the language, as only the current session would be updated. But you can modify the LanguageListener to support this.
And the case if an admin changes the language should be insignificant.

这篇关于symfony 2根据存储在数据库中的用户首选项设置区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆