Git Product home page Git Product logo

Comments (9)

nurtext avatar nurtext commented on June 26, 2024 1

Sure, thanks a lot for now :)

from apiplatformtranslationbundle.

nurtext avatar nurtext commented on June 26, 2024 1

@paullla Awesome, now it works! 👍
For some reason I though the concept was to have the translatable entity to hold the original text in the given base locale and to have the translation entity solely for translations.

from apiplatformtranslationbundle.

paullla avatar paullla commented on June 26, 2024

Hi @nurtext ,

It should be quite simple. Since this bundle is using the same concept as Sylius, you can take a look at Sylius example for adding translations programatically: https://docs.sylius.com/en/1.2/book/architecture/translations.html#how-to-add-a-new-translation-programmatically. It should work here too :) GL

from apiplatformtranslationbundle.

nurtext avatar nurtext commented on June 26, 2024

Hi @paullla,

thanks for your help. Unfortunately this won't work, I tried the following:

/** @var Salutation $salutationEntity */
$salutation = new Salutation();
$salutation->setTitle('Mr.');

/** @var SalutationTranslation $salutationTranslation */
$salutationTranslation = new SalutationTranslation();
$salutationTranslation->setLocale('de');
$salutationTranslation->setTitle('Herr');

$salutation->addTranslation($salutationTranslation);
$this->entityManager->flush($salutation);

Which resulted in: In TranslatableTrait.php line 65: No locale has been set and current locale is undefined.

This are my two classes:

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\SalutationTranslation;
use App\Repository\SalutationRepository;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=SalutationRepository::class)
 */
class Salutation extends AbstractTranslatable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity="SalutationTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     *
     * @Groups({"post_write", "translations"})
     */
    protected $translations;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string|null
     */
    public function getTitle(): ?string
    {
        return $this->getTranslation()->getTitle();
    }

    /**
     * @param string $title
     *
     * @return self
     */
    public function setTitle(string $title): self
    {
        $this->getTranslation()->setTitle($title);

        return $this;
    }

    /**
     * @return TranslationInterface
     */
    protected function createTranslation(): TranslationInterface
    {
        return new SalutationTranslation();
    }
}
<?php

namespace App\Entity;

use App\Entity\Salutation;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\SalutationTranslationRepository;
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;

/**
 * @ORM\Entity(repositoryClass=SalutationTranslationRepository::class)
 */
class SalutationTranslation extends AbstractTranslation
{
    /**
     * @ORM\ManyToOne(targetEntity="Salutation", inversedBy="translations")
     */
    protected $translatable;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     *
     * @Groups({"post_read", "post_write", "translations"})
     */
    private $title;

    /**
     * @ORM\Column(type="string")
     *
     * @Groups({"post_write", "translations"})
     */
    protected $locale;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param string $title
     *
     * @return void
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    /**
     * @return string|null
     */
    public function getTitle(): ?string
    {
        return $this->title;
    }
}

Any ideas what I'm doing wrong here?

Thanks!

from apiplatformtranslationbundle.

paullla avatar paullla commented on June 26, 2024

Current locale isn't set. When creating translation using API, it happens behind the hood. So I think the solution would be to also set current locale in your code: $salutationTranslation->setCurrentLocale('de');

from apiplatformtranslationbundle.

nurtext avatar nurtext commented on June 26, 2024

I tried both setLocale() and setCurrentLocale() on the $salutationTranslation object and both didn't work for me.

from apiplatformtranslationbundle.

paullla avatar paullla commented on June 26, 2024

I don't have any more ideas :D I'll need to reproduce it and debug the problem. I'll try to do it tomorrow and let you know the results.

from apiplatformtranslationbundle.

nurtext avatar nurtext commented on June 26, 2024

Hey @paullla, any news on this one? I tried a few different things on my own, basically creating objects one after another, trying to set different locales using different methods, but none made it work.

from apiplatformtranslationbundle.

paullla avatar paullla commented on June 26, 2024

Hi @nurtext, I found the problem. You mapped $title in Salutation to database, and also called the setter. Fields that are translated need to be mapped to the database in SalutationTranslation only.

First step is to remove @ORM\Column(type="string") from $title in Salutation
Next step is to remove $salutation->setTitle('Mr.');

So the result would be:

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\SalutationTranslation;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;

/**
 * @ApiResource()
 * @ORM\Entity()
 */
class Salutation extends AbstractTranslatable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    private $title;

    /**
     * @ORM\OneToMany(targetEntity="SalutationTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     * @Groups({"post_write", "translations"})
     */
    protected $translations;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string|null
     */
    public function getTitle(): ?string
    {
        return $this->getTranslation()->getTitle();
    }

    /**
     * @param string $title
     *
     * @return self
     */
    public function setTitle(string $title): self
    {
        $this->getTranslation()->setTitle($title);

        return $this;
    }

    /**
     * @return TranslationInterface
     */
    protected function createTranslation(): TranslationInterface
    {
        return new SalutationTranslation();
    }
}

and creating objects:

$salutation = new Salutation();

/** @var SalutationTranslation $salutationTranslation */
$salutationTranslation = new SalutationTranslation();
$salutationTranslation->setLocale('de');
$salutationTranslation->setTitle('Herr');

$salutation->addTranslation($salutationTranslation);
$this->entityManager->persist($salutation);
$this->entityManager->flush();

from apiplatformtranslationbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.