Skip to content

Instantly share code, notes, and snippets.

@agborkowski
Created October 12, 2012 17:32
Show Gist options
  • Save agborkowski/3880414 to your computer and use it in GitHub Desktop.
Save agborkowski/3880414 to your computer and use it in GitHub Desktop.
Lithiumphp::g11n::tld
/**
* @author AgBorkowski <andrzejborkowski@gmail.com>
* @link http://blog.aeonmedia.eu
*/
/**
* Locales
*
* Adds globalization specific settings to the environment. The settings for
* the current locale, time zone and currency are kept as environment settings.
* This allows for _centrally_ switching, _transparently_ setting and
* retrieving globalization related settings.
*
* The environment settings are:
*
* - `'locale'` The default effective locale.
* - `'locales'` Application locales available mapped to names. The available locales are used
* to negotiate he effective locale, the names can be used i.e. when displaying
* a menu for choosing the locale to users.
*
* @see lithiumm\g11n\Message
* @see lithiumm\core\Environment
*/
$locale = 'en';
$locales = array('pl' => 'Polski', 'fr' => 'French', 'en' => 'English', 'de' => 'German');
$tld = array(
'blusand.pl' => array('locale' => 'pl'),
'blusand.fr' => array('locale' => 'fr'),
'blusand.eu' => array('locale' => 'en'),
'blusand.net' => array('redirect' => 'blusand.eu'),
'blusand.co.uk' => array('redirect' => 'bluesand.eu'),
'blusand.de' => array('locale' => 'de')
);
Environment::set('production', compact('locale', 'locales', 'tld'));
$locale = 'en';
$locales = array('pl' => 'Polski', 'fr' => 'French', 'en' => 'English', 'de' => 'German');
$tld = array(
'dev.blusand.pl' => array('locale' => 'pl'),
'dev.blusand.fr' => array('locale' => 'fr'),
'dev.blusand.eu' => array('locale' => 'en'),
'dev.blusand.net' => array('redirect' => 'dev.blusand.eu'),
'dev.blusand.co.uk' => array('redirect' => 'dev.blusand.eu'),
'dev.blusand.de' => array('locale' => 'de')
);
Environment::set('development', compact('locale', 'locales', 'tld'));
Environment::set('test', array('locale' => 'en', 'locales' => array('en' => 'English')));
/**
* Effective/Request Locale
*
* Intercepts dispatching processes in order to set the effective locale by using
* the locale of the request or if that is not available retrieving a locale preferred
* by the client.
*
* @see lithiumm\g11n\Message
* @see lithiumm\core\Environment
*/
$setLocale = function($self, $params, $chain){
$locales = Environment::get('locales');
$locale = Environment::get('locale');
$tld = Environment::get('tld');
$host = $params['request']->env('HTTP_HOST');
if (isset($tld[$host])) {
if (!empty($tld[$host]['redirect']) && $tld[$host]['redirect'] !== $host) {
header('Location: http://' . $tld[$host]['redirect']);
exit;
}
$locale = $tld[$host]['locale'];
} else {
if (!$params['request']->locale()) {
$preferred = Locale::preferred($params['request'], array_keys($locales));
if ($preferred !== NULL) {
$locale = $preferred;
}
}
}
$params['request']->locale($locale);
Environment::set(true, array('locale' => $params['request']->locale()));
return $chain->next($self, $params, $chain);
};
ActionDispatcher::applyFilter('_callable', $setLocale);
ConsoleDispatcher::applyFilter('_callable', $setLocale);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment