好的,我设法将我的测试页面的代码与JMSTranslationBundle结合在一起来切换语言,如下所示
<li>
<a href="{{ path("main", {"_locale": "en","name": name}) }}">
<img src="{{ asset('img/flags/gb.png') }}"></a></li>
<li>
<a href="{{ path("main", {"_locale": "de","name": name}) }}">
<img src="{{ asset('img/flags/de.png') }}"></a></li>但这将适用于path ("main"),我如何才能使其动态地适用于我当前正在处理的页面/路由,包括所需的参数(在本例中为"name": name?所以如果我现在在“关于我们”的英文页面上,我可以自动切换到德语页面“关于我们”,包括它的参数?有可能吗?或者我必须用路径硬编码每个小树枝页面/模板?
发布于 2012-01-10 22:43:42
这描述了一个与您正在寻找的扩展完全相同的扩展:http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
可以通过注入@service_container来检索路由器和请求来修复ScopeWideningInjectionException,而不是直接注入它们。
发布于 2012-01-06 01:06:58
硬编码是一个糟糕的想法,这是可以实现的,但据我所知,这不是现成的。为了给一个具有相同路径和参数的url提供不同的语言环境,我创建了一个定制的twig扩展来实现这一点。
这个扩展提供了一个新的twig函数,它将读取当前路由、当前参数、消除私有参数并生成相同的路由,但不适用于其他语言环境。下面是有问题的twig扩展:
<?php
namespace Acme\WebsiteBundle\Twig\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LocalizeRouteExtension extends \Twig_Extension
{
protected $request;
protected $router;
public function __construct(ContainerInterface $container)
{
$this->request = $container->get('request');
$this->router = $container->get('router');
}
public function getFunctions()
{
return array(
'localize_route' => new \Twig_Function_Method($this, 'executeLocalizeRoute', array()),
);
}
public function getName()
{
return 'localize_route_extension';
}
/**
* Execute the localize_route twig function. The function will return
* a localized route of the current uri retrieved from the request object.
*
* The function will replace the current locale in the route with the
* locale provided by the user via the twig function.
*
* Current uri: http://www.example.com/ru/current/path?with=query
*
* In Twig: {{ localize_route('en') }} => http://www.example.com/en/current/path?with=query
* {{ localize_route('fr') }} => http://www.example.com/fr/current/path?with=query
*
* @param mixed $parameters The parameters of the function
* @param string $name The name of the templating to render if needed
*
* @return Output a string representation of the current localized route
*/
public function executeLocalizeRoute($parameters = array(), $name = null)
{
$attributes = $this->request->attributes->all();
$query = $this->request->query->all();
$route = $attributes['_route'];
# This will add query parameters to attributes and filter out attributes starting with _
$attributes = array_merge($query, $this->filterPrivateKeys($attributes));
$attributes['_locale'] = $parameters !== null ? $parameters : \Locale::getDefault();
return $this->router->generate($route, $attributes);
}
/**
* This function will filter private keys from the attributes array. A
* private key is a key starting with an underscore (_). The filtered array is
* then returned.
*
* @param array $attributes The original attributes to filter out
* @return array The filtered array, the array withtout private keys
*/
private function filterPrivateKeys($attributes)
{
$filteredAttributes = array();
foreach ($attributes as $key => $value) {
if (!empty($key) && $key[0] != '_') {
$filteredAttributes[$key] = $value;
}
}
return $filteredAttributes;
}
}现在,您可以通过捆绑包或直接将此服务定义加载到位于app/config下的config.yml文件中,从而启用此twig扩展。
services:
acme.twig.extension:
class: Acme\WebsiteBundle\Twig\Extension\LocalizeRouteExtension
scope: request
arguments:
request: "@request"
router: "@router"
tags:
- { name: twig.extension }现在,您可以在twig中执行此操作,以建议当前加载的页面的不同版本:
<a id='englishLinkId' href="{{ localize_route('en') }}">
English
</a>
<a id='frenchLinkId' href="{{ localize_route('fr') }}">
Français
</a>希望这对你有所帮助,这就是你要找的。
编辑:
似乎不可能直接缩小细枝扩展的范围。要避免这种情况,请始终注入依赖项容器,然后检索所需的服务。这应该通过更改twig扩展的构造函数定义和扩展的服务定义来反映出来。我编辑了我之前的答案,检查新的更新的构造函数定义和新的服务定义。
另一种可能的解决方案是注入一个助手服务,该服务负责本地化路由。此帮助器服务应在请求范围内。实际上,这就是我在代码中所做的。我有一个注入到我的twig扩展中的RoutingHelper服务。然后,在executeLocalizeRoute方法中,我使用我的帮助器来完成繁重的工作。
告诉我现在是否一切正常。
致以敬意,
哑光
https://stackoverflow.com/questions/8741363
复制相似问题