Use multilanguage with eID

Pass the language key to the view

In this example I´ll use ajax to submit/validate a form. Inside the controller I´ll assign the current frontend language key to the view to use it in the form later.

 

/**
 * Displays the form
 */
public function formAction()
{
    // assign language key to pass it in ajax request
    $this->view->assign('lang', $GLOBALS['TSFE']->lang);
}

 

Now I just need to add a hidden input to the form and I´m ready for eID:

 

<f:form.hidden id="userLang" name="lang" value="{lang}"/>

 

 

Use the key and initialize the eID class

In my example the eID class get´s the language key via post parameter. In my case it the best way was to insert the LanguageService and then fire the init() method with the value of passed post parameter.

 

/**
 * @var LanguageService
 */
protected $languageService;

/**
 * CustomerServerAssignment constructor.
 */
public function __construct()
{
 /** @var LanguageService $languageService */
 $this->languageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Lang\\LanguageService');
 $this->languageService->init(trim(GeneralUtility::_POST('language')));
}

 

Now we are ready to translate our content to the selected language. I added the method translate() for that to get it shorter inside the rest of my code:

 

/**
 * Returns the translation of $key
 *
 * @param string $key
 * @return string
 */
protected function translate($key)
{
 return $this->languageService->sL('LLL:EXT:my_ext/Resources/Private/Language/locallang.xlf:' . $key);
}