Files
spa-api/laravel/lang.php

180 lines
4.4 KiB
PHP
Raw Normal View History

2011-09-21 23:53:52 -05:00
<?php namespace Laravel; use Closure;
2011-06-08 23:45:08 -05:00
2011-09-03 22:36:27 -05:00
class Lang {
/**
* All of the loaded language lines.
*
* The array is keyed by [$language.$file].
*
* @var array
*/
2011-09-08 17:49:16 -05:00
protected static $lines = array();
/**
* The key of the language line being retrieved.
*
* @var string
*/
2011-09-08 17:49:16 -05:00
protected $key;
2011-06-08 23:45:08 -05:00
/**
* The replacements that should be made on the language line.
2011-06-08 23:45:08 -05:00
*
* @var array
*/
2011-09-08 17:49:16 -05:00
protected $replacements;
/**
2011-09-08 17:49:16 -05:00
* The language in which the line should be retrieved.
*
* @var string
*/
2011-09-08 17:49:16 -05:00
protected $language;
2011-06-08 23:45:08 -05:00
/**
2011-09-03 22:36:27 -05:00
* The paths containing the language files.
2011-06-08 23:45:08 -05:00
*
2011-09-03 22:36:27 -05:00
* @var array
2011-06-08 23:45:08 -05:00
*/
2011-09-08 17:49:16 -05:00
protected $paths;
2011-06-08 23:45:08 -05:00
/**
2011-09-03 22:36:27 -05:00
* Create a new Lang instance.
*
2011-06-08 23:45:08 -05:00
* @param string $key
* @param array $replacements
2011-09-03 22:36:27 -05:00
* @param string $language
2011-09-21 21:46:16 -05:00
* @param array $paths
2011-09-03 22:36:27 -05:00
* @return void
2011-06-08 23:45:08 -05:00
*/
2011-09-21 21:46:16 -05:00
protected function __construct($key, $replacements = array(), $language = null, $paths = array())
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
2011-09-21 21:46:16 -05:00
$this->paths = $paths;
2011-09-03 22:36:27 -05:00
$this->language = $language;
$this->replacements = $replacements;
2011-06-08 23:45:08 -05:00
}
/**
* Create a new language line instance.
*
2011-09-21 23:32:12 -05:00
* <code>
* // Create a new language line instance for a given line
* $line = Lang::line('validation.required');
*
* // Specify some replacements for the language line
* $line = Lang::line('validation.required', array('attribute' => 'email'));
* </code>
*
* @param string $key
* @param array $replacements
* @param string $language
2011-09-21 21:46:16 -05:00
* @param array $paths
* @return Lang
*/
2011-09-21 21:46:16 -05:00
public static function line($key, $replacements = array(), $language = null, $paths = array())
{
2011-09-21 21:46:16 -05:00
if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH);
2011-10-04 21:43:39 -05:00
if (is_null($language)) $language = Config::get('application.language');
2011-09-21 21:46:16 -05:00
return new static($key, $replacements, $language, $paths);
}
2011-06-08 23:45:08 -05:00
/**
2011-09-21 23:32:12 -05:00
* 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();
*
* // Get a language line in a specified language
* $line = Lang::line('validation.required')->get('sp');
*
* // Return a default value if the line doesn't exist
* $line = Lang::line('validation.required', null, 'Default');
* </code>
2011-06-08 23:45:08 -05:00
*
2011-07-12 20:04:14 -05:00
* @param string $language
2011-09-08 17:49:16 -05:00
* @param string $default
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-09-08 17:49:16 -05:00
public function get($language = null, $default = null)
2011-06-08 23:45:08 -05:00
{
2011-09-21 23:53:52 -05:00
// If a language was passed to the method, we will let it override the default
2011-09-08 17:49:16 -05:00
if ( ! is_null($language)) $this->language = $language;
list($file, $line) = $this->parse($this->key);
2011-06-08 23:45:08 -05:00
if ( ! $this->load($file))
2011-08-02 20:20:17 -05:00
{
2011-09-21 23:53:52 -05:00
return ($default instanceof Closure) ? call_user_func($default) : $default;
2011-08-02 20:20:17 -05:00
}
2011-09-03 22:36:27 -05:00
$line = Arr::get(static::$lines[$this->language.$file], $line, $default);
2011-07-12 20:04:14 -05:00
2011-06-08 23:45:08 -05:00
foreach ($this->replacements as $key => $value)
{
$line = str_replace(':'.$key, $value, $line);
}
return $line;
}
/**
2011-09-21 23:53:52 -05:00
* Parse a language key into its file and line segments.
2011-06-08 23:45:08 -05:00
*
* @param string $key
* @return array
*/
2011-09-08 17:49:16 -05:00
protected function parse($key)
2011-06-08 23:45:08 -05:00
{
2011-08-08 10:44:03 -05:00
if (count($segments = explode('.', $key)) > 1)
{
return array($segments[0], implode('.', array_slice($segments, 1)));
2011-08-08 10:44:03 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-03 22:10:07 -05:00
throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
2011-06-08 23:45:08 -05:00
}
/**
2011-09-21 23:53:52 -05:00
* Load all of the language lines from a language file.
2011-06-08 23:45:08 -05:00
*
* @param string $file
2011-08-18 23:23:57 -05:00
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-09-08 17:49:16 -05:00
protected function load($file)
2011-06-08 23:45:08 -05:00
{
2011-10-04 21:43:39 -05:00
if (isset(static::$lines[$this->language.$file])) return true;
2011-08-03 22:10:07 -05:00
$language = array();
2011-08-18 23:23:57 -05:00
2011-09-21 23:53:52 -05:00
// Language files cascade. Typically, the system language array is loaded first,
// followed by the application array. This allows the convenient overriding of the
// system language files (like validation) by the application developer.
2011-09-21 21:46:16 -05:00
foreach ($this->paths as $directory)
2011-06-08 23:45:08 -05:00
{
2011-09-03 22:36:27 -05:00
if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
{
$language = array_merge($language, require $path);
}
2011-06-08 23:45:08 -05:00
}
2011-08-18 23:23:57 -05:00
2011-09-21 23:53:52 -05:00
// 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.
2011-09-06 22:26:14 -05:00
if (count($language) > 0) static::$lines[$this->language.$file] = $language;
2011-08-18 23:23:57 -05:00
2011-09-03 22:36:27 -05:00
return isset(static::$lines[$this->language.$file]);
2011-06-08 23:45:08 -05:00
}
2011-07-21 08:32:59 -07:00
/**
* Get the string content of the language line.
*/
2011-09-21 23:53:52 -05:00
public function __toString() { return $this->get(); }
2011-07-21 08:32:59 -07:00
2011-06-08 23:45:08 -05:00
}