TYPO3流体模板获取语言代码

时间:2012-05-04 09:44:30

标签: typo3 fluid

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

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

<?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>

6 个答案:

答案 0 :(得分:12)

您可以在动作中指定变量:

$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);

答案 1 :(得分:11)

在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当然可以包含任何值。

答案 2 :(得分:6)

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

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

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

答案 3 :(得分:3)

我的解决方法是:

数据= 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>

答案 4 :(得分:1)

另一种选择可能是使用VHS扩展中的v:page.languageMenu ViewHelper。它允许您与其他ViewHelpers结合使用,并在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>

对于从每个ViewHelper返回的值,你可以做更多 lot - 你可以使用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 -->

答案 5 :(得分:0)

通过TYPO3 languageAspect的另一种方法

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 中测试的示例