Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Update feature #590: Better i18n init and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepone committed Jan 10, 2014
1 parent 5433062 commit b55b83d
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions modules/gleez/classes/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,25 @@ public static function initialize()
// 2. Check the user's preference
if(!$locale AND ($locale_override == 'ALL' OR $locale_override == 'USER'))
{
$locale = I18n::userLocale(self::$_languages);
$locale = I18n::userLocale();
}

// 3. Check the request client/browser's preference
if(!$locale AND ($locale_override == 'ALL' OR $locale_override == 'CLIENT'))
{
$locale = I18n::requestLocale(self::$_languages);
$locale = I18n::requestLocale();
}

// 4. Check the url preference and get the language from url
if(!$locale AND ($locale_override == 'ALL' OR $locale_override == 'URL'))
{
$locale = I18n::urlLocale(self::$_languages);
$locale = I18n::urlLocale();
}

// 5. Check the sub-domain preference and get the language form subdomain
if(!$locale AND ($locale_override == 'ALL' OR $locale_override == 'DOMAIN'))
{
$locale = I18n::domainLocale(self::$_languages);
$locale = I18n::domainLocale();
}

// 6. Default locale
Expand Down Expand Up @@ -125,19 +125,16 @@ public static function isAvailable($lang)
* // Get the language
* $lang = I18n::requestLocale();
*
* @param array $installed_locales array of instaleld locales
* @return string
*/
public static function requestLocale( array $installed_locales )
public static function requestLocale()
{
$requested_locales = Request::accept_lang();
//@todo score comparison
foreach($requested_locales as $locale => $score)
// Look for a preferred language in the `Accept-Language` header directive.
$locale = Request::initial()->headers()->preferred_language(array_keys(self::$_languages));

if( self::isAvailable($locale) )
{
if( self::isAvailable($locale) )
{
return $locale;
}
return $locale;
}

return FALSE;
Expand All @@ -149,10 +146,9 @@ public static function requestLocale( array $installed_locales )
* // Get the language
* $lang = I18n::userLocale();
*
* @param array $installed_locales array of instaleld locales
* @return string
*/
public static function userLocale( array $installed_locales )
public static function userLocale()
{
//Can't set guest users locale, default's to site locale
if(User::is_guest())
Expand All @@ -179,17 +175,17 @@ public static function userLocale( array $installed_locales )
* // Get the language
* $lang = I18n::cookieLocale();
*
* @param array $installed_locales array of instaleld locales
* @return string
*/
public static function cookieLocale( array $installed_locales )
public static function cookieLocale()
{
$cookie_data = strtolower(Cookie::get(self::$_cookie));

//double check cookie data
if ($cookie_data AND preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)$/", trim($cookie_data), $matches))
{
$locale = $matches[1];

if( self::isAvailable($locale) )
{
return $locale;
Expand All @@ -205,13 +201,12 @@ public static function cookieLocale( array $installed_locales )
* ex: example.com/fr/
* $lang = I18n::urlLocale();
*
* @param array $installed_locales array of instaleld locales
* @return string
*/
public static function urlLocale( array $installed_locales )
public static function urlLocale()
{
$uri = Request::detect_uri();
if (preg_match ('/^\/(' . join ('|', array_keys($installed_locales)) . ')\/?$/', $uri, $matches))
if (preg_match ('/^\/(' . join ('|', array_keys(self::$_languages)) . ')\/?$/', $uri, $matches))
{
//'~^(?:' . implode('|', array_keys($installed_locales)) . ')(?=/|$)~i'
// matched /lang or /lang/
Expand All @@ -227,12 +222,11 @@ public static function urlLocale( array $installed_locales )
* ex: fr.example.com
* $lang = I18n::domainLocale();
*
* @param array $installed_locales array of instaleld locales
* @return string
*/
public static function domainLocale( array $installed_locales )
public static function domainLocale()
{
if (preg_match ('/^(' . join ('|', array_keys($installed_locales)) . ')\./', $_SERVER['HTTP_HOST'], $matches))
if (preg_match ('/^(' . join ('|', array_keys(self::$_languages)) . ')\./', $_SERVER['HTTP_HOST'], $matches))
{
return $matches[1];
}
Expand All @@ -258,7 +252,7 @@ public static function lang($lang = NULL)
if ($lang && self::isAvailable($lang) )
{
// Store target language in I18n
I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', self::$_languages[$lang]['i18n_code']));
I18n::$lang = self::$_languages[$lang]['i18n_code'];

// Set locale
setlocale(LC_ALL, self::$_languages[$lang]['locale']);
Expand Down

0 comments on commit b55b83d

Please sign in to comment.