简体   繁体   English

如何配置 symfony2 以使用自定义翻译文件名约定?

[英]How to configure symfony2 to use custom translation file names convention?

According to official documentation :根据官方文件

The filename of the translation files is also important: each message file must be named according to the following path: domain.locale.loader:翻译文件的文件名也很重要:每个消息文件必须根据以下路径命名:domain.locale.loader:

I must to name all files such as messages.en_US.php , navigation.en_US.php , admin.en_US.php etc. and place in some folder.我必须命名所有文件,例如messages.en_US.phpnavigation.en_US.phpadmin.en_US.php等,并放在某个文件夹中。

i18n\
   admin.en.php
   messages.en.php
   navigation.en.php
   admin.fr.php
   messages.fr.php
   navigation.fr.php

But my project structure should follow the following structure:但是我的项目结构应该遵循以下结构:

i18n\
   en_US\
      admin.php
      messages.php
      navigation.php
   fr_FR\
      admin.php
      messages.php
      navigation.php

So how to configure symfony2 to support my structure and use translations in common way:那么如何配置 symfony2 以支持我的结构并以通用方式使用翻译:

echo $this->get('translator')->trans('hello', array(), 'messages');

This is not possible. 这是不可能的。 If you want to use Symfony, stick to the conventions from Symfony! 如果要使用Symfony,请遵循Symfony的约定!

To be correct: It's not possible in a simple way. 正确的说:这不可能以简单的方式实现。 You would need to write your own translator component and throw the translator component from Symfony away.. 您将需要编写自己的翻译器组件,并将Symfony的翻译器组件扔掉。

It is possible, you have to load resources calling the addResource() method.有可能,您必须调用addResource()方法加载资源。

$translator->addLoader('php', new PhpFileLoader());
//Inside a loop:
$translator->addResource('php', 'path/to/messages.php', $locale);

Here is the documentation https://symfony.com/doc/4.1/components/translation.html#loading-message-catalogs这是文档https://symfony.com/doc/4.1/components/translation.html#loading-message-catalogs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM