locastic/api-platform-translation-bundle

基于Sylius翻译的Api平台翻译包

安装数: 336,426

依赖者: 0

建议者: 0

安全: 0

星标: 85

关注者: 10

分支: 28

开放问题: 13

类型:symfony-bundle

v1.4 2024-05-10 11:45 UTC

This package is auto-updated.

Last update: 2024-09-10 13:13:06 UTC


README

Locastic Api Translation Bundle

基于ApiPlatformSylius翻译的翻译包

安装

$ composer require locastic/api-platform-translation-bundle

实现

可翻译实体

  • 使用Locastic\ApiTranslationBundle\Model\AbstractTranslatable扩展你的模型/resource
  • 添加createTranslation()方法,该方法返回新的翻译实体对象。例如
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;

class Post extends AbstractTranslatable
{
    // ...
    
    protected function createTranslation(): TranslationInterface
    {
        return new PostTranslation();
    }
}
  • 添加translations-属性。添加translations序列化组,并将其与翻译实体关联
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;

class Post extends AbstractTranslatable
{
    // ...
    
    /**
     * @ORM\OneToMany(targetEntity="PostTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     *
     * @Groups({"post_write", "translations"})
     */
    protected $translations;
}
  • 为所有可翻译字段添加虚拟字段,并添加读取序列化组。获取器和设置器必须调用翻译类的获取器和设置器。例如
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Symfony\Component\Serializer\Annotation\Groups;

class Post extends AbstractTranslatable
{
    // ...
    
    /**
    * @Groups({"post_read"})
    */
    private $title;
    
    public function setTitle(string $title)
    {
        $this->getTranslation()->setTitle($title);
    }

    public function getTitle(): ?string
    {
        return $this->getTranslation()->getTitle();
    }
}

翻译实体

  • 添加具有所有可翻译字段的实体。名称需要是可翻译实体的名称+Translation
  • 扩展Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation
  • 将序列化组translations添加到所有字段和其他读写组。例如翻译实体
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;

class PostTranslation extends AbstractTranslation
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Post", inversedBy="translations")
     */
    protected $translatable;
    
    /**
     * @ORM\Column(type="string")
     * 
     * @Groups({"post_read", "post_write", "translations"})
     */
    private $title;
    
    /**
     * @ORM\Column(type="string")
     *
     * @Groups({"post_write", "translations"})
     */
    protected $locale;

    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }
}

Api资源

  • 如果希望响应中返回所有翻译对象,请添加translation.groups过滤器。如果不使用translations组,则响应将只返回请求的locale翻译或回退locale翻译。
  • 为PUT和POST方法添加翻译到normalization_context,以确保它们返回所有翻译对象。
  • 示例
AppBundle\Entity\Post:
    itemOperations:
        get:
            method: GET
        put:
            method: PUT
            normalization_context:
                groups: ['translations']
    collectionOperations:
        get:
            method: GET
        post:
            method: POST
            normalization_context:
                groups: ['translations']
    attributes:
        filters: ['translation.groups']
        normalization_context:
            groups: ['post_read']
        denormalization_context:
            groups: ['post_write']

使用

显示单个翻译的语言参数

?locale=de

或使用Accept-Language http头

Accept-Language: de

显示所有翻译的序列化组

?groups[]=translations

POST翻译示例

{
    "datetime":"2017-10-10",
    "translations": { 
        "en":{
            "title":"test",
            "content":"test",
            "locale":"en"
        },
        "de":{
            "title":"test de",
            "content":"test de",
            "locale":"de"
        }
    }
}

编辑翻译示例

{
    "datetime": "2017-10-10T00:00:00+02:00",
    "translations": {
        "de": {
          "id": 3,
          "title": "test edit de",
          "content": "test edit de",
          "locale": "de"
        },
        "en": {
          "id": 2,
          "title": "test edit",
          "content": "test edit",
          "locale": "en"
        }
    }
}

贡献

如果您有改进此包的想法,请随时贡献。如果您有问题或发现了某些错误,请打开一个问题。

支持

希望我们帮助您使用此包或任何Api Platform/Symfony项目?请给我们发邮件至info@locastic.com