Files
spa-api/laravel/lang.php

247 lines
5.7 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 {
/**
* The key of the language line being retrieved.
*
* @var string
*/
protected $key;
2011-10-20 22:23:32 -05:00
/**
* The replacements that should be made on the language line.
2011-10-20 22:23:32 -05:00
*
* @var array
*/
protected $replacements;
2011-10-20 22:23:32 -05:00
/**
* The language in which the line should be retrieved.
*
* @var string
*/
protected $language;
2011-06-08 23:45:08 -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
*/
protected static $lines = array();
/**
* 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-06-08 23:45:08 -05:00
* @param string $key
* @param array $replacements
2011-09-03 22:36:27 -05:00
* @param string $language
* @return void
2011-06-08 23:45:08 -05:00
*/
protected function __construct($key, $replacements = array(), $language = null)
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
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');
*
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>
*
* @param string $key
* @param array $replacements
* @param string $language
* @return Lang
*/
public static function line($key, $replacements = array(), $language = null)
{
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
return new static($key, $replacements, $language);
}
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)
{
return ! is_null(static::line($key, array(), $language)->get());
}
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
* $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-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-01-16 13:59:24 -06:00
// 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))
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
// 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))
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-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
// 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
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
*/
public function __toString()
{
2012-01-16 13:59:24 -06:00
return (string) $this->get();
}
2011-07-21 08:32:59 -07:00
2011-11-16 12:57:20 -06:00
}