简体   繁体   English

无法从 symfony 4.3.3 中的数据库加载翻译

[英]Cannot load translation from database in symfony 4.3.3

i'm trying to load translations from database in Symfony 4. The Translator instance doesn't call the custom loader i wrote using this tutorial ( https://medium.com/@andrew72ru/store-translation-messages-in-database-in-symfony-3f12e579df74 ).我正在尝试从 Symfony 中的数据库加载翻译 4. Translator 实例不会调用我使用本教程编写的自定义加载器( https://medium.com/@andrew72ru/store-translation-messages-in-database-在-symfony-3f12e579df74 中)。

I created dummy files in the /translation folder (messages.it.db) to trigger the loader but it doesn't get called.我在/translation文件夹 (messages.it.db) 中创建了虚拟文件来触发加载程序,但它没有被调用。

services.yaml服务.yaml

parameters:
    locales: ['it','en']
    db_i18n.entity: App\Entity\Translation
 services:
    translation.loader.db:
        class: App\Loader\DbLoader
        arguments:
          - '@service_container'
          - '@doctrine.orm.entity_manager'
        tags:
          - { name: translation.loader, alias: db}

DbLoader.php DbLoader.php

namespace App\Loader;


use Creative\DbI18nBundle\Interfaces\EntityInterface;
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;

class DbLoader implements LoaderInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $doctrine;

    /**
     * @var string
     */
    private $entityClass;

    public function __construct(ContainerInterface $container, EntityManagerInterface $doctrine)
    {
        $this->doctrine = $doctrine;
        $this->entityClass = $container->getParameter('db_i18n.entity');
    }

    public function load($resource, $locale, $domain = 'messages')
    {
        $messages = $this->getRepository()->findByDomainAndLocale($domain, $locale);
        $values = array_map(static function (EntityInterface $entity) {
            return $entity->getTranslation();
        }, $messages);

        $catalogue = new MessageCatalogue($locale, [
            $domain => $values
        ]);

        return $catalogue;
    }

    public function getRepository(): TranslationRepositoryInterface
    {
        return $this->doctrine->getRepository($this->entityClass);
    }
}

Here's my translation table这是我的翻译表

Here is the test code i'm using to call the Translator这是我用来调用翻译器的测试代码

TestController.php测试控制器.php

class TestController extends AbstractController
{
    /**
     * @Route("/test", name="test")
     */
    public function index(TranslatorInterface $translator)
    {
        $translator->trans('prova', [], 'messages', 'it');

        return new Response();
    }
}

The result is supposed to be "prova it" but I get "prova" instead, which is the key of the translation.结果应该是“prova it”,但我得到的是“prova”,这是翻译的关键。 I tried to put a dd() on the DbLoader constructor and it's never been called.我试图在 DbLoader 构造函数上放置一个dd()并且它从未被调用过。

I also have in my project Api Platform, but i don't think it's causing this problem.我的项目中也有 Api 平台,但我认为它不会导致这个问题。

I resolved my issue.我解决了我的问题。

By using dd() on my Translator instance i discovered that Symfony wasn't loading my translation files correctly.通过在我的 Translator 实例上使用dd()我发现 Symfony 没有正确加载我的翻译文件。 Looking through the properties i noticed the path of my translation files were not correct.查看属性,我注意到我的翻译文件的路径不正确。

I placed them in src/Resources/translations instead and then it worked!我将它们放在src/Resources/translations中,然后它就起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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