Files
spa-api/laravel/lang.php

183 lines
3.7 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
2011-08-30 22:35:32 -05:00
class Lang {
2011-06-08 23:45:08 -05:00
/**
* All of the loaded language lines.
*
* The array is keyed by [$language.$file].
*
2011-06-08 23:45:08 -05:00
* @var array
*/
private $lines = array();
2011-06-08 23:45:08 -05:00
/**
* The default language being used by the application.
2011-06-08 23:45:08 -05:00
*
* @var string
*/
private $language;
2011-06-08 23:45:08 -05:00
/**
* The paths containing the language files.
*
* @var array
*/
private $paths;
/**
* The key of the language line being retrieved.
*
* @var string
*/
private $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
*/
private $replacements;
/**
* The language of the line being retrieved.
*
* This is set to the default language when a new line is requested.
* However, it may be changed using the "in" method.
*
* @var string
*/
private $line_language;
2011-06-08 23:45:08 -05:00
/**
* Create a new Lang instance.
*
* @param string $language
* @param array $paths
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct($language, $paths)
2011-06-08 23:45:08 -05:00
{
$this->paths = $paths;
$this->language = $language;
2011-06-08 23:45:08 -05:00
}
/**
* Begin retrieving a new language line.
*
* Language lines are retrieved using "dot" notation. So, asking for the "messages.required" langauge
* line would return the "required" line from the "messages" language file.
*
2011-06-08 23:45:08 -05:00
* @param string $key
* @param array $replacements
2011-06-08 23:45:08 -05:00
* @return Lang
*/
public function line($key, $replacements = array())
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
$this->replacements = $replacements;
$this->line_language = $this->language;
return $this;
2011-06-08 23:45:08 -05:00
}
/**
* Get the language line.
2011-06-08 23:45:08 -05:00
*
* A default value may also be specified, which will be returned in the language line doesn't exist.
*
2011-07-12 20:04:14 -05:00
* @param string $language
2011-06-08 23:45:08 -05:00
* @return string
*/
public function get($default = null)
2011-06-08 23:45:08 -05:00
{
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
{
return ($default instanceof \Closure) ? call_user_func($default) : $default;
2011-08-02 20:20:17 -05:00
}
$line = Arr::get($this->lines[$this->line_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;
}
/**
* Parse a language key.
*
* Language keys follow a {file}.{key} convention. If a specific language key is not
* specified, an exception will be thrown. Setting entire language files at run-time
* is not currently supported.
*
2011-06-08 23:45:08 -05:00
* @param string $key
* @return array
*/
private 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
}
/**
* Load a language file.
*
* If the language file has already been loaded, it will not be loaded again.
*
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
*/
private function load($file)
2011-06-08 23:45:08 -05:00
{
if (isset($this->lines[$this->line_language.$file])) return;
2011-08-03 22:10:07 -05:00
$language = array();
2011-08-18 23:23:57 -05:00
foreach ($this->paths as $directory)
2011-06-08 23:45:08 -05:00
{
if (file_exists($path = $directory.$this->line_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
if (count($language) > 0)
{
$this->lines[$this->line_language.$file] = $language;
}
2011-08-18 23:23:57 -05:00
return isset($this->lines[$this->line_language.$file]);
2011-06-08 23:45:08 -05:00
}
/**
* Set the language the line should be returned in.
*
* The language specified in this method should correspond to a language directory in your application.
*
* @param string $language
* @return Lang
*/
public function in($language)
{
$this->line_language = $line_language;
return $this;
}
2011-07-21 08:32:59 -07:00
/**
* Get the string content of the language line.
*/
public function __toString()
{
return $this->get();
}
2011-06-08 23:45:08 -05:00
}