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\EventInterface;
use Zend\ModuleManager\Feature;
class Module implements
Feature\BootstrapListenerInterface
{
public function onBootstrap(Event $e)
{
$default = 'en';
$supported = array('en', 'nl');
$app = $e->getApplication();
$headers = $app->getRequest()->getHeaders();
if ($headers->has('Accept-Language')) {
$header = $headers->get('Accept-Language')->getFieldValue();
$locale = Locale::lookup($supported, $header, $default);
Locale::setDefault($locale);
} else {
Locale::setDefault($default);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment