'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:

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.

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:

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


Solution 1:[1]

It is possible, you have to load resources calling the addResource() method.

$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

Solution 2:[2]

This is not possible. If you want to use Symfony, stick to the conventions from 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..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rodrigoq
Solution 2 Emii Khaos