symfony translation by using keys

2019-04-14 23:38发布

问题:

If I want to translate content in symfony, I will use the translator as it is described in the book:

$translated = $this->get('translator')->trans('Symfony2 is great');

But, if this translations are already existing in a database, how can I access this?

The db looks like

ID | locale | type   | field            | content
 1 |   en   | message| staff.delete     | delete this user?

I wil have to tell the translator where he can get the translation information. Cann you help me with a good tutorial or tipps an tricks?

回答1:

According to docs you need to register a service in order to load translations from other source like from database

You can also store translations in a database, or any other storage by providing a custom class implementing the LoaderInterface interface. See the translation.loader tag for more information.Reference

What i have done,i have a translation bundle where my translation entity resides so i have registered a service in config.yml and passed doctrine manager @doctrine.orm.entity_manager in order to get data from entity

services:

    translation.loader.db:
        class: Namespace\TranslationBundle\Loader\DBLoader
        arguments: [@doctrine.orm.entity_manager]
        tags:
            - { name: translation.loader, alias: db}

In DBLoader class i have fetched translations from database and sets as mentioned in docs translation.loader

My Loader class

namespace YourNamespace\TranslationBundle\Loader;

use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Doctrine\ORM\EntityManager;

class DBLoader implements LoaderInterface{
    private $transaltionRepository;
    private $languageRepository;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager){

        $this->transaltionRepository = $entityManager->getRepository("YourNamespaceTranslationBundle:LanguageTranslation");
        $this->languageRepository = $entityManager->getRepository("YourNamespaceTranslationBundle:Language");
    }

    function load($resource, $locale, $domain = 'messages'){
        //Load on the db for the specified local

        $language = $this->languageRepository->findOneBy( array('locale' => $locale));

        $translations = $this->transaltionRepository->getTranslations($language, $domain);

        $catalogue = new MessageCatalogue($locale);


        /**@var $translation YourNamespace\TranslationBundle\Entity\LanguageTranslation */
        foreach($translations as $translation){
            $catalogue->set($translation->getLanguageToken(), $translation->getTranslation(), $domain);
        }        
        return $catalogue;
    }
}

Note: Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resources: php app/console cache:clear