Skip to content

Commit

Permalink
Merge pull request #721 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
Improve the Language management
  • Loading branch information
daveismynamecom committed Apr 12, 2016
2 parents 977f113 + 8f798a6 commit 4f88009
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/Config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace App;

use Core\Language;
use Helpers\Session;

/**
Expand Down Expand Up @@ -123,5 +124,11 @@ public function __construct()
* Start sessions.
*/
Session::init();

/**
* Setup the current Language.
*/
Language::init();
}

}
4 changes: 4 additions & 0 deletions app/Controllers/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
use Core\Language as CoreLanguage;
use Helpers\Url;
use Helpers\Cookie;
use Helpers\Session;


class Language
{
public function change($language)
{
// Only set language if it's in the Languages array
if (preg_match ('/[a-z]/', $language) && in_array($language, CoreLanguage::$codes)) {
Session::set('language', ucfirst($language));
// Store the current Language into Cookie.
Cookie::set(PREFIX .'language', $language);
}

Expand Down
23 changes: 17 additions & 6 deletions system/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Core;

use Core\Error;
use Helpers\Session;
use Helpers\Cookie;

/**
Expand All @@ -30,18 +31,28 @@ class Language
private $array;


protected static function getCurrentLanguage($code = LANGUAGE_CODE)
public static function init()
{
if($code != LANGUAGE_CODE) {
// User defined Language Code?
return $code;
} else if (Cookie::exists(PREFIX .'language')) {
if (Session::exists('language')) {
// The Language was already set; nothing to do.
return;
} else if(Cookie::exists(PREFIX .'language')) {
$cookie = Cookie::get(PREFIX .'language');

if (preg_match ('/[a-z]/', $cookie) && in_array($cookie, self::$codes)) {
return ucfirst($cookie);
Session::set('language', ucfirst($cookie));
}
}
}

protected static function getCurrentLanguage($code)
{
if ($code != LANGUAGE_CODE) {
// User defined Language Code?
return $code;
} else if (Session::exists('language')) {
return Session::get('language');
}

return $code;
}
Expand Down
8 changes: 8 additions & 0 deletions system/Helpers/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public static function init()
}
}

/**
* Determine if a key exists into session.
*/
public static function exists($key)
{
return isset($_SESSION[SESSION_PREFIX .$key]);
}

/**
* Add value to a session.
*
Expand Down

0 comments on commit 4f88009

Please sign in to comment.