简体   繁体   English

TYPO3:如何在扩展名中呈现翻译的内容

[英]TYPO3: How to render translated content in extension

I am developing a TYPO3 6.0 plugin that shows the subpages of the current page as tabs. 我正在开发TYPO3 6.0插件,该插件将当前页面的子页面显示为选项卡。 For example, on the following pages my plugin is inserted on TabRoot : 例如,在以下页面上,我的插件已插入TabRoot

TYPO3页面示例

If TabRoot is requested, the plugin's ActionController looks up the database for the subpage titles and contents and passes all gathered data to a Fluid template. 如果请求TabRoot ,则插件的ActionController在数据库中查找子页面标题和内容,并将所有收集的数据传递到Fluid模板。 The page is then rendered like the following: 然后呈现页面,如下所示:

标签选择器功能

With JS in place I always hide/show content below based on the selection. 使用JS后,我总是根据选择隐藏/显示下面的内容。 My problem is that I want to show the translated content of the subpages based on the current language selection. 我的问题是,我想根据当前的语言选择显示子页面的翻译内容 How am I able to do this? 我该怎么做? I've tried it with several methods, but neither of them was flawless. 我已经尝试了几种方法,但是它们都不完美。 These are the methods I've tried: 这些是我尝试过的方法:


Using RECORDS This method is not affected by the selected language, it always returns the content in the default language: 使用RECORDS此方法不受所选语言的影响,它始终以默认语言返回内容:

//Get the ids of the parts of the page

$select_fields = "uid";
$from_table = "tt_content";
$where_clause = 'pid = ' . $pageId;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
      $select_fields,
      $from_table,
      $where_clause,
      $groupBy='',
      $orderBy='sorting',
      $limit=''
);

$ids = '';
$firstIteration = true;
while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
    if (!$firstIteration) $ids .= ",";
    $ids .= $row ['uid'];

    $firstIteration = false;
}
$GLOBALS['TYPO3_DB']->sql_free_result( $res );

//Render the parts of the page

$conf ['tables'] = 'tt_content';
$conf ['source'] = $ids;
$conf ['dontCheckPid'] = 1;
$content = $this->cObj->cObjGetSingle ( 'RECORDS', $conf );

Using CONTENTS According to TYPO3: How to render localized tt_content in own extension , this is the way to do it, however for me this also returns the content rendered with the default language. 根据TYPO3 使用CONTENTS :如何在自己的扩展名中呈现本地化的tt_content ,这是做到这一点的方法,但是对我来说,这也返回使用默认语言呈现的内容。 It is not affected by a language change. 它不受语言更改的影响。

$conf = array(
    'table' => 'tt_content',
    'select.' => array(
            'pidInList' => $pageId,
            'orderBy'  => 'sorting',
            'languageField' => 'sys_language_uid'
    )
);
$content = $this->cObj->cObjGetSingle ( 'CONTENT', $conf );

Using VHS: Fluid ViewHelpers I installed the vhs extension and tried to render the content with <v:content.render /> . 使用VHS:Fluid ViewHelpers我安装了vhs扩展并尝试使用<v:content.render />呈现内容。 The result is the same as with CONTENTS ; 结果与CONTENTS相同; it only works with the default language. 它仅适用于默认语言。

{namespace v=Tx_Vhs_ViewHelpers}

...

<v:content.render column="0" order="'sorting'" sortDirection="'ASC'"
        pageUid="{pageId}" render="1" hideUntranslated="1" />

Using my own SQL query I've tried to get the bodytext fields of the page and then render those with \\TYPO3\\CMS\\Frontend\\Plugin\\AbstractPlugin::pi_RTEcssText() . 使用我自己的SQL查询,我试图获取页面的bodytext字段,然后使用\\TYPO3\\CMS\\Frontend\\Plugin\\AbstractPlugin::pi_RTEcssText()呈现它们。 This method returns the content based on the current language, however the problem is that bodytext 's do not contain the complete content (images, other plugins, etc). 此方法基于当前语言返回内容,但是问题是bodytext并不包含完整的内容(图像,其他插件等)。

$select_fields = "bodytext";
$from_table = "tt_content";
$where_clause = 'pid = ' . $pageId
    . ' AND sys_language_uid = ' . $GLOBALS ['TSFE']->sys_language_uid;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
      $select_fields,
      $from_table,
      $where_clause,
      $groupBy='',
      $orderBy='sorting',
      $limit=''
);

$content = '';
while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
   $content .= 
   \TYPO3\CMS\Frontend\Plugin\AbstractPlugin::pi_RTEcssText( $row ['bodytext'] );
}
$GLOBALS['TYPO3_DB']->sql_free_result( $res );

What am I missing? 我想念什么? Why isn't the content rendered with the current language in the case of the CONTENTS method? 在使用CONTENTS方法的情况下,为什么不使用当前语言呈现CONTENTS

In TYPO3 4.x you could use the following methods to load the translated record: 在TYPO3 4.x中,您可以使用以下方法加载翻译后的记录:

  • t3lib_pageSelect->getRecordOverlay
  • t3lib_pageSelect->getPageOverlay

They are also available at $GLOBALS['TSFE']->sys_page->getRecordOverlay() . 它们也可以在$GLOBALS['TSFE']->sys_page->getRecordOverlay()

Easiest way is to use the cObject viewhelper to render right from TypoScript . 最简单的方法是使用cObject viewhelper从TypoScript进行渲染。

And inside your TypoScript template provide the configuration: TypoScript模板内部提供配置:

lib.myContent = CONTENT
lib.myContent {
  ...
}

BTW, you are bypassing the TYPO3 CMS API. 顺便说一句,您正在绕过TYPO3 CMS API。 Please do not do so. 请不要这样做。 Always use the API methods to query for data. 始终使用API​​方法来查询数据。 eg \\TYPO3\\CMS\\core\\Database\\DatabaseConnection is always available at GLOBALS['TYPO3_DB']-> . 例如\\TYPO3\\CMS\\core\\Database\\DatabaseConnection始终在GLOBALS['TYPO3_DB']->可用。 Do not use the the mysql function. 不要使用mysql函数。

On top of that, I believe that you can archive whatever you are trying to do with pure TypoScript , without the need to program anything. 最重要的是,我相信您可以存档使用纯TypoScript ,而无需进行任何编程。 Feel free to ask a new questions to get help on this. 随时提出新问题以获取帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM