Symfony 4.3 禁用翻译回退 [英] Symfony 4.3 disable translation fallback
问题描述
遵循有关翻译的 4.3 文档(https://symfony.com/doc/4.3/translation.html#basic-translation),我正在尝试翻译页面标题.
Following the 4.3 documentation about translations (https://symfony.com/doc/4.3/translation.html#basic-translation), I'm trying to translate a page title.
如果当前语言环境不存在翻译,我不想退回到任何内容.
I don't want to fallback to anything if the translation doesn't exist for the current locale.
现在,我正在获取默认区域设置翻译(如果存在)或它可以找到的第一个翻译.
Right now, I'm getting the default locale translation if it exists or the first translation it can find.
我怎样才能禁用它?这是我的/config/packages/translation.yaml 文件:
How can I disable that? Here's my /config/packages/translation.yaml file:
framework:
default_locale: de
translator:
default_path: '%kernel.project_dir%/translations'
这是我的控制器中的代码:
Here's the code inside my controller:
/** @var TranslatorInterface $translatorInterface */
$categoryTitle = $translatorInterface->trans('category_title',[]);
只要翻译键 'category_title' 存在于任何 messages.*.yaml
中,我就会得到一个值.
As long as the translation key 'category_title' exists in any messages.*.yaml
, I'm getting a value.
推荐答案
我找到了解决方案,/config/packages/framework.yaml需要更新:
I found the solution, /config/packages/framework.yaml needed to be updated:
framework:
secret: '%env(APP_SECRET)%'
default_locale: '%locale%'
translator: { fallbacks: '%fallback%' }
这是translation.yaml:
Here's translation.yaml:
framework:
default_locale: '%locale%'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks: '%fallback%'
最后在 services.yaml 中我有一个空数组用于回退:
And finally in services.yaml I have an empty array for fallback:
parameters:
locale: 'en'
locales: ['en', 'de', 'es', 'fr', 'it', 'nl', 'no', 'pt', 'sv']
fallback: []
这篇关于Symfony 4.3 禁用翻译回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!