如何在 Symfony 中为翻译添加新域
How to add a new domain for translations in Symfony
我正在使用 symfony,在我的包中我需要创建一些翻译,但我宁愿在与 "messages" 不同的域中进行翻译,就像 FOS User Bundle 所做的那样(他们使用 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
Best Practices-guide 建议将它们存储在 app/Resources/translations/
中。或者,您可以将它们放在包的翻译文件夹中:src/MyBundle/Resources/translations
.
请注意,在使用翻译器时必须指定您的域,例如在你的树枝模板中。参见:
您必须指定域名。例如在树枝中:
{{ some_things | trans({}, 'my_domaine_name') }}
如果你有类似 x.en.yaml
的东西,并且你想在 twing 模板中使用它,你可以这样做:
{% trans from 'x' %}word_to_translate{% endtrans %}
我正在使用 symfony,在我的包中我需要创建一些翻译,但我宁愿在与 "messages" 不同的域中进行翻译,就像 FOS User Bundle 所做的那样(他们使用 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
Best Practices-guide 建议将它们存储在 app/Resources/translations/
中。或者,您可以将它们放在包的翻译文件夹中:src/MyBundle/Resources/translations
.
请注意,在使用翻译器时必须指定您的域,例如在你的树枝模板中。参见:
您必须指定域名。例如在树枝中:
{{ some_things | trans({}, 'my_domaine_name') }}
如果你有类似 x.en.yaml
的东西,并且你想在 twing 模板中使用它,你可以这样做:
{% trans from 'x' %}word_to_translate{% endtrans %}