Added default value to Lang::line()->get().

This commit is contained in:
Taylor Otwell 2011-06-22 06:52:10 -07:00
parent eb3a5b0dc9
commit a536ceae72
1 changed files with 18 additions and 31 deletions

View File

@ -2,13 +2,6 @@
class Lang { class Lang {
/**
* All of the loaded language files.
*
* @var array
*/
private static $loaded = array();
/** /**
* All of the loaded language lines. * All of the loaded language lines.
* *
@ -55,24 +48,30 @@ public static function line($key)
} }
/** /**
* Get the language line for a given language. * Get the language line.
* *
* @param string $language * @param mixed $default
* @return string * @return string
*/ */
public function get($language = null) public function get($default = null)
{ {
if (is_null($language)) $language = Config::get('application.language');
{
$language = Config::get('application.language');
}
list($file, $line) = $this->parse($this->key); list($file, $line) = $this->parse($this->key);
$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))
{
return $default;
}
// -------------------------------------------------------------- // --------------------------------------------------------------
// Get the language line from the appropriate file array. // 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])) if (array_key_exists($line, static::$lines[$language.$file]))
{ {
@ -80,7 +79,7 @@ public function get($language = null)
} }
else else
{ {
throw new \Exception("Language line [$line] does not exist for language [$language]"); return $default;
} }
// -------------------------------------------------------------- // --------------------------------------------------------------
@ -127,27 +126,15 @@ private function parse($key)
private function load($file, $language) private function load($file, $language)
{ {
// -------------------------------------------------------------- // --------------------------------------------------------------
// If we have already loaded the language file, bail out. // If we have already loaded the language file or the file
// doesn't exist, bail out.
// -------------------------------------------------------------- // --------------------------------------------------------------
if (in_array($language.$file, static::$loaded)) if (array_key_exists($language.$file, static::$lines) or ! file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
{ {
return; return;
} }
// -------------------------------------------------------------- static::$lines[$language.$file] = require $path;
// Load the language file into the array of lines. The array
// is keyed by the language and file name.
// --------------------------------------------------------------
if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
{
static::$lines[$language.$file] = require $path;
}
else
{
throw new \Exception("Language file [$file] does not exist for language [$language].");
}
static::$loaded[] = $language.$file;
} }
/** /**