Negotiates the client language of a http request using the Accept-Language
http header.
composer require usox/language-negotiator
There are several ways to use the negotiator.
use Usox\LanguageNegotiator\LanguageNegotiator;
$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
$_SERVER
);
$clientLanguage = $negotiator->negotiate();
use Usox\LanguageNegotiator\LanguageNegotiator;
$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
);
$clientLanguage = $negotiator->negotiate(
$_SERVER
);
The negotiator will automatically enrich ServerRequest
with the negotiated client language. It will be added
as an attribute which can obtained using the attribute name constant.
use Usox\LanguageNegotiator\LanguageNegotiator;
$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
);
// assumes, you have some kind of framework which supports PSR request handling
$myFramework->addMiddleware($negotiator);
// get the language from the psr server request
$clientLanguage = $request->getAttribute(LanguageNegotiator::REQUEST_ATTRIBUTE_NAME);