Comments (9)
Sure, thanks a lot for now :)
from apiplatformtranslationbundle.
@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.
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.
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.
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.
I tried both setLocale()
and setCurrentLocale()
on the $salutationTranslation
object and both didn't work for me.
from apiplatformtranslationbundle.
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.
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.
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)
- PUT requests not updating translations, they always INSERT and DELETE all of them HOT 5
- Uncaught Error: Return value of Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable::getTranslations() must implement interface Doctrine\Common\Collections\Collection, null returned HOT 1
- Can't force certain locale HOT 1
- Translations create not working as expected when GraphQl is used HOT 1
- best practice to prevent the user from translating the same word in the same language
- inconsistency of returning types HOT 2
- Error on loading AliceBundleFixtures HOT 1
- The Symfony validator don't with this bundle HOT 1
- validation of the fields of my entity with PUT REQUEST HOT 2
- Loader Load Exception HOT 1
- OrderFilter on translated fields and a certain locale HOT 2
- editing object after increment 'Translation' id
- Automatically add translation when it does not exist HOT 4
- Force length 2 for locales HOT 5
- Filter on current locale translations
- support api-platform/core^3.0@rc HOT 1
- unique constraint " name + locale " HOT 2
- Usage question about Api Platforms json-response HOT 1
- New release? HOT 2
- Doctrine/orm 3 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apiplatformtranslationbundle.