craffft/contao-translation-fields

此软件包已被废弃,不再维护。未建议替代包。

Contao开源CMS的翻译字段

2.0.0 2019-07-12 20:59 UTC

This package is auto-updated.

Last update: 2020-06-12 22:54:03 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!"

一次性运行

如果您已经在应用程序字段中有了内容,您必须确保翻译字段不会删除您的数据内容。因此,您必须创建一个一次性运行程序,该程序将当前值插入到 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