elao/form-translation-bundle

为表单生成翻译键

安装数量: 189 349

依赖关系: 1

建议者: 0

安全: 0

星标: 44

关注者: 17

分支: 12

开放问题: 2

类型:symfony-bundle

v4.2.0 2024-03-06 10:14 UTC

This package is auto-updated.

Last update: 2024-09-06 11:35:28 UTC


README

Build Status

描述

此包以“逻辑”方式为表单字段生成翻译键的便捷方式。它主要用于生成字段的自动标签,但也可以用于构建任何键。

例如,在名为“register”的RegisterType表单中,其“name”字段的键将是form.register.children.name.label

另一个更高级的例子是一个“emails”字段,它是一个collection类型的text输入,它将生成以下键:

  • form.register.children.emails.label
  • form.register.children.emails.label_add
  • form.register.children.emails.label_delete
  • form.register.children.emails.children.prototype.label

或者在yml

form:
    register:
        children:
            emails:
                label:          # add your trans for the fieldset ex: Email
                label_add:      # add your trans for add button ex: Add an email
                label_delete:   # add your trans for remove button ex: Remove an email
                children:
                    prototype:
                        label:  # add your trans for the label of one email field ex: Email address

注意:键仅在实际运行时生成,使用translation:update时不会导出(我们正在努力解决这个问题)。

安装

将 ElaoFormTranslationBundle 添加到您的 composer.json 中

{
    "require": {
        "elao/form-translation-bundle": "3.*"
    }
}

现在运行以下命令下载包

$ php composer.phar update elao/form-translation-bundle

在内核中注册包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Elao\Bundle\FormTranslationBundle\ElaoFormTranslationBundle(),
    );
}

如何使用它

为了自动生成翻译键,您有两种选择

按字段生成

如果您将表单字段的“label”选项设置为 true,则会生成一个键并将其设置为字段标签。 否则我们不会生成您的标签!

<?php

class RegisterType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, array(
                'label' => true // Will generate: "form.register.children.name.label"
            ));
            ->add('email', null, array(
                'label' => false // Will NOT generate a `<label>` in the `HTML`
            ));
            ->add('email', null, array(
                'label' => 'my.custom.key' // Default behavior
            ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'register';
    }
}

全局配置键

如果您想为所有标签生成键,可以将选项 auto_generate 设置为 true

elao_form_translation:
    auto_generate: true

这将默认标签值设置为 true,因此将为每个标签生成键。

如果您需要覆盖此行为,您仍然可以为您的字段提供一个 label 键以使用您的翻译键。在这种情况下,不会生成任何键。

自定义和配置

自定义键

键是按照以下模式构建的

[root][separator](parent_field_name)[separator][children][separator](field_name)[key]

您可以自定义(并删除)这些标记中的任何一个,以更改键的构建方式

elao_form_translation:
    blocks:
        # Prefix for children nodes (string|false)
        children:   "children"

        # Prefix for prototype nodes (string|false)
        prototype:  "prototype"

        # Prefix at the root of the key (string|false)
        root:       "form"

        # Separator te be used between nodes (string|false)
        separator:  "."

例如,如果您只需要简单的键,可以使用以下配置

elao_form_translation:
    blocks:
        root:      false
        children:  false
        separator: "_"

这将生成这样的键

# (parent_field_name)[separator](field_name)[separator][key]
register_name_label

为所有表单设置默认翻译域

您可以使用以下配置为所有表单和选择设置默认翻译域

elao_form_translation:
    default_translation_domain: "forms"

这将设置默认的 translation_domainchoice_translation_domain 选项,适用于您应用程序中的所有表单和选择。

您仍然可以在每个表单类型的 configureOptions 方法中覆盖这些选项。

您还可以通过将选项设置为 false 来禁用所有表单的翻译。

elao_form_translation:
    default_translation_domain: false

默认配置

elao_form_translation:

    # Can be disabled
    enabled: true

    # Generate translation keys for all missing labels
    auto_generate: false

    # Set default translation domain on all forms
    default_translation_domain: ~

    # Customize available keys
    keys:
        form:
            label:  "label"
            help:   "help"
            # Add yours ...
        collection:
            label_add:      "label_add"
            label_delete:   "label_delete"
            # Add yours ...

    # Customize the ways keys are built
    blocks:

        # Prefix for prototype nodes
        prototype:  "prototype"

        # Prefix for children nodes
        children:   "children"

        # Prefix at the root of the key
        root:       "form"

        # Separator te be used between nodes
        separator:  "."