Don*_*Msc 5 php translation symfony
因此,我为Symfony 4框架提供了以下演示,该演示可以正常工作并按预期转换所有内容:
$translator = new \Symfony\Component\Translation\Translator('en_GB');
$translator->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader());
$translator->addResource('array', [
'sample' => 'The English translation goes here...',
], 'en_GB');
echo $translator->trans('sample');
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法使以下演示工作:
echo $this->get('translator')->trans('sample');
Run Code Online (Sandbox Code Playgroud)
适当的配置如下:
/config/services.yaml:
parameters:
locale: en
Run Code Online (Sandbox Code Playgroud)
/config/packages/translation.yaml:
framework:
default_locale: en
translator:
fallbacks: ['en']
paths:
- '%kernel.project_dir%/translations'
Run Code Online (Sandbox Code Playgroud)
/translations/messages.en.yaml:
sample: The English translation goes here...
Run Code Online (Sandbox Code Playgroud)
转储以下内容:
$request->getLocale();
$request->getDefaultLocale();
Run Code Online (Sandbox Code Playgroud)
产生以下字符串:
'en_GB'
'en'
Run Code Online (Sandbox Code Playgroud)
而且我尝试将翻译文件重命名为“消息”。恩 .yaml ''消息。zh_CN .yaml”,“消息。en_US .yaml”,然后运行“ php bin /控制台缓存:清除 ”以确保完全清除了缓存。
我似乎无法从yaml文件中显示翻译。没有错误显示,只有翻译键可见。
有人可以看到我在某个地方犯了一个明显的错误还是忽略了某些东西吗?
PHP 7.2.0-2 + ubuntu16.04.1 + deb.sury.org + 2(cli)(内置:2017年12月7日20:14:31)(NTS)Linux Mint 18,Apache2 Ubuntu。
我有同样的问题,我找到了解决方案。也许这也是其他人的解决方案。就我而言,命令
./bin/console cache:clear
Run Code Online (Sandbox Code Playgroud)
没有清除/ var / cache / translations文件夹。手动删除此文件夹后
rm -rf var/log/translations
Run Code Online (Sandbox Code Playgroud)
翻译加载得像预期的那样。
我不知道为什么在这种情况下cache:clear可能无法正常工作,但目前我对此很满意。也许下周我会花一些时间来调试原因。