问题
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