Symfony2数据库翻译加载器未执行 [英] Symfony2 Database Translation Loader isn't executed

查看:121
本文介绍了Symfony2数据库翻译加载器未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现自己的翻译加载器.我在以下方面使用了本教程: http://blog.elendev.com/development/php/symfony/use-a-database-as-translation-provider-in-symfony-2/ 来实现我自己的翻译加载器.

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.

我的代码没有收到任何错误,但是Loader的load函数从未执行过.

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

有什么办法告诉symfony应该执行哪个翻译?

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;
}}

有什么想法吗?

欢呼 蒂莫

推荐答案

Aldo已经回答了这个问题,但只是为了正式答复并帮助其他人:

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

来自 Symfony依赖项注入标记:"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.

因此,您需要在翻译文件夹app/Resources/translations/中创建格式为<domain>.<locale>.<loader-alias>的文件.

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

在您的情况下,一个文件为英语的app/Resources/translations/messages.en.db.

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

这篇关于Symfony2数据库翻译加载器未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆