tuxes3/doctrine-util-bundle

简单地将实体在表单中翻译

安装: 15

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

dev-master 2014-08-28 11:38 UTC

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 缓存
  • ...