enderlab / translatable-entity-bundle
Translate entity field
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
- ext-intl: *
- scienta/doctrine-json-functions: ^5.2
- symfony/webpack-encore-bundle: ^1.16
This package is auto-updated.
Last update: 2025-02-23 14:19:13 UTC
README
| /!\ Documentation in progress /!\
Table of content
Installation
composer require enderlab/translatable-entity
Configuration
# File config/packages/translatable_entity.yaml translatable_entity: default_locale: en # replace this value by your default locale availables_locales: # replace this value by your availables locales - en - fr default_timezone: Europe\London # replace this value by your default timezone availables_timezones: # replace this value by your availables timezones en: Europe\London fr: Europe\Paris
Usage
Update your entity
In your entity class
- Create your entity
- Declare translatable fields in json
- Extends EnderLab\TranslatableEntityBundle\Entity\TranslatableEntity
- Add attribute #[TranslatableField] on translatable field
- Remove getters and setters for translatable properties
<?php namespace App\Entity; use App\Repository\ProductRepository; use Doctrine\ORM\Mapping as ORM; use EnderLab\TranslatableEntityBundle\Entity\TranslatableEntityInterface; use EnderLab\TranslatableEntityBundle\Traits\TranslatableEntityTrait; use EnderLab\TranslatableEntityBundle\Attributes\TranslatableField; #[ORM\Entity(repositoryClass: ProductRepository::class)] - class Product + class Product implements TranslatableEntityInterface { use TranslatableEntityTrait; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column] + #[TranslatableField] private array $name = []; #[ORM\Column] + #[TranslatableField] private array $description = []; public function getId(): ?int { return $this->id; } - public function getName(): ?string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this - } - - public function getDescription(): ?string - { - return $this->description; - } - - public function setDescription(string $description): self - { - $this->description = $description; - - return $this - } }
To help the autocompletion of your ide, you can add the following comments
# Product class <?php namespace App\Entity; use App\Repository\ProductRepository; use Doctrine\ORM\Mapping as ORM; use EnderLab\TranslatableEntityBundle\Entity\TranslatableEntityInterface; use EnderLab\TranslatableEntityBundle\Traits\TranslatableEntityTrait; use EnderLab\TranslatableEntityBundle\Attributes\TranslatableField; + /** + * @method string getName() + * @method Product setName(?string $name) + * @method string getDescription() + * @method Product setDescription(?string $description) + */ #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product implements TranslatableEntityInterface { use TranslatableEntityTrait; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column] #[TranslatableField] protected array $name = []; #[ORM\Column] #[TranslatableField] protected array $description = []; public function getId(): ?int { return $this->id; } }
Use in your code
# Product class <?php namespace App\Entity; use App\Repository\ProductRepository; use Doctrine\ORM\Mapping as ORM; use EnderLab\TranslatableEntityBundle\Entity\TranslatableEntityInterface; use EnderLab\TranslatableEntityBundle\Traits\TranslatableEntityTrait; use EnderLab\TranslatableEntityBundle\Attributes\TranslatableField; #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product implements TranslatableEntityInterface { use TranslatableEntityTrait; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column] #[TranslatableField] protected array $name = []; #[ORM\Column] #[TranslatableField] protected array $description = []; public function getId(): ?int { return $this->id; } } # Use product object $product = new Product(); $product ->setName('Produit test') # Set value for current locale ->setNameFr('Produit test') # Set value for fr locale ->setNameEn('Test product') # Set value for en locale ->setDescriptionFr('Super produit test') ->setDescriptionEn('Great test product') ; // Display product name with current locale echo $product->getName(); // Display product name with fr locale echo $product->getNameFr(); // Display product name with en locale echo $product->getNameEn(); // Display array of all locales echo print_r($product->getNameAll());
Use in twig template
// Display product name with the current locale <div>{{ product.name }}</div> // Display product name with the fr locale <div>{{ product.nameFr }}</div> Display product name with the en locale <div>{{ product.nameEn }}</div> // Display product name all locales <div>{{ product.nameAll|join(',') }}</div>