refactored the lang class for dependency injection.
This commit is contained in:
parent
f113b5c829
commit
66af3542e9
|
@ -51,6 +51,12 @@
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
||||||
|
'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 {
|
||||||
*
|
*
|
||||||
* @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 string
|
|
||||||
*/
|
|
||||||
public $language;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The place-holder replacements.
|
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $replacements = array();
|
private $paths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The key of the language line being retrieved.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $key;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The replacements that should be made on the language line.
|
||||||
|
*
|
||||||
|
* @var 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 @@ private function __construct($key, $replacements = array())
|
||||||
* @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 @@ public function get($default = null)
|
||||||
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 @@ private function parse($key)
|
||||||
*/
|
*/
|
||||||
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 @@ private function load($file)
|
||||||
|
|
||||||
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 @@ private function load($file)
|
||||||
*/
|
*/
|
||||||
public function in($language)
|
public function in($language)
|
||||||
{
|
{
|
||||||
$this->language = $language;
|
$this->line_language = $line_language;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue