Created
October 14, 2011 20:05
-
-
Save nerdsrescueme/1288165 to your computer and use it in GitHub Desktop.
Locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom; | |
class Locale { | |
private static $locale; | |
public static function get() | |
{ | |
if(static::$locale === null) | |
{ | |
static::$locale = new static(); | |
} | |
return static::$locale; | |
} | |
public $active; // Current locale string | |
private $available = array(); // All available locales | |
private $config = array(); | |
private $items = array(); | |
protected $components = array(); | |
public function __construct() | |
{ | |
$this->config = \Atom\Config::get('locale', array()); | |
$this->set_active($this->config['default']); | |
$this->available = $this->config['available'] | |
// Load pre-defined components from configuration | |
foreach($this->config['components'] as $com => $opts) | |
{ | |
if($opts['autoload']) | |
{ | |
$this->register($com, $opts); | |
} | |
} | |
} | |
public function register($component, array $options = array()) | |
{ | |
$class = $component; | |
if(strpos($class, '\\') === false) | |
{ | |
$class = '\\Atom\\Locale\\'.ucfirst($class); | |
} | |
else | |
{ | |
$component = array_pop(explode('\\', $component)); | |
} | |
$this->components[$component] = new $class($options);\ | |
// All sub-components must have access to this class instance | |
// accomplished by adding a locale property to any sub-class | |
$this->components[$component]->locale = $this; | |
return $this; | |
} | |
public function set_active($locale = 'default') | |
{ | |
if($locale == 'default') | |
{ | |
$locale = $this->config['default']; | |
} | |
if(in_array($locale, $this->available)) | |
{ | |
$this->active = $locale; | |
return $this; | |
} | |
throw new \InvalidArgumentException('Locale ['.$locale.'] is not listed as an available locale in locale.php config.'); | |
} | |
private function parse_locale() | |
{ | |
list($language, $region) = explode($this->active); | |
return array('language' => $language, 'region' => $region); | |
} | |
public function __get($property) | |
{ | |
if(isset($this->components[$property])) | |
{ | |
return $this->components[$property]; | |
} | |
\InvalidArgumentException('Property ['.$property.'] does not exist in Locale.'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Locale; | |
class Message { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment