connectsb/translationbundle

管理特定实体相关联的翻译的包

v1.0.1 2015-01-31 19:19 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:50:16 UTC


README

使用此Bundle,您可以在Symfony2中编辑定义的翻译。编辑后的翻译将存储在您的数据库中的两个实体中。

1) 安装

首先,您必须将以下行添加到您的 composer.json 文件中

{
    "require": {
        "connectsb/translationbundle": "dev-master"
    }
}

您还必须将 TranslationBundle 添加到您的 AppKernel.php 中

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new ConnectSB\TranslationBundle\ConnectSBTranslationBundle()
        );
    }
}

2) 使用

config.yml 中,您必须设置以下参数

connect_sb_translation:
    database_translations_domain:   # name of the translation file (can't be messages)
    database_translations_entity:   # name of the entity for the translations

此Bundle依赖于两个实体,BaseTranslationKey 和 BaseTranslationValue。键实体包含翻译的键,而 BaseTranslationValue 包含键的实际值以及区域。

您应该扩展这两个实体,因为您将需要在翻译和您的实体之间定义关系。

扩展 BaseTranslationKey 类的示例类如下

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class TranslationKey extends BaseTranslationKey
{
    /**
     * @ORM\OneToMany(targetEntity="TranslationValue", mappedBy="translationKey", cascade={"persist"})
     */
    protected $translationValues;

    /**
     * Define connection with your entity here
     *
     * @ORM\ManyToOne(targetEntity="ExampleEntity", inversedBy="translationKeys", cascade={"persist"})
     * @ORM\JoinColumn(name="example_entity_id", referencedColumnName="id")
     */
    private $exampleEntity;
}

其次,您应该扩展 BaseTranslationValue 类,此类的示例可能如下所示

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class TranslationValue extends BaseTranslationValue
{
    /**
     * @ORM\ManyToOne(targetEntity="TranslationKey", inversedBy="translationValues", cascade={"persist"})
     * @ORM\JoinColumn(name="translation_key_id", referencedColumnName="id")
     */
    protected $translationKey;
}

这两个实体都需要 getter 和 setter。Doctrine 可以为您创建这些 getter 和 setter。您应该在项目根目录处执行以下命令: php app/console doctrine:generate:entities [您的文件夹名称],其中 [您的文件夹名称] 应该是 Bundle 的顶级文件夹。

3) 表单

此Bundle包含多个表单。可以使用这些表单来编辑翻译。以下是一个示例

public function editTranslations(Request $request, ExampleEntity $exampleEntity)
{
    // Set the entity ID in the request so the bundle can read it
    $request->request->set('entityId', $exampleEntity->getId());

    /** TranslationKey[] $translations */
    $translations = $this->get('connect_sb_database_translation_service')->getTranslationsCollection();

    // Set the translationKeys (this is necessary in order to build the form)
    $exampleEntity->setTranslationKeys($translations);

    $form = $this->createForm(
        new TranslationsCollectionType($this->get('connect_sb_database_translation_service')), $exampleEntity
    );
    
    $form->handleRequest($request);

    if ($form->isValid()) {
        // Get the (edited) translationKeys from the form
        $translationKeys = $form->getData()->getTranslationKeys();

        /** TranslationKey[] $modifiedTranslationKeys */
        $modifiedTranslationKeys = $this->get('connect_sb_database_translation_service')->getModifiedTranslations($translationKeys);
        $exampleEntity->setTranslationKeys($modifiedTranslationKeys);
      
        // persist and flush
    }
      // return and render the form
}