Skip to content

Instantly share code, notes, and snippets.

@noopable
Forked from juriansluiman/Module.php
Created July 18, 2012 10:20
Show Gist options
  • Save noopable/3135420 to your computer and use it in GitHub Desktop.
Save noopable/3135420 to your computer and use it in GitHub Desktop.
Set a default locale for your application in Zend Framework 2
<?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