从自定义目录加载 Symfony2 翻译

Load Symfony2 translations from custom directory

众所周知,Symfony2 在这些目录中查找翻译:

YourBundle\Resources\translations\ - here lot of .yml files
app\Resources\translations\ - here lot of .yml files

但是如果我想将我的翻译放入

中怎么办?

MyBundle\Resources\translations15_04_23\ - and lot of .yml files will lay here

我阅读了文档但没有看到解决方案,这可能吗?也许我省略了部分文档?有人可以帮我吗?谢谢!

的确,这里记录了它:http://symfony.com/doc/current/components/translation/custom_formats.html#components-translation-custom-loader

您的操作与文档中的完全相同,但是当您使用翻译加载器时,您可以通过自己选择目录来添加翻译:

$translator->addResource('my_format', __DIR__.'/translations/messages.txt', 'fr_FR');

(只需将 __DIR__ 替换为您明智选择的目录,如果您使用动态文件,请务必小心:例如,您应该使用服务以确保您的代码很容易可维护)