12

是否可以在 TYPO3 Fluid 模板中获取当前语言键(或代码)?

同时,我使用此处找到的视图助手找到了另一个解决方案:

<?php
class Tx_AboUnitReservation_ViewHelpers_LanguageViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

    /**
     * Get the current language
     */
    protected function getLanguage() {
        if (TYPO3_MODE === 'FE') {
            if (isset($GLOBALS['TSFE']->config['config']['language'])) {
                return $GLOBALS['TSFE']->config['config']['language'];
            }
        } elseif (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
            return $GLOBALS['BE_USER']->uc['lang'];
        }
        return 'en'; //default
    }

    /**
     * Return current language
     * @return  string
     */
    public function render() {
        return $this->getLanguage();
    }

}

?>

我在流体模板中使用如下。

<f:alias map="{isGerman: 'de'}">
    <f:if condition="{aboUnitReservation:language()} == {isGerman}">
        <script type="text/javascript" src="{f:uri.resource(path:'js/jquery.ui.datepicker-de-CH.js')}"></script>
    </f:if>
</f:alias>
4

6 回答 6

14

在 Fluid 模板中使用 TypoScript 对象的另一种解决方案:

# German language
temp.language = TEXT
temp.language.value = at

# English language
[globalVar = GP:L = 1]
  temp.language.value = en
[global]

lib.language < temp.language

和流体代码:

<f:if condition="{f:cObject(typoscriptObjectPath: 'lib.language')} == 'at'">
    <f:then>
        ...
    </f:then>
    <f:else>
        ...
    </f:else>
</f:if>

当然,对象temp.language可以包含任何值。

于 2016-01-12T13:44:32.930 回答
13

您可以在您的操作中分配变量:

$this->view->assign("sysLanguageUid", $GLOBALS['TSFE']->sys_language_uid);

然后在您的视图中阅读它:

<f:if condition="{sysLanguageUid} == 0">
    You're reading English version of page
</f:if>

另一方面,在控制器中分配 redy-to-use 变量会更容易和更舒适,因为<f:if ...>块非常简单,有时只是不舒服:

switch ($GLOBALS['TSFE']->sys_language_uid) {
    case 1:
        $msg = "Bienvenidos";
        break;
    case 2:
        $msg = "Willkommen";
        break;
    default:
        $msg = "Welcome";
        break;
}

$this->view->assign("myMessage", $msg);
于 2012-05-04T10:34:23.207 回答
8

为了获取当前语言,您可以使用 VHS 扩展中包含的 Page/LanguageViewHelper。

{v:page.language(languages: 'en,fr', pageUid: '0', normalWhenNoLanguage: 'en')}

看看这里: http: //fluidtypo3.org/viewhelpers/vhs/1.8.3/Page/LanguageViewHelper.html

于 2014-03-27T11:35:06.713 回答
5

我的解决方案是这样的:

数据 = TSFE:sys_language_uid

(输出是语言 UID)

page = PAGE
page {

    ## Fluid-Template ##
    10 = FLUIDTEMPLATE
    10 {
        ## Variablen ##
        variables {
            pageTitle = TEXT
            pageTitle.data = page:title
            siteTitle = TEXT
            siteTitle.data = TSFE:tmpl|setup|sitetitle
            rootPage = TEXT
            rootPage.data = leveluid:0
            baseurl = TEXT
            baseurl.value = {$config.domain}
            pageLanguage = TEXT
            pageLanguage.data = TSFE:sys_language_uid
        }

        ## Settings ##
        settings {

        }
    }
}

现在您可以在 FLUID 中使用新的“变量”:

<f:if condition="{pageLanguage}==0">
    <f:then><a href="http://domain.cc/" title="">DE</a></f:then>
    <f:else><a href="http://domain.cc/en/" title="">EN</a></f:else>
</f:if>

或者您将其用于更多语言代码:

<f:if condition="{pageLanguage}==0">
    <f:then>Do this</f:then>
    <f:else if="{pageLanguage}==1">
        Do this instead if variable two evals true
    </f:else>
    <f:else if="{pageLanguage}==2">
        Or do this if variable three evals true
    </f:else>
    <f:else>
        Or do this if nothing above is true
    </f:else>
</f:if>
于 2018-07-24T14:13:17.800 回答
2

通过 TYPO3 语言方面的另一种方法

Extbase 扩展中的示例(例如showAction

# instantiate TYPO3 Context and get language aspect
$languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');

# now assign any property of the actual language to the view. In this example: the language uid.
$this->view->assign('actualLanguageId', $languageAspect->getId());

在 TYPO3 10.4 LTS 中测试的示例

于 2021-08-05T11:44:26.850 回答
1

另一种选择是使用v:page.languageMenuVHS 扩展中的 ViewHelper。它将允许您与其他 ViewHelper 组合并在 Fluid 模板中使用类似以下内容:

{namespace v=Tx_Vhs_ViewHelpers}
<v:page.languageMenu as="languages">

    <!-- gets the current language key -->
    {languages -> v:iterator.filter(propertyName: 'current', filter: 1)}

    <!-- iterates over flag names of all languages which apply to the current page -->
    <f:for each="{languages -> v:iterator.extract(key: 'flag')}" as="languageFlagName">

        <!-- example suggestion: image -->
        <f:image src="{f:uri.resources(path: 'Images/Flags/{languageFlagName}.png')}" />

        <!-- example suggestion: label read from LLL:EXT:myext/Resources/Private/Language/locallang.xml:languages.$languageFlagName -->
        <f:translate key="languages.{languageFlagName}" default="{languageFlagName} />

    </f:for>

</v:page.languageMenu>

您可以使用从每个 ViewHelpers 返回的值做更多事情 - 例如,您可以使用v:var.set在模板中定义包含提取的标志名称的新变量:

<!-- Using {languages} inside <v:page.languageMenu> like above -->
{languages -> v:iterator.filter(propertyName: 'current', filter: 1) -> v:var.set(name: 'currentLanguage')}
<!-- variable {currentLanguage} now contains an array of values describing the current language -->
于 2013-11-13T01:10:56.947 回答