Symfony 4.3 disable translation fallback

岁酱吖の 提交于 2021-02-11 15:53:37

问题


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.

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',[]);

As long as the translation key 'category_title' exists in any messages.*.yaml, I'm getting a value.


回答1:


Remove a fallback option from the translation.yaml file

framework:
    translator:
        fallbacks: []



回答2:


I found the solution, /config/packages/framework.yaml needed to be updated:

framework:
secret: '%env(APP_SECRET)%'
default_locale: '%locale%'
translator: { fallbacks: '%fallback%' }

Here's translation.yaml:

framework:
default_locale: '%locale%'
translator:
    default_path: '%kernel.project_dir%/translations'
    fallbacks: '%fallback%'

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: []


来源:https://stackoverflow.com/questions/61369820/symfony-4-3-disable-translation-fallback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!