icodr8/contao-translation-fields

此包已弃用,不再维护。作者建议使用 craffft/translation-fields-bundle 包。

Contao开源CMS的翻译字段

2.0.0 2019-07-12 20:59 UTC

This package is auto-updated.

Last update: 2022-02-01 12:31:29 UTC


README

什么是Translation Fields?

Translation Fields是一个库,供Contao开发者使用,以在Contao开源CMS中获得美观的翻译字段。每个翻译字段都获得一个语言标志,可以通过将标志更改为另一种语言来进行翻译。翻译将保存在表 tl_translation_fields 中,并且该表的一个键将存储在字段自身中。

依赖项

故障排除

直接在github上!请参阅 https://github.com/Craffft/contao-translation-fields/issues

文档

输入类型

您可以在后端使用三种输入类型。

  • TranslationTextField(与输入类型 text 相同)
  • TranslationTextArea(与输入类型 textarea 相同)
  • TranslationInputType(与输入类型 inputType 相同)

如何在DCA中定义字段

要使用翻译字段,您必须在您的DCA代码中进行以下更改。

  • 为您的字段添加索引
  • 更改输入类型
  • 将sql更改为int(10)
  • 将关系添加到您的字段

每个字段都使用不同的设置。您可以在以下代码中看到这一点。

示例

文本字段

原始字段

$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'text',
    'eval'                    => array('maxlength'=>255),
    'sql'                     => "varchar(255) NOT NULL default ''"
);

更改后的字段

$GLOBALS['TL_DCA']['tl_mytable']['config']['sql']['keys']['myfield'] = 'index';
$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'TranslationTextField',
    'eval'                    => array('maxlength'=>255),
    'sql'                     => "int(10) unsigned NOT NULL default '0'",
    'relation'                => array('type'=>'hasOne', 'load'=>'lazy')
);

文本区域字段

原始字段

$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'textarea',
    'eval'                    => array('rte'=>'tinyMCE', 'tl_class'=>'long'),
    'sql'                     => "text NULL"
);

更改后的字段

$GLOBALS['TL_DCA']['tl_mytable']['config']['sql']['keys']['myfield'] = 'index';
$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'TranslationTextArea',
    'eval'                    => array('rte'=>'tinyMCE', 'tl_class'=>'long'),
    'sql'                     => "int(10) unsigned NOT NULL default '0'",
    'relation'                => array('type'=>'hasOne', 'load'=>'lazy')
);

输入单元字段

原始字段

$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'inputUnit',
    'options'                 => array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'),
    'eval'                    => array('maxlength'=>200, 'tl_class'=>'w50'),
    'sql'                     => "blob NULL"
);

更改后的字段

$GLOBALS['TL_DCA']['tl_mytable']['config']['sql']['keys']['myfield'] = 'index';
$GLOBALS['TL_DCA']['tl_mytable']['fields']['myfield'] = array
(
    'label'                   => &$GLOBALS['TL_LANG']['tl_mytable']['myfield'],
    'exclude'                 => true,
    'inputType'               => 'TranslationInputUnit',
    'options'                 => array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'),
    'eval'                    => array('maxlength'=>200, 'tl_class'=>'w50'),
    'sql'                     => "blob NULL",
    'relation'                => array('type'=>'hasOne', 'load'=>'lazy')
);

如何翻译字段值

要翻译当前字段的键,您可以使用以下方法

翻译值

将字段键翻译为当前语言的翻译值。

$intId = '1485'; // Example value

$strTranslated = \TranslationFields::translateValue($intId);

echo $strTranslated; // Returns e.g. "Hi there!"

您可以选择向translateValue方法添加强制语言。

$intId = '1485'; // Example value
$strForceLanguage = 'de';

$strTranslated = \TranslationFields::translateValue($intId, $strForceLanguage);

echo $strTranslated; // Returns e.g. "Hallo zusammen!"

翻译DataContainer对象

将数据容器对象中所有翻译字段值转换为翻译后的值。

$objDC->exampleValue = '1485'; // Example value

$objDC = \TranslationFields::translateDCObject($objDC);

echo $objDC->exampleValue; // Returns e.g. "Hi there!"

翻译DCA

将数据容器数组中所有翻译字段值转换为翻译后的值。

$arrDC['exampleValue'] = '1485'; // Example value

$arrDC = \TranslationFields::translateDCArray($arrDC, $strTable);

echo $arrDC['exampleValue']; // Returns e.g. "Hi there!"

Runonce

如果您的应用程序字段中已有内容,您必须确保翻译字段不会删除您的数据内容。因此,您需要创建一个Runonce,将当前值插入到 tl_translation_fields 表中,并将键与字段关联。

您可以通过以下代码实现

class MyApplicationRunconce extends \Controller
{
    // Code ...

    public function __construct()
    {
        parent::__construct();

        // Code ...

        // Load required translation-fields classes
        \ClassLoader::addNamespace('TranslationFields');
        \ClassLoader::addClass('TranslationFields\Updater', 'system/modules/translation-fields/classes/Updater.php');
        \ClassLoader::addClass('TranslationFields\TranslationFieldsWidgetHelper', 'system/modules/translation-fields/classes/TranslationFieldsWidgetHelper.php');
        \ClassLoader::addClass('TranslationFields\TranslationFieldsModel', 'system/modules/translation-fields/models/TranslationFieldsModel.php');
        \ClassLoader::register();
    }


    public function run()
    {
        // Code ...

        \TranslationFields\Updater::convertTranslationField('tl_my_table_name', 'my_field_name');

        // Code ...
    }

    // Code ...
}

例如,您可以查看我扩展Photoalbums2的runonce.php文件:https://github.com/Craffft/contao-photoalbums2/blob/master/config/runonce.php