gpt4 book ai didi

symfony - Gedmo\Translatable 忽略配置中设置的默认语言环境

转载 作者:行者123 更新时间:2023-12-04 00:33:43 26 4
gpt4 key购买 nike

我决定在 Symfony 上安装 "gedmo/doctrine-extensions" 以使用 Translatable

它工作正常,只是监听器忽略了我指定的默认语言环境,总是回落到 en_US

可翻译作为服务插入:

#config.yml
services:
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setDefaultLocale, [ ru ] ]
- [ setTranslationFallback, [ true ] ]

当我尝试在数据库中find() 对象时,它总是获取en_US 而不是ru:

$test = $em->find('Vendor\Entity\Test', 1);
//outputs row with 'locale' = "en_US" from `ext_translations_test` table

仅当我直接指定语言时,例如:

$test->setTranslatableLocale('ru');
$em->refresh($test);

它给出了所需的翻译。

有什么方法可以设置有效的默认语言环境吗?

更新

config.yml 中添加另一个调用函数解决了这个问题,虽然现在我不太确定 setDefaultLocale() 函数实际上做了什么,如 Gedmo\Translatable\TranslatableListener::$defaultLocale 属性提供了我见过的最可怕的评论。将尝试找到更多...

services:
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setDefaultLocale, [ ru ] ]
- [ setTranslatableLocale, [ ru ] ]
- [ setTranslationFallback, [ true ] ]

最佳答案

根据:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/symfony2.md

Note: if you noticed, there's Acme\DemoBundle\Listener\DoctrineExtensionListener you will need to create this listener class if you use loggable or translatable behaviors. This listener will set the locale used from request and username to loggable. So, to finish the setup create Acme\DemoBundle\Listener\DoctrineExtensionListener

确保您也设置了内核监听器。

namespace Acme\DemoBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DoctrineExtensionListener implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function onLateKernelRequest(GetResponseEvent $event)
{
$translatable = $this->container->get('gedmo.listener.translatable');
$translatable->setTranslatableLocale($event->getRequest()->getLocale());
}

public function onKernelRequest(GetResponseEvent $event)
{
$securityContext = $this->container->get('security.context', ContainerInterface::NULL_ON_INVALID_REFERENCE);
if (null !== $securityContext && null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$loggable = $this->container->get('gedmo.listener.loggable');
$loggable->setUsername($securityContext->getToken()->getUsername());
}
}
}

并将以下内容添加到您的配置文件中:

services:
extension.listener:
class: Acme\DemoBundle\Listener\DoctrineExtensionListener
calls:
- [ setContainer, [ @service_container ] ]
tags:
# translatable sets locale after router processing
- { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
# loggable hooks user username if one is in security context
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

关于symfony - Gedmo\Translatable 忽略配置中设置的默认语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com