janwebdev/translatable-entity-bundle
Composer 安装命令:
composer require janwebdev/translatable-entity-bundle
包简介
Make Entity translatable
README 文档
README
A modern Symfony bundle for creating translatable entities with support for PHP 8.2+ and Symfony 6.4/7.x.
Features
- 🚀 Modern PHP 8.2+ - Full type safety with typed properties and return types
- ⚡ Performance Optimized - Reflection caching and efficient translation lookups
- 🎯 Symfony 7.x Ready - Uses modern Symfony attributes and autowiring
- 🔄 Flexible Fallback System - Configurable translation fallback mechanisms
- 📦 Easy Integration - Simple setup with Doctrine ORM
Table of Contents
- Requirements
- Installation
- Enable the Bundle
- Creating Translatable Entities
- Usage in Controllers
- Repository Examples
- Form Integration
- Customizing Translation Behavior
- Testing
Requirements
- PHP: 8.2 or higher
- Symfony: 6.4 or higher
- Doctrine ORM: 2.19 or 3.x
Installation
Install the bundle via Composer:
composer require janwebdev/translatable-entity-bundle
Enable the Bundle
The bundle should be automatically registered. Verify in config/bundles.php:
<?php // config/bundles.php return [ // ... Janwebdev\TranslatableEntityBundle\TranslatableEntityBundle::class => ['all' => true], ];
Creating Translatable Entities
Step 1: Create the Main Entity
<?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Janwebdev\TranslatableEntityBundle\Model\TranslatableWrapper; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article extends TranslatableWrapper { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private ?int $id = null; /** * @var Collection<int, ArticleTranslation> */ #[ORM\OneToMany( targetEntity: ArticleTranslation::class, mappedBy: 'translatable', cascade: ['persist', 'remove'], orphanRemoval: true )] private Collection $translations; public function __construct() { $this->translations = new ArrayCollection(); } public function getId(): ?int { return $this->id; } /** * @return Collection<int, ArticleTranslation> */ public function getTranslations(): Collection { return $this->translations; } public function addTranslation(ArticleTranslation $translation): self { if (!$this->translations->contains($translation)) { $this->translations->add($translation); $translation->setTranslatable($this); } return $this; } public function removeTranslation(ArticleTranslation $translation): self { if ($this->translations->removeElement($translation)) { if ($translation->getTranslatable() === $this) { $translation->setTranslatable(null); } } return $this; } }
Step 2: Create the Translation Entity
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Janwebdev\TranslatableEntityBundle\Model\TranslatingInterface; #[ORM\Entity] #[ORM\Table(name: 'article_translation')] #[ORM\UniqueConstraint(name: 'article_locale_unique', columns: ['article_id', 'locale'])] class ArticleTranslation implements TranslatingInterface { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private ?int $id = null; #[ORM\Column(type: 'string', length: 10)] private string $locale; #[ORM\ManyToOne(targetEntity: Article::class, inversedBy: 'translations')] #[ORM\JoinColumn(name: 'article_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] private ?Article $translatable = null; #[ORM\Column(type: 'string', length: 255)] private string $title; #[ORM\Column(type: 'text', nullable: true)] private ?string $content = null; public function getId(): ?int { return $this->id; } public function setLocale(string $locale): void { $this->locale = $locale; } public function getLocale(): string { return $this->locale; } public function setTranslatable(Article $translatable): void { $this->translatable = $translatable; } public function getTranslatable(): ?Article { return $this->translatable; } public function getTitle(): string { return $this->title; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function getContent(): ?string { return $this->content; } public function setContent(?string $content): self { $this->content = $content; return $this; } }
Usage in Controllers
<?php namespace App\Controller; use App\Entity\Article; use App\Repository\ArticleRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class ArticleController extends AbstractController { #[Route('/article/{id}', name: 'article_show')] public function show(Article $article): Response { // The bundle automatically sets the locale from the request // Access translated properties through magic methods $title = $article->title; // or $article->getTitle() $content = $article->content; // or $article->getContent() return $this->render('article/show.html.twig', [ 'article' => $article, 'title' => $title, 'content' => $content, ]); } }
Repository Examples
Always join translations for better performance:
<?php namespace App\Repository; use App\Entity\Article; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; class ArticleRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Article::class); } /** * Get all articles with translations joined */ public function findAllWithTranslations(): array { return $this->createQueryBuilderWithTranslations() ->getQuery() ->getResult(); } /** * Find one article by ID with translations */ public function findOneWithTranslations(int $id): ?Article { return $this->createQueryBuilderWithTranslations() ->andWhere('a.id = :id') ->setParameter('id', $id) ->getQuery() ->getOneOrNullResult(); } /** * Create query builder with translations joined */ private function createQueryBuilderWithTranslations(): QueryBuilder { return $this->createQueryBuilder('a') ->leftJoin('a.translations', 'at') ->addSelect('at'); } }
Form Integration
Translation Form Type
<?php namespace App\Form; use App\Entity\ArticleTranslation; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class ArticleTranslationType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('locale', HiddenType::class) ->add('title', TextType::class, [ 'label' => 'Title', 'required' => true, ]) ->add('content', TextareaType::class, [ 'label' => 'Content', 'required' => false, ]); } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => ArticleTranslation::class, ]); } }
Main Entity Form Type
<?php namespace App\Form; use App\Entity\Article; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class ArticleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('translations', CollectionType::class, [ 'entry_type' => ArticleTranslationType::class, 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'label' => 'Translations', ]); } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => Article::class, ]); } }
Customizing Translation Behavior
Override these methods in your translatable entity to customize behavior:
<?php namespace App\Entity; use Janwebdev\TranslatableEntityBundle\Model\TranslatableWrapper; class Article extends TranslatableWrapper { // ... your code ... /** * Return true to use the first available translation as fallback * when translation for current locale is not found */ protected function acceptFirstTranslationAsDefault(): bool { return true; // Default: false } /** * Return false to NOT use default locale translation as fallback */ protected function acceptDefaultLocaleTranslationAsDefault(): bool { return true; // Default: true } /** * Handle the case when no translation is found * Override to provide custom behavior (e.g., return empty translation) */ protected function handleTranslationNotFound(): never { // Create an empty translation instead of throwing exception $class = ArticleTranslation::class; $this->translation = new $class(); $this->translation->setLocale($this->locale->getLocale()); return $this->translation; } }
Fallback Priority
The bundle uses this fallback mechanism:
- Exact match - Translation for current locale
- Default locale - Translation for default locale (if
acceptDefaultLocaleTranslationAsDefault()returns true) - First translation - First available translation (if
acceptFirstTranslationAsDefault()returns true) - Handle not found - Calls
handleTranslationNotFound()(throws exception by default)
Testing
Run the test suite:
composer install vendor/bin/phpunit
Run code quality checks:
vendor/bin/grumphp run
Run static analysis:
vendor/bin/phpstan analyse
Changelog
Please see CHANGELOG for more information on recent changes.
License
The MIT License (MIT). Please see License File for more information.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
janwebdev/translatable-entity-bundle 适用场景与选型建议
janwebdev/translatable-entity-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 250 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「i18n」 「translation」 「bundle」 「translatable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 janwebdev/translatable-entity-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 janwebdev/translatable-entity-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 janwebdev/translatable-entity-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
The bundle for easy using json-rpc api on your project
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Nette Framework adapter for I18n package
Yii2 integration for AirBnB Polyglot.js
统计信息
- 总下载量: 250
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-06