简体   繁体   English

TYPO3:本地化条件,流体模板中的sys_language_uid

[英]TYPO3: localization condition, sys_language_uid in fluid-template

What I would like to achieve is that an image changes depending on what language is selected at that moment. 我想实现的是,图像会根据当时选择的语言而变化。

This is my HTML 这是我的HTML

<f:if condition="{TSFE:sys_language_uid} == 1">
   <f:then>
     <f:link.page pageUid="{settings.rootpid}" class="navbar-brand">
       <img src="fileadmin/branding/brand/images/png/image1.png" alt="Logo {settings.brandname}" />
     </f:link.page>
    </f:then>
    <f:else>
      <f:link.page pageUid="{settings.rootpid}" class="navbar-brand">
        <img src="fileadmin/branding/brand/images/png/image2.png" alt="Logo {settings.brandname}" />
      </f:link.page>
    </f:else>
</f:if>

And this is my TS 这是我的TS

[globalVar = GP:L = 1]
   config {
      sys_language_uid = 1
      language = nl
      locale_all = nl_NL.UTF-8
      htmlTag_setParams = lang="nl" dir="ltr" class="no-js"
   }
[global]

[globalVar = GP:L = 2]
   config {
      sys_language_uid = 2
      language = fr
      locale_all = fr_FR.UTF-8
      htmlTag_setParams = lang="fr" dir="ltr" class="no-js"
     }  

[global]

I tried a ton of different ways of writing this but can't seem to make it work hopefully somebody can help. 我尝试了很多不同的编写方式,但似乎无法使其发挥作用,希望有人能提供帮助。

Pass the pre-evaluated boolean to the template. 将预先评估的布尔值传递给模板。 Either from your controller (there you have access to the TSFE) or via TS to the FLUIDTEMPLATE object. 从您的控制器(您可以访问TSFE)或通过TS到FLUIDTEMPLATE对象。 It's not clear from the question where you are coming from. 从这个问题还不清楚你来自哪里。 You should move the condition inline into the src , this way you don't duplicate the whole markup, but only switch the value. 您应该将条件内联移动到src ,这样就不会复制整个标记,而只需要切换值即可。

Alternatively, you can pre-calculate the src value in the controller or TS and just pass it to the view. 或者,您可以在控制器或TS中预先计算src值,然后将其传递给视图。

I assume that you are using your own distribution, or extend the functionality of some package ... 我假设您正在使用自己的发行版,或扩展了某些软件包的功能...

Try this in your constants.ts (so they are available in the BE constant editor) myext/Configuration/TypoScript/constants.ts : 在您的constants.ts中尝试此操作(以便它们在BE常量编辑器中可用) myext/Configuration/TypoScript/constants.ts

myext.configuration {
    logo {
        src {
            # cat=myext/general/05; type=string; label=English Logo
            default = fileadmin/branding/brand/images/png/image0.png
            # cat=myext/general/06; type=string; label=Dutch Logo
            nl = fileadmin/branding/brand/images/png/image1.png
            # cat=myext/general/07; type=string; label=French Logo
            fr = fileadmin/branding/brand/images/png/image2.png
        }
    }
}

then this in your setup.ts myext/Configuration/TypoScript/setup.ts : 然后在setup.ts myext/Configuration/TypoScript/setup.ts

page = PAGE
page {
    # Page Main template
    10 = FLUIDTEMPLATE
    10 {
        variables { 

            # Logo
            logoSrc = TEXT
            logoSrc.value = {$myext.configuration.logo.src.default}
        }
    }
}

[globalVar = GP:L=1]
    page.10.variables.logoSrc.value = {$myext.configuration.logo.src.nl}
[end]

[globalVar = GP:L=2]
    page.10.variables.logoSrc.value = {$myext.configuration.logo.src.fr}
[end]

now you can simply use {logoSrc} in your fluidtemplate 现在,您只需在您的FluidTemplate中使用{logoSrc}

 ...
<f:link.page pageUid="{settings.rootpid}" class="navbar-brand">
    <img src="{logoSrc}" alt="Logo {settings.brandname}" />
</f:link.page>
...

You can get current languageUid using viewHelper something like below. 您可以使用viewHelper获得当前的languageUid,如下所示。

You ViewHelper file. ViewHelper文件。

<?php
namespace MyVendor\ExtensionKey\ViewHelpers;

class GetLangUidViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
    * GetLangUid
    *
    **/
    public function render() {
        return $GLOBALS['TSFE']->sys_language_uid;
    }
}

In your fluid template get current laguageUid like below. 在您的流体模板中,获取当前的laguageUid,如下所示。

{namespace L=MyVendor\ExtensionKey\ViewHelpers}

<f:if condition="{L:getLangUid()} == 1">
   <f:then>
     <f:link.page pageUid="{settings.rootpid}" class="navbar-brand">
       <img src="fileadmin/branding/brand/images/png/image1.png" alt="Logo {settings.brandname}" />
     </f:link.page>
    </f:then>
    <f:else>
      <f:link.page pageUid="{settings.rootpid}" class="navbar-brand">
        <img src="fileadmin/branding/brand/images/png/image2.png" alt="Logo {settings.brandname}" />
      </f:link.page>
    </f:else>
</f:if>

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

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