tuxes3 / doctrine-util-bundle
简单地将实体在表单中翻译
dev-master
2014-08-28 11:38 UTC
Requires
- php: >=5.3.2
- doctrine/orm: ~2.2,>=2.2.3
- symfony/form: >=2.3
- symfony/framework-bundle: >=2.3
- symfony/http-foundation: >=2.3
This package is auto-updated.
Last update: 2024-09-14 18:42:02 UTC
README
简单地将实体在表单中翻译。
安装:将以下内容添加到你的 composer.json 中 https://packagist.org.cn/packages/tuxes3/doctrine-util-bundle
在 AppKernel.php 中添加到你的项目中
...
new TX3\DoctrineUtilBundle\Tuxes3DoctrineUtilBundle(),
...
标注你的实体
...
use TX3\DoctrineUtilBundle\Annotations as TX3;
...
/**
* @var string
* @TX3\Translatable()
*/
protected $name;
/**
* @TX3\Translatable(true)
*/
protected $translations = array();
...
在 FormType 中使用它
...
$builder->add('translations', 'translation', array(
'label' => 'Tag Name',
));
...
根据 config.yml 中定义的语言渲染表单,每个定义的语言都有一个输入框
tuxes3_doctrine_util:
locales: [de,en]
通过翻译字段搜索
...
$repo = $this->em->getRepository("XXXXBundle:Tag");
$translationRepo = $this->em->getRepository("Tuxes3DoctrineUtilBundle:Translation");
$tag = $repo->find($translationRepo->id('Tag', 'name', $name)); // 'Tag' ===> Classname of entity, 'name' ===> fieldname
...
在代码中保存翻译
$tag = new Tag();
$tag->setName($name);
$this->em->persist($tag);
$this->em->flush(); // <-- flush is important as instead the id is not set... :/
$translation = new Translation();
$translation->setObjid($tag->getId());
$translation->setField('name');
$translation->setClass('Tag');
$translation->setContent($name);
$translation->setLocale('en');
$this->em->persist($translation);
待办事项
- 按语言搜索
- 表单渲染中的更多选项
- 使用 Doctrine 缓存
- ...