Symfony2 Database Translation Loader isn't executed

半腔热情 提交于 2019-12-01 06:22:30

问题


I have to implement my own translation loader. I've used the tutorial on: http://blog.elendev.com/development/php/symfony/use-a-database-as-translation-provider-in-symfony-2/ to implement my own translation loader.

I don't get any error's by my code, but the load function of my Loader never gets executed.

Is there any way to tell symfony which translation should be executed?

config.yml

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

DBLoader.php

class DBLoader implements LoaderInterface {

private $transaltionRepository;
private $languageRepository;

/**
 * @param EntityManager $entityManager
 */
public function __construct(EntityManager $entityManager){
    $this->transaltionRepository = $entityManager->getRepository("MyskTranslationBundle:LanguageTranslation");
    $this->languageRepository = $entityManager->getRepository("MyskTranslationBundle:Language");
    echo "yeah";
}

function load($resource, $locale, $domain = 'messages'){
    die();
    //Load on the db for the specified local
    $language = $this->languageRepository->getLanguage($locale);
    $translations = $this->transaltionRepository->getTranslations($language, $domain);
    $catalogue = new MessageCatalogue($locale);

    foreach($translations as $translation){
        $catalogue->set($translation->getLanguageToken()->getToken(), $translation->getTranslation(), $domain);
    }

    return $catalogue;
}}

Any ideas?

Cheers Timo


回答1:


Aldo answered this already but just to make it an official answer and to help others:

You need to create a "fake" translation file to trigger your loader

From Symfony Dependency Injection Tags: "translation.loader"

[...] If you're loading translations from a database, you'll still need a resource file, but it might either be blank or contain a little bit of information about loading those resources from the database. The file is key to trigger the load method on your custom loader.

So you need to crate files of the form <domain>.<locale>.<loader-alias> in your translations folder app/Resources/translations/.

In your case one file would be app/Resources/translations/messages.en.db for English.




回答2:


http://blog.elendev.com/page/3/#post-26 or http://blog.elendev.com/development/php/symfony/use-a-database-as-translation-provider-in-symfony-2/ Another issue here is that there are no LanguageRepository class in the example. You can use this example.

<?php
namespace TranslationBundle\Repository;
use Doctrine\ORM\EntityRepository;
class LanguageRepository extends EntityRepository
{
    public function getLanguage($locale)
    {
        return $this->findOneBy(array('locale' => $locale));
    }
}


来源:https://stackoverflow.com/questions/13350866/symfony2-database-translation-loader-isnt-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!