barthy-koeln / cached-prezent-translation
提供与 prezent/translatable-bundle 一起使用的特性
1.0.1
2022-08-24 12:17 UTC
Requires
- php: ^7.4|^8.1
- prezent/doctrine-translatable-bundle: ^1.2
Requires (Dev)
- barthy-koeln/beautify-specify: ^1.0
- brainmaestro/composer-git-hooks: ^2.8
- codeception/assert-throws: ^1.1
- friendsofphp/php-cs-fixer: ^2.16
- phpmd/phpmd: ^2.9
- phpunit/phpunit: ^9.4
- squizlabs/php_codesniffer: ^3.5
README
此库提供了一个简单的特性,可用于与 prezent/doctrine-translatable-bundle
一起使用。
这主要是从 prezent/doctrine-translatable
关于代理获取器和设置器的文档 中复制粘贴的 php 特性,并针对 php >= 7.4 和有见地的代码风格进行了适配。
该特性存储当前区域设置、备用区域设置,并缓存最近获取的翻译。
通常,应用程序只需要一个翻译:当前区域设置的翻译或备用翻译。由于 prezent/translatable-bundle
使用 FETCH_EXTRA_LAZY
,如果从对象多次查询,则缓存的翻译不会触发任何额外的直接 SELECT 语句。
在需要多个翻译的情况下(即必须为应用程序加载多个翻译,最好手动完全初始化集合或自己处理缓存。
安装
composer require barthy-koeln/cached-prezent-translation
使用方法
实体
<?php use BarthyKoeln\CachedPrezentTranslation\CachedPrezentTranslationTrait; use Doctrine\Common\Collections\ArrayCollection; use Prezent\Doctrine\Translatable\Entity\AbstractTranslatable; class TranslatedEntity extends AbstractTranslatable { use CachedPrezentTranslationTrait; /** * @Assert\Valid() * @Prezent\Translations(targetEntity="App\Entity\TranslatedEntityTranslation") * @var ArrayCollection */ protected $translations; public function __construct() { $this->translations = new ArrayCollection(); } public function getTitle(?string $locale = null): string { return $this->translate($locale)->getTitle(); } }