Symfony表单 - 国家/地区选择区域设置不是symfony的默认值

时间:2015-04-24 11:39:14

标签: php forms symfony internationalization

我尝试使用国家/地区呈现表单选择。为此,我使用表单选择类型" country"。

http://symfony.com/doc/current/reference/forms/types/country.html

此时一切都很好。问题出在国际化上。如果用户使用其他语言,则不会翻译城市。

按照文档,"国家"选择使用" Locale :: getDefault()"猜猜这个地方。但它带有错误的语言环境。

echo \Locale::getDefault(); // echoes en.

$request = $this->get('request');
echo $request->getLocale(); // echoes symfony current user locale. fr_FR.

如何在" country"上使用当前的symfony区域设置?选择选择?

1 个答案:

答案 0 :(得分:2)

Nikos M.建议后,我创建了一个事件监听器来覆盖默认语言环境。

namespace Foo\AppBundle\EventListener;

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


/**
 * Event Listener that overrides intl default locale for "country" choice form
 *
 * @author some.cool.guy@foo.com
 */
class LocaleListener implements EventSubscriberInterface {

    public function onFilterControllerEvent(FilterControllerEvent $event)
    {
        $request = $event->getRequest();
        $locale = $request->getLocale();

        \Locale::setDefault($locale);
    }

    public static function getSubscribedEvents()
    {
        return array(
            // Before controller load due to BeSimpleI18nRoutingBundle
            KernelEvents::CONTROLLER => array(array('onFilterControllerEvent', 17)),

        );
    }
}