enderlab / translatable-entity-bundle
翻译实体字段
dev-main
2023-01-23 09:54 UTC
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: 2024-09-23 13:32:18 UTC
README
| /!\ 文档正在制作中 /!\
目录
安装
composer require enderlab/translatable-entity
配置
# 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
使用
更新实体
在实体类中
- 创建实体
- 在json中声明可翻译字段
- 扩展 EnderLab\TranslatableEntityBundle\Entity\TranslatableEntity
- 在可翻译字段上添加属性 #[TranslatableField]
- 删除可翻译属性的getter和setter
<?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 - } }
为了帮助你的ide自动完成,你可以添加以下注释
# 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; } }
在代码中使用
# 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());
在twig模板中使用
// 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>