I thought flow would switch automatically the language based on the “Acccept-Language request HTTP header”.
So to test it, I switch between one browser which sends only the accept-language request header “de” and another browser which sends the accept-language request header “de,en-US;q=0.9,en;q=0.8”.
Do I therefore have to…
provide the AcceptLanguage HTTP header to the detectLocaleFromHttpHeader() method, which will analyze the header and return the best matching Locale object.
…by myself in my controller? Till now I thought that this is done automatically by flow by the Detector class.
thank you very much for your further try.
With <f:translate id="member.salutation" source="My.Package" />
I get “member.salutation” in frontend for both languages, also when I set your suggested Settings in the Settings.yaml of my package.
With <label for="salutation"><f:translate id="member.salutation" package="My.Package"/></label>
I get again only “Salutation” in frontend for both languages.
The default behaviour is not to automatically switch the locale as this might not be what the dev wants.
I found an old example in slack which should help you. And you can also use the Detectors detectLocaleFromHttpHeader() method instead of the argument like in the example.
But I would expect us to be able to do similar, and then “override” the Configuration object inside I18n\Service, which is currently set to a default depending on the Settings.yaml value
Would be great to have this component as tiny standalone package. Install and everything works as expected. But never looked deeper into the topic. Last time I needed to set the locale manually was years ago.
I started adding something like this to Flow core a while ago, but never got around to finish it yet. It’s still on my TODO list and any help is highly welcome!
<?php
namespace My\Package\Http\LanguageDetection;
/*
* This file is part of the My.Package package.
*
*/
use Neos\Flow\Annotations as Flow;
/**
* The Utility class for locale specific actions
*
* @Flow\Scope("singleton")
*/
class GetLanguageHeader
{
/**
* A pattern which matches HTTP Accept-Language Headers
*/
const PATTERN_MATCH_ACCEPTLANGUAGE = '/([a-z]{1,8}(-[a-z]{1,8})?|\*)(;q=(1|0(\.[0-9]+)?))?/i';
/**
* Parses Accept-Language header and returns 'de' if Accept-Language header contains 'de', otherwise 'en'
* Returns 'de' if there is no Accept-Language header
*
*
* @param string $acceptLanguageHeader
* @return string The language
*/
public static function parseLanguageHeader($acceptLanguageHeader)
{
$acceptLanguageHeader = str_replace(' ', '', $acceptLanguageHeader);
// if there is a language header
if (preg_grep(self::PATTERN_MATCH_ACCEPTLANGUAGE, $acceptLanguageHeader) !== false) {
foreach ($acceptLanguageHeader as $localeIdentifier) {
// if language header contains 'de' or '*', return de; else return 'en'
$de = 'de';
$all = '*';
$find = strpos ($localeIdentifier, $de);
$findAll = strpos ($localeIdentifier, $all);
if( $find !== false || $findAll !== false ) {
return 'de';
} else {
return 'en';
}
}
}
// if no language header, return 'de'
return 'de';
}
}