merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

View File

@@ -26,7 +26,7 @@ class Lang {
/**
* All of the loaded language lines.
*
* The array is keyed by [$language][$file].
* The array is keyed by [$bundle][$language][$file].
*
* @var array
*/
@@ -54,6 +54,9 @@ class Lang {
* // Create a new language line instance for a given line
* $line = Lang::line('validation.required');
*
* // Create a new language line for a line belonging to a bundle
* $line = Lang::line('admin::messages.welcome');
*
* // Specify some replacements for the language line
* $line = Lang::line('validation.required', array('attribute' => 'email'));
* </code>
@@ -65,10 +68,7 @@ class Lang {
*/
public static function line($key, $replacements = array(), $language = null)
{
if (is_null($language))
{
$language = Config::$items['application']['language'];
}
if (is_null($language)) $language = Config::get('application.language');
return new static($key, $replacements, $language);
}
@@ -76,9 +76,6 @@ class Lang {
/**
* Get the language line as a string.
*
* If a language is specified, it should correspond to a directory
* within your application language directory.
*
* <code>
* // Get a language line
* $line = Lang::line('validation.required')->get();
@@ -98,92 +95,104 @@ class Lang {
{
if (is_null($language)) $language = $this->language;
list($file, $line) = $this->parse($this->key);
list($bundle, $file, $line) = $this->parse($this->key);
if ( ! $this->load($file))
// If the file doesn't exist, we'll just return the default value that was
// given to the method. The default value is also returned even when the
// file exists and the file does not actually contain any lines.
if ( ! static::load($bundle, $language, $file))
{
return ($default instanceof Closure) ? call_user_func($default) : $default;
return value($default);
}
return $this->replace(Arr::get(static::$lines[$language][$file], $line, $default));
}
$lines = static::$lines[$bundle][$language][$file];
/**
* Make all necessary replacements on a language line.
*
* Replacements place-holder are prefixed with a colon, and are replaced
* with the appropriate value based on the replacement array set for the
* language line instance.
*
* @param string $line
* @return string
*/
protected function replace($line)
{
foreach ($this->replacements as $key => $value)
$line = array_get($lines, $line, $default);
// If the line is not a string, it probably means the developer asked for
// the entire langauge file and the value of the requested value will be
// an array containing all of the lines in the file.
if (is_string($line))
{
$line = str_replace(':'.$key, $value, $line);
foreach ($this->replacements as $key => $value)
{
$line = str_replace(':'.$key, $value, $line);
}
}
return $line;
}
/**
* Parse a language key into its file and line segments.
* Parse a language key into its bundle, file, and line segments.
*
* Language keys are formatted similarly to configuration keys. The first
* segment represents the language file, while the second segment
* represents a language line within that file.
* Language lines follow a {bundle}::{file}.{line} naming convention.
*
* @param string $key
* @return array
*/
protected function parse($key)
{
if (count($segments = explode('.', $key)) > 1)
{
return array($segments[0], implode('.', array_slice($segments, 1)));
}
$bundle = Bundle::name($key);
throw new \InvalidArgumentException("Invalid language line [$key].");
$segments = explode('.', Bundle::element($key));
// If there are not at least two segments in the array, it means that
// the developer is requesting the entire language line array to be
// returned. If that is the case, we'll make the item "null".
if (count($segments) >= 2)
{
$line = implode('.', array_slice($segments, 1));
return array($bundle, $segments[0], $line);
}
else
{
return array($bundle, $segments[0], null);
}
}
/**
* Load all of the language lines from a language file.
*
* If the language file is successfully loaded, true will be returned.
*
* @param string $bundle
* @param string $language
* @param string $file
* @return bool
*/
protected function load($file)
public static function load($bundle, $language, $file)
{
if (isset(static::$lines[$this->language][$file])) return true;
$language = array();
if (file_exists($path = LANG_PATH.$this->language.'/'.$file.EXT))
if (isset(static::$lines[$bundle][$language][$file]))
{
$language = array_merge($language, require $path);
return true;
}
// If language lines were actually found, they will be loaded into
// the array containing all of the lines for all languages and files.
// The array is keyed by the language and the file name.
if (count($language) > 0)
{
static::$lines[$this->language][$file] = $language;
}
return isset(static::$lines[$this->language][$file]);
$lines = array();
// Language files can belongs to the application or to any bundle
// that is installed for the application. So, we'll need to use
// the bundle's path when checking for the file.
//
// This is similar to the loading method for configuration files,
// but we do not need to cascade across directories since most
// likely language files are static across environments.
$path = Bundle::path($bundle)."language/{$language}/{$file}".EXT;
if (file_exists($path)) $lines = require $path;
static::$lines[$bundle][$language][$file] = $lines;
return count($lines) > 0;
}
/**
* Get the string content of the language line.
*
* @return string
*/
public function __toString()
{
return $this->get();
return (string) $this->get();
}
}