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>
nito
  • 1,157
  • 8
  • 21
Rico Leuthold
  • 1,975
  • 5
  • 34
  • 49

7 Answers7

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.

Robert G.
  • 317
  • 2
  • 8
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);
biesior
  • 55,576
  • 10
  • 125
  • 182
  • Very simple and elegant compared to what I've found - thank you. – Rico Leuthold May 04 '12 at 11:05
  • 7
    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. – Claus Due Nov 13 '13 at 00:58
  • Lol you call this elegant? Wtf a switch case is NOT elegant at all. – Kaasstengel Oct 30 '18 at 10:43
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

Florian Rival
  • 601
  • 4
  • 11
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>
Sebastian
  • 883
  • 11
  • 32
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

Josef Glatz
  • 514
  • 4
  • 20
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 -->
yunzen
  • 32,854
  • 11
  • 73
  • 106
Claus Due
  • 4,166
  • 11
  • 23
  • 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 ! – webman Apr 28 '20 at 13:03
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>
Urs Bo
  • 308
  • 1
  • 11