Flow: translation of labels not working

Hi Bastian,
thank you for your hint. I tried it with my http component, but I get

# Warning: preg_match_all() expects parameter 2 to be string, array given in .../Data/Temporary/Development/Cache/Code/Flow_Object_Classes/Neos_Flow_I18n_Utility.php line 52

So instead of

if (preg_match_all(self::PATTERN_MATCH_ACCEPTLANGUAGE, $acceptLanguageHeader, $matches, \PREG_PATTERN_ORDER) !== false) {

I use

`if (preg_grep

The detectLocaleFromHttpHeader() method is missing type hints but it expects a string.
So instead of $httpRequest->getHeader('Accept-Language') try with $httpRequest-> getHeaderLine('Accept-Language')

That works for me :ok_hand:
So to summarize it here’s the working code:

My.Package/Configuration/Settings.yaml

Neos:
  Flow:
    http:
      chain:
        'process':
          chain:
            'custom':
              position: 'before routing'
              component: 'My\Package\Http\LanguageDetection\LanguageDetectionComponent'
    i18n:
      defaultLocale: de

My.Package/Classes/Http/LanguageDetection/LanguageDetectionComponent.php

<?php
namespace My\Packageg\Http\LanguageDetection;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Psr\Http\Message\ServerRequestInterface;


/**
 * A HTTP component that detects and sets the current locale from Accept-Language header.
 */
class LanguageDetectionComponent implements ComponentInterface 
{
	

	/**
	 * @var \Neos\Flow\I18n\Service
	 * @Flow\Inject
	 */
	protected $i18nService;
	
	/**
	 * @var \Neos\Flow\I18n\Detector
     * @Flow\Inject
	 */
	protected $detector; 
	
	    
	/**
	 * @param ServerRequestInterface $httpRequest
	 * @return Locale
	 */
	protected function retrieveLocaleFromRequest(ServerRequestInterface $httpRequest)
	{
		// use core detector
		$language = $this->detector->detectLocaleFromHttpHeader($httpRequest->getHeaderLine('Accept-Language'));
		return $language;	
	}
	

	/**
	 * @param ComponentContext $componentContext
	 * @return void
	 */
	public function handle(ComponentContext $componentContext)
	{
		$httpRequest = $componentContext->getHttpRequest();
		$detectedLocale = $this->retrieveLocaleFromRequest($httpRequest);
       	$this->i18nService->getConfiguration()->setCurrentLocale($detectedLocale);
	}
}

It’s always a pleasure to act with this great community :heart_eyes:
Thank you all!

4 Likes