baconmanager / translation-form-bundle
这是 symfony2 的一个包
dev-master
2016-02-06 21:24 UTC
This package is not auto-updated.
Last update: 2024-09-14 18:19:27 UTC
README
此包旨在改变 A2lixFormBundle 的行为,该包用于渲染多语言表单
新增功能
- 提供者,用于返回特定实体的语言
- 用于实现仓库和实体的接口
- twig 函数,用于在表单选项卡中渲染语言名称
- 事件监听器,用于保存添加内容的语言记录
- 配置包含语言的实体
安装
在 composer 中添加 A2lixFormBundle 的安装
composer require a2lix/translation-form-bundle
在文件 app/AppKernel.php 中添加以下行
# app/AppKernel.php public function registerBundles() { $bundles = array( // ... new A2lix\TranslationFormBundle\A2lixTranslationFormBundle(), new A2C\Bundle\TranslationFormBundle\A2CTranslationFormBundle(), new Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle(), // ... ); }
配置
app/config/config.yml 的配置
# Translate Form a2lix a2lix_translation_form: locale_provider: locale_doctrine_provider locales: [en_US] default_locale: en_US manager_registry: doctrine templating: "BaconTranslationFormBundle::default.html.twig" #Bacon TranslationForm bacon_translation_form: class_language_provider: Bacon\Bundle\LanguageBundle\Entity\Language
使用方法
要使用,您必须实现两个可用的接口
- Bacon\Bundle\TranslationFormBundle\Locale\EntityInterface.php
- Bacon\Bundle\TranslationFormBundle\Locale\RepositoryInterface.php
代码应该如下所示
# src/AppBundle/Entity/Language.php <?php namespace AppBundle\Entity; use A2C\Bundle\CoreBundle\Entity\BaseEntity; use A2C\Bundle\TranslationFormBundle\Locale\EntityInterface; use Doctrine\ORM\Mapping as ORM; /** * Class Language * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\LanguageRepository") * @ORM\Table(name="language") */ class Language extends BaseEntity implements EntityInterface { // -------- /** * @ORM\Column(name="acron",type="string",length=2,nullable=false) */ private $acron; /** * @ORM\Column(name="locale",type="string",length=5,nullable=false) */ private $locale; public function getAcron() { return $this->acron; } public function getLocale() { return $this->locale; } // -------- }
# src/AppBundle/Entity/Repository/LanguageRepository.php <?php namespace AppBundle\Entity\Repository; use Bacon\Bundle\TranslationFormBundle\Locale\RepositoryInterface; use Doctrine\ORM\EntityRepository; class LanguageRepository extends EntityRepository implements RepositoryInterface { // -------- /** * @return array */ public function getAllLocale() { $queryBuilder = $this->createQueryBuilder('l'); $queryBuilder->orderBy('l.orderBy','DESC'); $query = $queryBuilder->getQuery(); $query->useResultCache(true,60,md5('bacon_cache_locale_provider')); return $query->getResult(); } // -------- }