简体   繁体   中英

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');

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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