Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Behaviors Laravel Package

knplabs/doctrine-behaviors

View on GitHub
Deep Wiki
Context7

Translatable - API Platform

How to use Translatable with API platform.

Let's say you have a Document Entity like so:

Please note: you don't have a title property in this entity, however it has a getter for it, we are accessing the DocumentTranslation via the TranslatableMethodsTrait you can do this for all the translatable properties.

use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

class Document implements TranslatableInterface
{
use TranslatableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

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

Then in the DocumentTranslation entity:

use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

#[ORM\Entity]
class DocumentTranslation implements TranslationInterface
{
    use TranslationTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\Column(type: 'string', length: 255)]
    private string $title;

    public function getId(): int
    {
        return $this->id;
    }

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

Now we can implement an Event Subscriber to listen to the accept-language header on each request:

<?php

declare(strict_types=1);

namespace App\Event\Subscriber;

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
{
    /**
     * [@return](https://github.com/return) array<string, array<int[]|string[]>>
     */
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => [['onKernelRequest', 20]],
        ];
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        $request = $event->getRequest();
        $acceptLanguage = $request->headers->get('accept-language');
        if (empty($acceptLanguage)) {
            return;
        }

        $arr = HeaderUtils::split($acceptLanguage, ',;');
        if (empty($arr[0][0])) {
            return;
        }

        // Symfony expects underscore instead of dash in locale
        $locale = str_replace('-', '_', $arr[0][0]);

        $request->setLocale($locale);
    }
}

Now we can make a request to /api/documents do not for get to add the Accept-Language header value, in this case it's set to 'cz'. When you change the Accept-Language header value, notice the title change language.

{
  "[@context](https://github.com/context)": "/api/contexts/Document",
  "[@id](https://github.com/id)": "/api/documents",
  "[@type](https://github.com/type)": "hydra:Collection",
  "hydra:member": [
    {
      "[@id](https://github.com/id)": "/api/documents/1",
      "[@type](https://github.com/type)": "Document",
      "id": 1,
      "newTranslations": [],
      "currentLocale": "cz",
      "defaultLocale": "en",
      "title": "příkladová data",
      "translationEntityClass": "App\\Entity\\DocumentTranslation"
    }
  ]
}

That's it!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui