我正在使用symfony,在我的捆绑包中,我需要创建一些翻译,但我宁愿在不同的域而不是"消息"中进行,就像FOS用户捆绑包一样(他们使用FOSUserBundle域(。
所以我用我的翻译创建了一个MyBundle.en.yml文件,但它们没有加载。我已经在文档中阅读了我需要这样做的内容:
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
$translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
$translator->addResource(
'xlf',
'navigation.fr.xlf',
'fr_FR',
'navigation'
);
http://symfony.com/doc/current/components/translation.html#using-message-domains
但是我应该在哪里做呢?
更新
经过一些调查,如果我运行翻译的调试,它会说我在模板中用于我的域的所有翻译都丢失了。
我的翻译文件位于 src/Acme/Bundle/MyBundle/resources/translations/MyDomain.yml
我试图找到app/Resources/translation/MyDomain.yml
但相同的结果。
我也尝试删除缓存(rm -rf app/cache
(,但仍然无法正常工作
>如果您将文件放置在正确的位置,Symfony将自动为您执行此操作。您可以在文档中找到Symfony所采用的约定的完整描述:https://symfony.com/doc/current/translation.html#translation-resource-file-names-and-locations
最佳实践指南建议将它们存储在 app/Resources/translations/
中。或者,您可以将它们放在捆绑包的翻译文件夹中:src/MyBundle/Resources/translations
.
请注意,在使用翻译器时,您必须指定您的域,例如在您的树枝模板中。看:
- https://symfony.com/doc/current/components/translation.html#using-message-domains
- https://symfony.com/doc/current/translation.html#twig-templates
指定域名。 例如在树枝中:
{{ some_things | trans({}, 'my_domaine_name') }}
如果你有类似x.en.yaml
的东西,并且你想在twing模板中使用它,你可以做这样的事情:
{% trans from 'x' %}word_to_translate{% endtrans %}