【问题标题】:Using Doctrine extension Translatable with API Pplatform使用 Doctrine 扩展可翻译与 API 平台
【发布时间】:2021-01-08 21:22:22
【问题描述】:

我正在尝试使用 API Plateform 和 ReactJS 向我的应用程序添加多语言功能。 我已经安装了 StofDoctrineExtensionsBundle,我想使用扩展 Translatable。 我发送本地(“EN”或“FR”等)然后我想发送响应切换本地。

use Gedmo\Translatable\Translatable;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=CountryRepository::class)
 */
class Country implements Translatable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(type="string", length=255)
     */
    private $name;

services.yml

App\EventSubscriber\LocaleSubscriber:
    arguments: ['%kernel.default_locale%']

LocaleSubscriber.php

<?php
namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class LocaleSubscriber implements EventSubscriberInterface
{
    private $defaultLocale;

    public function __construct(string $defaultLocale = 'en')
    {
        $this->defaultLocale = $defaultLocale;
    }

    public function onKernelRequest(RequestEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }

        // try to see if the locale has been set as a _locale routing parameter
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            // must be registered before (i.e. with a higher priority than) the default Locale listener
            KernelEvents::REQUEST => [['onKernelRequest', 20]],
        ];
    }
}

在允许我获取国家列表的网络服务的响应中,没有名称字段。

如何让国家名称切换语言? 谢谢。

【问题讨论】:

    标签: symfony doctrine translation api-platform.com doctrine-extensions


    【解决方案1】:

    AFAIK 使用会话不是很 RESTfull,您的 api 将不再是无状态的。

    您可以使用接受语言请求标头。大多数浏览器会在每个请求中自动发送它。这是一个事件订阅者,它把它放在 symfonys 请求对象中:

    <?php
    // src/EventSubscriber/LocaleSubscriber.php
    
    namespace App\EventSubscriber;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpFoundation\HeaderUtils;
    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    
    class LocaleSubscriber implements EventSubscriberInterface
    {
    
        public function onKernelRequest(RequestEvent $event)
        {
            $request = $event->getRequest();
            $accept_language = $request->headers->get("accept-language");
            if (empty($accept_language)) {
                return;
            }
            $arr = HeaderUtils::split($accept_language, ',;');
            if (empty($arr[0][0])) {
                return;
            }
    
            // Symfony expects underscore instead of dash in locale
            $locale = str_replace('-', '_', $arr[0][0]);
    
            $request->setLocale($locale);
        }
    
        public static function getSubscribedEvents()
        {
            return [
                // must be registered before (i.e. with a higher priority than) the default Locale listener
                KernelEvents::REQUEST => [['onKernelRequest', 20]],
            ];
        }
    }
    

    如果您希望用户能够动态选择语言环境,我想从您自己的代码中为每个请求添加接受语言请求标头将覆盖浏览器的默认设置。

    我为my tutorial 制作了这个 EventSubscriber。第 3 章是关于本地化和国际化的。 api 端比 react 客户端简单。

    【讨论】:

    • BTW,你不需要添加一些配置来激活实现Translatable功能的Doctrine EventSubscriber吗?
    猜你喜欢
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多