Why are you using Locale::acceptFromHttp()
? You can write your own parser easily with the ZF2 request object.
$headers = $request->getHeaders();
if ($headers->has('Accept-Language')) {
$locales = $headers->get('Accept-Language')->getPrioritized();
$first = array_shift($locales);
// "language" here is the full locale, though it's quite confusing :s
return $first->getLanguage();
}
There are others complaining about the magic of acceptFromHttp()
and the apparent bugs in this part.
Mind that checking this code (as well as using acceptFromHttp()
does not look for backup alternatives. Let's say your application defaults to 'fr-FR' and you support 'fr-FR' and 'en-US'. A client makes a request with Accept-Language 'nl-NL' and 'en-US'. Your code will check 'nl-NL', which is not supported, and uses the fallback 'fr-FR'. However, the second best option from the client ('en-US') is supported in your app. So you're sending back a locale which the client cannot understand, while you have in fact a negotiable locale available.
NB. You can use SlmLocale for ZF2 to detect locales automatically with a fallback and locale negotiation. The locale is set into php's Locale object with setDefault()
so translators and the like pick up this locale automatically. Disclaimer I wrote that library :)