Get the current language code inside a TYPO3 Fluid Template
Asked Answered
B

7

12

Is it possible to get the current language key (or code) in a TYPO3 Fluid template?

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>
Bertsche answered 4/5, 2012 at 9:44 Comment(0)
P
14

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:

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);
Perimorph answered 4/5, 2012 at 10:34 Comment(3)
Very simple and elegant compared to what I've found - thank you.Bertsche
I would advise against this type of assignment, it carries with it a few problems. First, it uses hard-coded references to system language UIDs which is likely to break if transferred to another site. Second, if you do need to translate labels, you should be using f:translate. Perhaps a better strategy is to read the "flag icon name" value from the language and use that in a name of a translated label, e.g. LLL:EXT:myext/Resources/Private/Language/locallang.xml:languages.de and .en etc.Supplementary
Lol you call this elegant? Wtf a switch case is NOT elegant at all.Auston
M
16

Another solution using TypoScript object in Fluid template:

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

Machzor answered 12/1, 2016 at 13:44 Comment(0)
P
14

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:

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);
Perimorph answered 4/5, 2012 at 10:34 Comment(3)
Very simple and elegant compared to what I've found - thank you.Bertsche
I would advise against this type of assignment, it carries with it a few problems. First, it uses hard-coded references to system language UIDs which is likely to break if transferred to another site. Second, if you do need to translate labels, you should be using f:translate. Perhaps a better strategy is to read the "flag icon name" value from the language and use that in a name of a translated label, e.g. LLL:EXT:myext/Resources/Private/Language/locallang.xml:languages.de and .en etc.Supplementary
Lol you call this elegant? Wtf a switch case is NOT elegant at all.Auston
V
8

In order to get the current langage, you can use the Page/LanguageViewHelper included with the VHS extension.

{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

Vinegarish answered 27/3, 2014 at 11:35 Comment(3)
For I use vhs in most projects anyway - that's the best solution for me!Jehiel
So with that a "real world example" could be <f:if condition="{v:page.language()}==1"> <f:then>Willkommen</f:then> <f:else>Welcome</f:else> </f:if>Jehiel
@ Tobias Gaertner you safe my day!Langford
T
6

my Solution is this:

data = TSFE:sys_language_uid

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

<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>
Toluidine answered 24/7, 2018 at 14:13 Comment(0)
E
4

Another approach via the TYPO3 languageAspect

Example within an Extbase Extension (e.g. 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

Excess answered 5/8, 2021 at 11:44 Comment(1)
Thats the right answer 2021. Thanks mate works perfeclty, just tested in the Frontend with <f:debug> // TYPO3 10.4Brockbrocken
S
1

Another option could be to use the v:page.languageMenu ViewHelper from the VHS extension. It would allow you to combine with other ViewHelpers and use something like the following in a Fluid template:

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

<!-- 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 -->
Supplementary answered 13/11, 2013 at 1:10 Comment(1)
loading VHS only to use the language, you should post your comment of using the language file for this, I'd upvote that smart answer in a click !Briny
K
1

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

<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>
Katzen answered 3/6, 2022 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.