简体   繁体   English

TYPO3 Fluid Template 获取语言代码

[英]TYPO3 Fluid Template get language code

Is it possible to get the current language key (or code) in a TYPO3 Fluid template?是否可以在 TYPO3 Fluid 模板中获取当前语言键(或代码)?

In the meantime I've found another solution using a view helper found 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();
    }

}

?>

Which I use in the fluid template as follows.我在流体模板中使用如下。

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

Another solution using TypoScript object in Fluid template:在 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

And Fluid code:和流体代码:

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

Object temp.language can contain any values, of course.当然,对象temp.language可以包含任何值。

You can just assign variable in your action:您可以在您的操作中分配变量:

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

and then read it in your view:然后在您的视图中阅读它:

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

on the other hand it would be easier and more comfortable to assign redy-to-use variable in controller as <f:if ...> block is quite simple and sometimes just uncomfortable:另一方面,在控制器中分配 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);

In order to get the current langage, you can use the Page/LanguageViewHelper included with the VHS extension.为了获取当前语言,您可以使用 VHS 扩展中包含的 Page/LanguageViewHelper。

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

Have a look here : http://fluidtypo3.org/viewhelpers/vhs/1.8.3/Page/LanguageViewHelper.html看看这里:http: //fluidtypo3.org/viewhelpers/vhs/1.8.3/Page/LanguageViewHelper.html

my Solution is this:我的解决方案是这样的:

data = TSFE:sys_language_uid数据 = TSFE:sys_language_uid

(The Output is the 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 {

        }
    }
}

Now u can use the new "variables" in FLUID:现在您可以在 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>

Or you use this for more Lang-Code:或者您将其用于更多语言代码:

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

Another approach via the TYPO3 languageAspect通过 TYPO3 语言方面的另一种方法

Example within an Extbase Extension (eg showAction ) 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());

Example tested in TYPO3 10.4 LTS在 TYPO3 10.4 LTS 中测试的示例

Another option could be to use the v:page.languageMenu ViewHelper from the VHS extension.另一种选择是使用 VHS 扩展中的v:page.languageMenu ViewHelper。 It would allow you to combine with other ViewHelpers and use something like the following in a Fluid template:它将允许您与其他 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>

There is a lot more you can do with the values returned from each of these ViewHelpers - you can for example use v:var.set to define new variables in the template which contain extracted flag names:对于每个 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 -->

In TYPO3 10 and above you could access directly the language uid and use it together with a condition in your Fluid-Template (see here some hints for using Fluid):在 TYPO3 10 及更高版本中,您可以直接访问语言 uid 并将其与 Fluid-Template 中的条件一起使用(请参阅此处使用 Fluid 的一些提示):

<f:if condition="{data.sys_language_uid} = 1">
  <f:then>
         This is language with uid "1"           
  </f:then>
  <f:else>
         This is language with other uid  than "1"        
  </f:else>
</f:if>

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

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