Commit 66af3542 authored by Taylor Otwell's avatar Taylor Otwell

refactored the lang class for dependency injection.

parent f113b5c8
...@@ -51,6 +51,12 @@ return array( ...@@ -51,6 +51,12 @@ return array(
}), }),
'laravel.lang' => array('singleton' => true, 'resolver' => function($container)
{
return new Lang($container->resolve('laravel.config')->get('application.language'), array(SYS_LANG_PATH, LANG_PATH));
}),
'laravel.package' => array('singleton' => true, 'resolver' => function() 'laravel.package' => array('singleton' => true, 'resolver' => function()
{ {
return new Package; return new Package;
......
...@@ -9,45 +9,58 @@ class Lang { ...@@ -9,45 +9,58 @@ class Lang {
* *
* @var array * @var array
*/ */
public static $lines = array(); private static $lines = array();
/** /**
* The key of the line that is being requested. * The default language being used by the application.
* *
* @var string * @var string
*/ */
public $key; private $language;
/** /**
* The language the line should be returned in. * The paths containing the language files.
*
* @var array
*/
private $paths;
/**
* The key of the language line being retrieved.
* *
* @var string * @var string
*/ */
public $language; private $key;
/** /**
* The place-holder replacements. * The replacements that should be made on the language line.
* *
* @var array * @var array
*/ */
public $replacements = array(); private $replacements;
/**
* The language of the line being retrieved.
*
* @var string
*/
private $line_language;
/** /**
* Create a new Lang instance. * Create a new Lang instance.
* *
* @param string $key * @param string $language
* @param array $replacements * @param array $paths
* @return void * @return void
*/ */
private function __construct($key, $replacements = array()) public function __construct($language, $paths)
{ {
$this->key = $key; $this->paths = $paths;
$this->replacements = $replacements; $this->language = $language;
$this->language = Config::get('application.language');
} }
/** /**
* Create a new Lang instance. * Begin retrieving a new language line.
* *
* Language lines are retrieved using "dot" notation. So, asking for the "messages.required" langauge * Language lines are retrieved using "dot" notation. So, asking for the "messages.required" langauge
* line would return the "required" line from the "messages" language file. * line would return the "required" line from the "messages" language file.
...@@ -56,9 +69,13 @@ class Lang { ...@@ -56,9 +69,13 @@ class Lang {
* @param array $replacements * @param array $replacements
* @return Lang * @return Lang
*/ */
public static function line($key, $replacements = array()) public function line($key, $replacements = array())
{ {
return new static($key, $replacements); $this->key = $key;
$this->replacements = $replacements;
$this->line_language = $this->language;
return $this;
} }
/** /**
...@@ -78,7 +95,7 @@ class Lang { ...@@ -78,7 +95,7 @@ class Lang {
return ($default instanceof \Closure) ? call_user_func($default) : $default; return ($default instanceof \Closure) ? call_user_func($default) : $default;
} }
$line = Arr::get(static::$lines[$this->language.$file], $line, $default); $line = Arr::get(static::$lines[$this->line_language.$file], $line, $default);
foreach ($this->replacements as $key => $value) foreach ($this->replacements as $key => $value)
{ {
...@@ -118,13 +135,13 @@ class Lang { ...@@ -118,13 +135,13 @@ class Lang {
*/ */
private function load($file) private function load($file)
{ {
if (isset(static::$lines[$this->language.$file])) return; if (isset(static::$lines[$this->line_language.$file])) return;
$language = array(); $language = array();
foreach (array(SYS_LANG_PATH, LANG_PATH) as $directory) foreach ($this->paths as $directory)
{ {
if (file_exists($path = $directory.$this->language.'/'.$file.EXT)) if (file_exists($path = $directory.$this->line_language.'/'.$file.EXT))
{ {
$language = array_merge($language, require $path); $language = array_merge($language, require $path);
} }
...@@ -132,10 +149,10 @@ class Lang { ...@@ -132,10 +149,10 @@ class Lang {
if (count($language) > 0) if (count($language) > 0)
{ {
static::$lines[$this->language.$file] = $language; static::$lines[$this->line_language.$file] = $language;
} }
return isset(static::$lines[$this->language.$file]); return isset(static::$lines[$this->line_language.$file]);
} }
/** /**
...@@ -148,7 +165,7 @@ class Lang { ...@@ -148,7 +165,7 @@ class Lang {
*/ */
public function in($language) public function in($language)
{ {
$this->language = $language; $this->line_language = $line_language;
return $this; return $this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment