2012-05-11 12:20:18 +03:00
|
|
|
<?php namespace Laravel;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-09-03 22:36:27 -05:00
|
|
|
class Lang {
|
|
|
|
|
|
|
|
|
|
/**
|
2011-10-21 21:49:33 -05:00
|
|
|
* The key of the language line being retrieved.
|
2011-08-29 22:11:40 -05:00
|
|
|
*
|
2011-10-21 21:49:33 -05:00
|
|
|
* @var string
|
2011-08-29 22:11:40 -05:00
|
|
|
*/
|
2011-10-21 21:49:33 -05:00
|
|
|
protected $key;
|
2011-08-29 22:11:40 -05:00
|
|
|
|
2011-10-20 22:23:32 -05:00
|
|
|
/**
|
2011-10-21 21:49:33 -05:00
|
|
|
* The replacements that should be made on the language line.
|
2011-10-20 22:23:32 -05:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2011-10-21 21:49:33 -05:00
|
|
|
protected $replacements;
|
2011-10-20 22:23:32 -05:00
|
|
|
|
2011-08-29 22:11:40 -05:00
|
|
|
/**
|
2011-10-21 21:49:33 -05:00
|
|
|
* The language in which the line should be retrieved.
|
2011-08-24 22:51:32 -05:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-10-21 21:49:33 -05:00
|
|
|
protected $language;
|
2011-08-24 22:51:32 -05:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-10-21 21:49:33 -05:00
|
|
|
* All of the loaded language lines.
|
|
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* The array is keyed by [$bundle][$language][$file].
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2011-10-21 21:49:33 -05:00
|
|
|
protected static $lines = array();
|
2011-08-29 22:11:40 -05:00
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
/**
|
|
|
|
|
* The language loader event name.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
const loader = 'laravel.language.loader';
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-09-03 22:36:27 -05:00
|
|
|
* Create a new Lang instance.
|
2011-08-24 22:51:32 -05:00
|
|
|
*
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param string $key
|
2011-07-21 08:18:49 -07:00
|
|
|
* @param array $replacements
|
2011-09-03 22:36:27 -05:00
|
|
|
* @param string $language
|
|
|
|
|
* @return void
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-10-13 21:32:11 -05:00
|
|
|
protected function __construct($key, $replacements = array(), $language = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-29 22:11:40 -05:00
|
|
|
$this->key = $key;
|
2011-09-03 22:36:27 -05:00
|
|
|
$this->language = $language;
|
2012-04-19 23:40:38 +04:00
|
|
|
$this->replacements = (array) $replacements;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-20 23:14:09 -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');
|
|
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* // Create a new language line for a line belonging to a bundle
|
|
|
|
|
* $line = Lang::line('admin::messages.welcome');
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* // Specify some replacements for the language line
|
|
|
|
|
* $line = Lang::line('validation.required', array('attribute' => 'email'));
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-09-20 23:14:09 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param array $replacements
|
|
|
|
|
* @param string $language
|
|
|
|
|
* @return Lang
|
|
|
|
|
*/
|
2011-10-13 21:32:11 -05:00
|
|
|
public static function line($key, $replacements = array(), $language = null)
|
2011-09-20 23:14:09 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
if (is_null($language)) $language = Config::get('application.language');
|
2011-10-04 21:43:39 -05:00
|
|
|
|
2011-10-13 21:32:11 -05:00
|
|
|
return new static($key, $replacements, $language);
|
2011-09-20 23:14:09 -05:00
|
|
|
}
|
|
|
|
|
|
2012-02-01 10:02:46 -06:00
|
|
|
/**
|
|
|
|
|
* Determine if a language line exists.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param string $language
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function has($key, $language = null)
|
|
|
|
|
{
|
2012-05-05 11:36:04 -05:00
|
|
|
return static::line($key, array(), $language)->get() !== $key;
|
2012-02-01 10:02:46 -06:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-09-21 23:32:12 -05:00
|
|
|
* Get the language line as a string.
|
|
|
|
|
*
|
|
|
|
|
* <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
|
2012-05-12 18:28:45 +02:00
|
|
|
* $line = Lang::line('validation.required')->get(null, 'Default');
|
2011-09-21 23:32:12 -05:00
|
|
|
* </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
|
|
|
{
|
2012-05-03 08:42:44 -05:00
|
|
|
// If no default value is specified by the developer, we'll just return the
|
|
|
|
|
// key of the language line. This should indicate which language line we
|
|
|
|
|
// were attempting to render and is better than giving nothing back.
|
|
|
|
|
if (is_null($default)) $default = $this->key;
|
|
|
|
|
|
2011-11-23 08:29:23 -06:00
|
|
|
if (is_null($language)) $language = $this->language;
|
2011-09-08 17:49:16 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
list($bundle, $file, $line) = $this->parse($this->key);
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2012-05-03 08:42:44 -05:00
|
|
|
// If the file does not exist, we'll just return the default value that was
|
2012-01-16 13:59:24 -06:00
|
|
|
// given to the method. The default value is also returned even when the
|
2012-05-03 08:42:44 -05:00
|
|
|
// file exists and that file does not actually contain any lines.
|
2012-01-16 13:59:24 -06:00
|
|
|
if ( ! static::load($bundle, $language, $file))
|
2011-08-02 20:20:17 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return value($default);
|
2011-08-02 20:20:17 -05:00
|
|
|
}
|
|
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
$lines = static::$lines[$bundle][$language][$file];
|
2011-07-12 20:04:14 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
$line = array_get($lines, $line, $default);
|
|
|
|
|
|
|
|
|
|
// If the line is not a string, it probably means the developer asked for
|
2012-07-21 20:18:55 -04:00
|
|
|
// the entire language file and the value of the requested value will be
|
2012-01-16 13:59:24 -06:00
|
|
|
// an array containing all of the lines in the file.
|
|
|
|
|
if (is_string($line))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
foreach ($this->replacements as $key => $value)
|
|
|
|
|
{
|
|
|
|
|
$line = str_replace(':'.$key, $value, $line);
|
|
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Parse a language key into its bundle, file, and line segments.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* Language lines follow a {bundle}::{file}.{line} naming convention.
|
2011-10-13 21:32:11 -05:00
|
|
|
*
|
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
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$bundle = Bundle::name($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)
|
2011-08-08 10:44:03 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$line = implode('.', array_slice($segments, 1));
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
return array($bundle, $segments[0], $line);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return array($bundle, $segments[0], null);
|
|
|
|
|
}
|
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
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* @param string $bundle
|
|
|
|
|
* @param string $language
|
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
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function load($bundle, $language, $file)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
if (isset(static::$lines[$bundle][$language][$file]))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return true;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-08-18 23:23:57 -05:00
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
// We use a "loader" event to delegate the loading of the language
|
|
|
|
|
// array, which allows the develop to organize the language line
|
|
|
|
|
// arrays for their application however they wish.
|
|
|
|
|
$lines = Event::first(static::loader, func_get_args());
|
|
|
|
|
|
|
|
|
|
static::$lines[$bundle][$language][$file] = $lines;
|
|
|
|
|
|
|
|
|
|
return count($lines) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load a language array from a language file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $bundle
|
|
|
|
|
* @param string $language
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function file($bundle, $language, $file)
|
|
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$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
|
2012-02-16 15:12:42 -06:00
|
|
|
// the bundle's path when looking for the file.
|
|
|
|
|
$path = static::path($bundle, $language, $file);
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-02-16 15:12:42 -06:00
|
|
|
if (file_exists($path))
|
|
|
|
|
{
|
|
|
|
|
$lines = require $path;
|
|
|
|
|
}
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
return $lines;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2012-02-16 15:12:42 -06:00
|
|
|
/**
|
|
|
|
|
* Get the path to a bundle's language file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $bundle
|
|
|
|
|
* @param string $language
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function path($bundle, $language, $file)
|
|
|
|
|
{
|
|
|
|
|
return Bundle::path($bundle)."language/{$language}/{$file}".EXT;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-21 08:32:59 -07:00
|
|
|
/**
|
|
|
|
|
* Get the string content of the language line.
|
2012-01-16 13:59:24 -06:00
|
|
|
*
|
|
|
|
|
* @return string
|
2011-07-21 08:32:59 -07:00
|
|
|
*/
|
2011-10-13 21:32:11 -05:00
|
|
|
public function __toString()
|
|
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return (string) $this->get();
|
2011-10-13 21:32:11 -05:00
|
|
|
}
|
2011-07-21 08:32:59 -07:00
|
|
|
|
2012-05-12 18:28:45 +02:00
|
|
|
}
|