如何从Symfony4中的数据库加载翻译?

时间:2018-05-01 14:01:01

标签: symfony internationalization translation

根据this symfony documentation article,应该可以加载您自己的翻译格式。所以我试图从数据库中加载它们。

但无论我试图让这项工作。它没有。

使用数据库加载器是正确的方法还是我应该做些什么来从数据库加载翻译?

应用\翻译\装载机\ DatabaseLoader.php:

namespace App\Translation\Loader;

use App\Entity\Translation;
use App\Domain\TranslationManagerInterface;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;

/**
 * Database Loader
 */
class DatabaseLoader implements LoaderInterface
{
    /**
     * Translation Manager
     *
     * @var TranslationManagerInterface
     */
    private $_translationManager;

    /**
     * Constructor
     *
     * @param TranslationManagerInterface $translationManager
     */
    public function __construct(TranslationManagerInterface $translationManager)
    {
        $this->_translationManager = $translationManager;
    }

    /**
     * {@inheritDoc}
     */
    public function load($resource, $locale, $domain = 'general')
    {
        $translations = $this->_translationManager->findByLocaleAndDomain($locale, $domain);

        $catalogue = new MessageCatalogue($locale);

        /* @var Translation $translation */
        foreach($translations as $translation)
        {
            $catalogue->set(
                $translation->getToken(),
                $translation->getContent(),
                $translation->getDomain()()
            );
        }
    }
}

配置/ services.yaml:

# database loader
    translation.loader.database:
        class: 'App\Translation\Loader\DatabaseLoader'
        arguments: [ 'App\Domain\TranslationManager' ]
        tags:
            - { name: translation.loader, alias: database, priority: 100 }

我甚至尝试在内核请求上使用事件监听器手动将加载器添加到转换器。我不想使用完整的捆绑包,因为我已经是一个充满翻译的数据库

2 个答案:

答案 0 :(得分:0)

通过Symfony 4.2.4测试也许这个答案来得有点晚,但是我想帮助其他遇到同样问题的游客,他们和我一样绝望。

  1. 您的类DatabaseLoader中有一个小错误。您必须在函数load()中返回$ catalogue

    public function load($resource, $locale, $domain = 'general')
    {
        $translations = $this->_translationManager->findByLocaleAndDomain($locale, $domain);
    
        $catalogue = new MessageCatalogue($locale);
    
        /* @var Translation $translation */
        foreach($translations as $translation)
        {
            $catalogue->set(
                $translation->getToken(),
                $translation->getContent(),
                $translation->getDomain()()
            );
        }
    
        // you must return $catalogue here
        return $catalogue;
    }
    
  2. 您必须创建“假”翻译文件才能触发加载程序。看这里: https://stackoverflow.com/a/33300593/6709953

    示例:对于翻译域“ messages”和语言环境“ en”,必须有一个空文件 translations / messages.en.database

    (小提示:在config / services.yaml中,您设置了“别名:数据库”,这意味着翻译文件的后缀必须为.database)

  3. 如果旧的竞争翻译文件存在于文件夹 translation / 中,则必须删除它们。因此,例如,如果还有文件 translations / messages.en.yaml ,则必须将其删除。

答案 1 :(得分:0)

我扩展了Translator类,以避免必须为应用程序中的每个域创建一个虚拟翻译文件。

要覆盖标准的Translator组件,需要加载Compiler Pass。

翻译器编译器通行证:

namespace App\DependencyInjection\Compiler;

use App\Domain\TranslationManagerInterface;
use App\Translation\Translator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class TranslatorCompilerPass implements CompilerPassInterface
{
    private $translatorServiceId;
    private $readerServiceId;
    private $loaderTag;
    private $debugCommandServiceId;
    private $updateCommandServiceId;

    /**
     * @param string $translatorServiceId
     * @param string $readerServiceId
     * @param string $loaderTag
     * @param string $debugCommandServiceId
     * @param string $updateCommandServiceId
     */
    public function __construct(
        string $translatorServiceId = 'translator.default',
        string $readerServiceId = 'translation.reader',
        string $loaderTag = 'translation.loader',
        string $debugCommandServiceId = 'console.command.translation_debug',
        string $updateCommandServiceId = 'console.command.translation_update'
    ) {
        $this->translatorServiceId = $translatorServiceId;
        $this->readerServiceId = $readerServiceId;
        $this->loaderTag = $loaderTag;
        $this->debugCommandServiceId = $debugCommandServiceId;
        $this->updateCommandServiceId = $updateCommandServiceId;
    }

    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        $loaders = [];
        $loaderRefs = [];
        foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
            $loaderRefs[$id] = new Reference($id);
            $loaders[$id][] = $attributes[0]['alias'];
            if (isset($attributes[0]['legacy-alias'])) {
                $loaders[$id][] = $attributes[0]['legacy-alias'];
            }
        }

        if ($container->hasDefinition($this->readerServiceId)) {
            $definition = $container->getDefinition($this->readerServiceId);
            foreach ($loaders as $id => $formats) {
                foreach ($formats as $format) {
                    $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
                }
            }
        }

        $definition = $container->findDefinition($this->translatorServiceId);

        $definition
            ->setClass(Translator::class)
            ->replaceArgument(0, null)
            ->replaceArgument(1, $definition->getArgument(1))
            ->replaceArgument(2, $definition->getArgument(4)["cache_dir"])
            ->replaceArgument(3, $definition->getArgument(4)["debug"])
            ->replaceArgument(4, $definition->getArgument(4)["resource_files"])
            ->addMethodCall("setTranslationManager", [new Reference(TranslationManagerInterface::class)])
        ;


        if (!$container->hasParameter('twig.default_path')) {
            return;
        }

        if ($container->hasDefinition($this->debugCommandServiceId)) {
            $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
        }

        if ($container->hasDefinition($this->updateCommandServiceId)) {
            $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
        }
    }
}

翻译器类:

namespace App\Translation;

use App\Domain\TranslationManagerInterface;
use App\Entity\Model\Translation\Translation;
use Symfony\Component\Translation\Translator as SymfonyTranslator;

class Translator extends SymfonyTranslator
{
    /**
     * @var TranslationManagerInterface
     */
    private $_translationManager;

    /**
     * @param TranslationManagerInterface $translationManager
     */
    public function setTranslationManager(TranslationManagerInterface $translationManager)
    {
        $this->_translationManager = $translationManager;
    }

    /**
     * {@inheritdoc}
     */
    public function trans($id, array $parameters = [], $domain = null, $locale = null)
    {
        if (null === $locale && null !== $this->getLocale()) {
            $locale = $this->getLocale();
        }

        if (null === $locale) {
            $locale = $this->getFallbackLocales()[0]; // fallback locale
        }

        // the translation manager automagically creates new translation entries if it doesn't exist yet
        $translation = $this->_translationManager->findOneByTokenLocaleAndDomain($id, $locale, $domain);

        // check if it exists
        if (isset($translation) && null !== $translation && $translation instanceof Translation) {
            return strtr($translation->getContent(), $parameters);
        }

        // fallback
        return parent::trans($id, $parameters, $domain, $locale);
    }
}