1. Go to this page and download the library: Download tmi/translation-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Tmi\TranslationBundle\Doctrine\Model\TranslatableInterface;
use Tmi\TranslationBundle\Doctrine\Model\TranslatableTrait;
#[ORM\Entity]
class Product implements TranslatableInterface
{
use TranslatableTrait;
#[ORM\Column]
private string $name;
// ... your other fields
}
// Translate only (caller must persist)
$translatedEntity = $entityTranslator->translate($entity, 'de_DE');
// Translate and persist in one call (v2.1)
$translatedEntity = $entityTranslator->translateAndPersist($entity, 'de_DE');
// Find existing translation or create + persist a new one (v2.1)
$translatedEntity = $entityTranslator->getOrTranslate($entity, 'de_DE');
#[ORM\ManyToOne(targetEntity: Media::class)]
#[SharedAmongstTranslations]
private Media $video; // Shared across all translations
use Tmi\TranslationBundle\Doctrine\Repository\TranslatableEntityRepository;
/** @extends TranslatableEntityRepository<Product> */
class ProductRepository extends TranslatableEntityRepository
{
}
use Doctrine\ORM\EntityRepository;
use Tmi\TranslationBundle\Doctrine\Repository\TranslatableRepositoryTrait;
class ProductRepository extends EntityRepository
{
use TranslatableRepositoryTrait;
}
// All locale variants for a single Tuuid, keyed by locale
$variants = $repository->findAllLocaleVariants($product->getTuuid());
// ['en_US' => Product, 'de_DE' => Product, ...]
// Batch lookup for multiple Tuuids
$batch = $repository->findAllLocaleVariantsBatch([$tuuid1, $tuuid2]);
// ['<tuuid1>' => ['en_US' => Product, ...], '<tuuid2>' => [...]]