Symfony 应用程序中缺少翻译

Missing translations in a Symfony application

我有一个基于 Symfony 5.2 的网络应用程序。在某些视图中,探查器说缺少翻译。

有一种方法可以只对验证器和安全消息使用翻译,而根本不对应用字符串使用翻译吗?

这是我的 translation.yml:

framework:
    default_locale: pt_BR
    translator:
        default_path: '%kernel.project_dir%/translations'
        enabled: true
        fallbacks:
            - en

我知道这听起来有点傻,但是对于不需要翻译的字符串,您可以不使用翻译器。

实际上,这意味着不在您的模板中使用 |trans,并且很可能会告诉您的表单不应翻译标签。您可以通过 disabling the option translation_domain on your form types 手动或创建默认执行此操作的 FormExtension 来实现此目的。

FormTypeExtension 可能看起来像这样:

class TranslationDomainDefaultDisableExtension extends AbstractTypeExtension
{
    public static function getExtendedTypes()
    {
        return [FormType::class]; // The most generic type means, all types extending from it are also affected
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('translation_domain', false);
    }
}

如果您想要保留翻译以备将来使用,并且只需要消除缺少翻译的警告,您可以改为更新现有翻译:

php bin/console translation:update

The translation:update command extracts translation strings from templates of a given bundle or the default translations directory. It can display them or merge the new ones into the translation files.

When new translation strings are found it can automatically add a prefix to the translation message.

这将更新(或创建)您保留在 translations/ 中的翻译文件,其中缺少的字符串使用密钥作为文本,这可能是您想要的行为。不过要小心,这些新翻译可能会打乱您对英语的回退。您将必须检查并可能为此调整文件中的更改。