Refactor Lang class.

This commit is contained in:
Taylor Otwell 2011-07-06 14:45:08 -07:00
parent e2973e565d
commit 5643daa66c
1 changed files with 6 additions and 26 deletions

View File

@ -61,31 +61,16 @@ public function get($default = null)
$this->load($file, $language); $this->load($file, $language);
// --------------------------------------------------------------
// If the language file did not exist, return the default value.
// --------------------------------------------------------------
if ( ! array_key_exists($language.$file, static::$lines)) if ( ! array_key_exists($language.$file, static::$lines))
{ {
return $default; // The language file doesn't exist, return the default value.
} $line = is_callable($default) ? call_user_func($default) : $default;
// --------------------------------------------------------------
// Get the language line from the appropriate file array.
// If the line doesn't exist, return the default value.
// --------------------------------------------------------------
if (array_key_exists($line, static::$lines[$language.$file]))
{
$line = static::$lines[$language.$file][$line];
} }
else else
{ {
return $default; $line = Arr::get(static::$lines[$language.$file], $line, $default);
} }
// --------------------------------------------------------------
// Make all place-holder replacements. Place-holders are prefixed
// with a colon for convenient location.
// --------------------------------------------------------------
foreach ($this->replacements as $key => $value) foreach ($this->replacements as $key => $value)
{ {
$line = str_replace(':'.$key, $value, $line); $line = str_replace(':'.$key, $value, $line);
@ -102,10 +87,8 @@ public function get($default = null)
*/ */
private function parse($key) private function parse($key)
{ {
// -------------------------------------------------------------- // The left side of the dot is the file name, while the right side of the dot
// The left side of the dot is the file name, while the right // is the item within that file being requested.
// side of the dot is the item within that file being requested.
// --------------------------------------------------------------
$segments = explode('.', $key); $segments = explode('.', $key);
if (count($segments) < 2) if (count($segments) < 2)
@ -125,10 +108,7 @@ private function parse($key)
*/ */
private function load($file, $language) private function load($file, $language)
{ {
// -------------------------------------------------------------- // If we have already loaded the language file or the file doesn't exist, bail out.
// If we have already loaded the language file or the file
// doesn't exist, bail out.
// --------------------------------------------------------------
if (array_key_exists($language.$file, static::$lines) or ! file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) if (array_key_exists($language.$file, static::$lines) or ! file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
{ {
return; return;