Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TYPO3 Fluid Template get language code

Tags:

typo3

fluid

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>
like image 821
Rico Leuthold Avatar asked May 04 '12 09:05

Rico Leuthold


4 Answers

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>
like image 114
Sebastian Schmal Avatar answered Sep 20 '22 14:09

Sebastian Schmal


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);
like image 24
biesior Avatar answered Sep 17 '22 14:09

biesior


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.

like image 15
Robert G. Avatar answered Jan 01 '70 00:01

Robert G.


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

like image 8
Florian Rival Avatar answered Jan 01 '70 00:01

Florian Rival