用于 TLD 的 Symfony2 语言
我是 Symfony2 的新手,我正在寻找使用我的主机名的 TLD 选择语言。 (以正确的方式)
我已经找到了一些通过表单更改语言的方法: http://symfony .com/blog/play-with-the-user-language
但是当新用户连接时我需要能够选择语言:
- www.hostname.fr => fr
- www.hostname.it =>它
- www.hostname.co.uk => 使用
现在我添加一个为每个请求触发的服务侦听器:
services:
kernel.listener.domain_langue_listener:
class: acme\DemoBundle\Listener\DomainLangueListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onDomainParse }
类:
namespace acme\DemoBundle\Listener;
class DomainLangueListener
{
public function onDomainParse(Event $event)
{
$request = $event->getRequest();
$session = $request->getSession();
preg_match('/[^.]+$/', $request->getHost(), $match);
$session->setLocale($match[0]);
}
}
此侦听器可以工作,但我想使用一些参数来定义哪个 TLD 与哪种语言匹配,但在侦听器中我无法像这样访问容器:
$this->container->getParameter('tld_allowed');
我想知道是否有另一种方法可以访问侦听器中的参数 或者使用主机名选择语言的其他方法
感谢您的回答。
I'm new in Symfony2 and I'm looking for choose the language with the TLD of my hostname.
(in a proper way)
I already find some way to change the language with a form: http://symfony.com/blog/play-with-the-user-language
But I need to be able to select the language when a new user connect with:
- www.hostname.fr => fr
- www.hostname.it => it
- www.hostname.co.uk => en
For now I add a service listener that trigger for each request:
services:
kernel.listener.domain_langue_listener:
class: acme\DemoBundle\Listener\DomainLangueListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onDomainParse }
With the class:
namespace acme\DemoBundle\Listener;
class DomainLangueListener
{
public function onDomainParse(Event $event)
{
$request = $event->getRequest();
$session = $request->getSession();
preg_match('/[^.]+$/', $request->getHost(), $match);
$session->setLocale($match[0]);
}
}
This Listener works but I would like to use some Parameters to define which TLD match with which Language, But in the Listener I cannot access to the container like that:
$this->container->getParameter('tld_allowed');
I would like to know if there is another way to access to Parameters in a Listener
Or an other way to select a language with the hostname
Thanks for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了。现在您唯一要做的就是使用“参数”(构造函数的参数)将参数注入到侦听器中。
%foobar%
指“foobar”参数,@foobar
指“foobar”服务。还有听者:
You're almost there. The only thing you have to do now is inject the parameters into your listener, using "arguments" (arguments for the constructor).
%foobar%
refers to the "foobar" parameter,@foobar
refers to the "foobar" service.And the listener: