TYPO3 Fluid Template get language code
Solution 1
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);
Solution 2
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.
Solution 3
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
Solution 4
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>
Solution 5
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
Comments
-
Rico Leuthold almost 2 years
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>