Man*_*olo 2 php internationalization symfony
我正在通过添加更多语言来改进我的Web应用程序.我按照本教程完成了它:https://coderwall.com/p/eiqd_g
一切似乎工作正常,但现在我已经发现,当试图在论坛上创建一个新帖子(HerzultForumBundle)时,我得到了上述异常.
我可以在调试器中看到:
在第8422行的kernel.root_dir/cache/dev/classes.php中
function twig_array_merge($arr1, $arr2)
{
if (!is_array($arr1) || !is_array($arr2)) {
throw new Twig_Error_Runtime('The merge filter only works with arrays or hashes.');
}
return array_merge($arr1, $arr2);
}
Run Code Online (Sandbox Code Playgroud)
在twig_array_merge(null,数组( '_区域设置'=> 'EN')) 在kernel.root_dir /高速缓存的/ dev /枝条/ C7/1/9188032d83474dae4ab5fad0cdaf278f4614c031cf7a5531428c5812bd57.php在线路200
echo ">
\t <a href=\"";
// line 81
echo twig_escape_filter($this->env, $this->env->getExtension('routing')->getPath($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route"), "method"), twig_array_merge($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route_params"), "method"), array("_locale" => (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale"))))), "html", null, true);
echo "\">";
echo twig_escape_filter($this->env, (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale")), "html", null, true);
echo "</a>
Run Code Online (Sandbox Code Playgroud)
所以看起来twig_array_merge()函数的第一个参数是null值.和HerzultForumBundle:Topic:new.html.twig模板似乎是原因.该HerzultForumBundle:Topic:new.html.twig模板包含以下代码:
{% extends 'HerzultForumBundle::layout.html.twig' %}
{% block title %}New Reply{% endblock %}
{% block content %}
<div class="forum post_new">
<ul class="crumbs">
<li><a href="{{ path('herzult_forum_index') }} ">Forum</a></li>
<li><a href="{{ forum_urlForCategory(topic.category) }}">{{ topic.category.name }}</a></li>
<li><a href="{{ forum_urlForTopic(topic) }}">{{ topic.subject }}</a></li>
<li>New Reply</li>
</ul>
<div class="main">
<h2>New Reply</h2>
<form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">
{{ form_widget(form) }}
<div>
<button type="submit" name="reply">Add post</button>
</div>
</form>
</div>
<div class="side">
<p><a href="{{ forum_urlForTopic(topic) }}">Back to the topic</a></p>
</div>
</div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
我的猜测是异常来自这一行:
<form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">
但不知道如何解决它.任何的想法?该论坛之前的工作非常顺利.
好吧,这是我的问题,在基本模板中:
<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
<li {% if locale == app.request.locale %}class="active"{% endif %}>
<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">{{ locale }}</a>
</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
所以认为这app.request.get('_route_params')是空的.我试过了:
<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
<li {% if locale == app.request.locale %}class="active"{% endif %}>
{% if app.request.get('_route_params') %} <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">{{ locale }}</a>{% endif %}
</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
是的!有用.我不会在论坛上翻译,但没关系,我认为这在英语中是可以理解的.