Symfony 4 不会翻译并始终显示英语默认值
Symfony 4 won't translate and always shows the English default
我运行正在使用 Symfony 4,我使用
安装了翻译系统
composer require symfony/translation
我在 /translations
中创建了两个文件:
- messages.en.xlf
- messages.es.xlf
使用php bin/console debug:translation es
我也得到了正确的结果:
----------- ---------- ------ ---------------------- -------------------------------
State Domain Id Message Preview (es) Fallback Message Preview (en)
----------- ---------- ------ ---------------------- -------------------------------
messages test Value ES Value EN
----------- ---------- ------ ---------------------- -------------------------------
条目如下所示:
<trans-unit id="test">
<source>test</source>
<target>Value EN</target>
</trans-unit>
还有这个:
<trans-unit id="test">
<source>test</source>
<target>Value ES</target>
</trans-unit>
在我的控制器中,我设置了语言环境,这似乎工作正常并且也在 Twig 模板中表示:
public function index(Request $request, TranslatorInterface $translator)
{
$request->setLocale('es');
// prints 'es'
print $request->getLocale();
return $this->render();
}
在 Twig 中:
{# prints 'es' #}
{{ app.request.locale }}
然而,当我现在运行:
print $translator->trans('test');
或:
{{ 'test'|trans }}
我总是得到 Value EN
,这似乎不正确,应该是 Value ES
。
我 运行正在使用 Symfony 4.0 并多次清除缓存 - 甚至删除了整个文件夹。
translations.yml
看起来像这样:
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks:
- '%locale%'
services.yml
看起来像这样:
parameters:
locale: 'en'
locales: en|de|es
我在这里错过了什么?
To set the user's locale, you may want to create a custom event
listener so that it's set before any other parts of the system (i.e.
the translator) need it:
然后(这是解释代码行为的部分)
Setting the locale using $request->setLocale() in the controller is
too late to affect the translator. Either set the locale via a
listener (like above), the URL (see next) or call setLocale() directly
on the translator service.
我运行正在使用 Symfony 4,我使用
安装了翻译系统composer require symfony/translation
我在 /translations
中创建了两个文件:
- messages.en.xlf
- messages.es.xlf
使用php bin/console debug:translation es
我也得到了正确的结果:
----------- ---------- ------ ---------------------- -------------------------------
State Domain Id Message Preview (es) Fallback Message Preview (en)
----------- ---------- ------ ---------------------- -------------------------------
messages test Value ES Value EN
----------- ---------- ------ ---------------------- -------------------------------
条目如下所示:
<trans-unit id="test">
<source>test</source>
<target>Value EN</target>
</trans-unit>
还有这个:
<trans-unit id="test">
<source>test</source>
<target>Value ES</target>
</trans-unit>
在我的控制器中,我设置了语言环境,这似乎工作正常并且也在 Twig 模板中表示:
public function index(Request $request, TranslatorInterface $translator)
{
$request->setLocale('es');
// prints 'es'
print $request->getLocale();
return $this->render();
}
在 Twig 中:
{# prints 'es' #}
{{ app.request.locale }}
然而,当我现在运行:
print $translator->trans('test');
或:
{{ 'test'|trans }}
我总是得到 Value EN
,这似乎不正确,应该是 Value ES
。
我 运行正在使用 Symfony 4.0 并多次清除缓存 - 甚至删除了整个文件夹。
translations.yml
看起来像这样:
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks:
- '%locale%'
services.yml
看起来像这样:
parameters:
locale: 'en'
locales: en|de|es
我在这里错过了什么?
To set the user's locale, you may want to create a custom event listener so that it's set before any other parts of the system (i.e. the translator) need it:
然后(这是解释代码行为的部分)
Setting the locale using $request->setLocale() in the controller is too late to affect the translator. Either set the locale via a listener (like above), the URL (see next) or call setLocale() directly on the translator service.