Skip to content

Instantly share code, notes, and snippets.

@xelaz
Forked from noopable/Module.php
Last active August 29, 2015 14:09
Show Gist options
  • Save xelaz/8f59af81964e09a2bc8b to your computer and use it in GitHub Desktop.
Save xelaz/8f59af81964e09a2bc8b to your computer and use it in GitHub Desktop.
<?php
namespace Application;
use Locale;
use Zend\EventManager\Event;
use Zend\ModuleManager\Feature;
class Module implements
Feature\BootstrapListenerInterface
{
public function onBootstrap(Event $e)
{
$default = 'nl';
$supported = array('en', 'en-GB');
$app = $e->getApplication();
$headers = $app->getRequest()->getHeaders();
if ($headers->has('Accept-Language')) {
$locales = $headers->get('Accept-Language')->getPrioritized();
// Loop through all locales, highest priority first
foreach ($locales as $locale) {
if (!!($match = Locale::lookup($supported, $locale))) {
// The locale is one of our supported list
Locale::setDefault($match);
break;
}
}
if (!$match) {
// Nothing from the supported list is a match
Locale::setDefault($default);
}
} else {
Locale::setDefault($default);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment