AOEpeople / TYPO3_Restler

restler (PHP REST-Framework) for TYPO3
GNU General Public License v3.0
30 stars 17 forks source link

Add an option to easily change request language #69

Closed soee closed 1 year ago

soee commented 2 years ago

What is the proper way to change the request language used by Restler?

From what i see the language used by frontend controller is set in https://github.com/AOEpeople/TYPO3_Restler/blob/main/Classes/System/TYPO3/Loader.php#L213 using the site default language.

In one of our projects we base on the Accept-Language header and having custom middleware executed before Restlers' Dispatcher we can set request argument language and language aspect based on this header value. But it is ignored by Reslter when it initializes frontend rendering through Loader::initializeFrontendRendering(). We could probably add hook into TypoScriptFrontendController::settingLanguage() function and modify the language aspect there but i think the middleware layer is better place to handle it.

What do you think about changing the Loader::initializeFrontendRendering() function to create the Requets object this way:

$request = $this->getRequest()
    ->withAttribute('site', $site)
    ->withAttribute('routing', $pageArguments)
    ->withAttribute('language', $this->getRequest()->getAttribute('language'))
    ->withAttribute('normalizedParams', $normalizedParams)
    ->withQueryParams($_GET)
    ->withCookieParams($_COOKIE);

so here we fetch the SiteLanguage object from the language request attribute $this->getRequest()->getAttribute('language') and we can easily modify it through middlewares.

felixsemmler commented 2 years ago

Hi Marcin, sorry for my late response. I think also that middleware is the right place to do this. You can create a pull-request for this feature and we have a look for it soon.