Files
spa-api/laravel/lang.php

136 lines
2.8 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
class Lang {
/**
* All of the loaded language lines.
*
* The array is keyed by [$language.$file].
*
2011-06-08 23:45:08 -05:00
* @var array
*/
2011-07-26 23:23:34 -05:00
public static $lines = array();
2011-06-08 23:45:08 -05:00
/**
* The key of the line that is being requested.
*
* @var string
*/
2011-07-26 23:23:34 -05:00
public $key;
2011-06-08 23:45:08 -05:00
/**
* The place-holder replacements.
*
* @var array
*/
2011-07-26 23:23:34 -05:00
public $replacements = array();
2011-06-08 23:45:08 -05:00
/**
* Create a new Lang instance.
*
2011-07-07 23:48:05 -05:00
* Language lines are retrieved using "dot" notation. So, asking for the
* "messages.required" language line would return the "required" line
* from the "messages" language file.
*
* @param string $key
* @param array $replacements
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct($key, $replacements = array())
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
$this->replacements = $replacements;
2011-06-08 23:45:08 -05:00
}
/**
* Create a Lang instance for a language line.
*
* @param string $key
* @param array $replacements
2011-06-08 23:45:08 -05:00
* @return Lang
*/
public static function line($key, $replacements = array())
2011-06-08 23:45:08 -05:00
{
return new static($key, $replacements);
2011-06-08 23:45:08 -05:00
}
/**
* Get the language line.
2011-06-08 23:45:08 -05:00
*
2011-07-12 20:04:14 -05:00
* @param string $language
* @param mixed $default
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-08-02 20:20:17 -05:00
public function get($language = null, $default = null)
2011-06-08 23:45:08 -05:00
{
2011-08-14 22:34:22 -05:00
if (is_null($language)) $language = Config::get('application.language');
2011-06-08 23:45:08 -05:00
2011-08-03 22:10:07 -05:00
list($module, $file, $line) = $this->parse($this->key, $language);
2011-06-08 23:45:08 -05:00
2011-08-18 23:23:57 -05:00
if ( ! $this->load($module, $file, $language))
2011-08-02 20:20:17 -05:00
{
return is_callable($default) ? call_user_func($default) : $default;
}
2011-08-18 23:23:57 -05:00
$line = Arr::get(static::$lines[$module][$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.
*
* @param string $key
2011-08-03 22:10:07 -05:00
* @param string $language
2011-06-08 23:45:08 -05:00
* @return array
*/
2011-08-03 22:10:07 -05:00
private function parse($key, $language)
2011-06-08 23:45:08 -05:00
{
2011-08-18 19:56:29 -05:00
list($module, $key) = Module::parse($key);
2011-08-03 22:10:07 -05:00
2011-08-08 10:44:03 -05:00
if (count($segments = explode('.', $key)) > 1)
{
2011-08-18 19:56:29 -05:00
return array($module, $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.
*
2011-08-03 22:10:07 -05:00
* @param string $module
2011-06-08 23:45:08 -05:00
* @param string $file
* @param string $language
2011-08-18 23:23:57 -05:00
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-08-03 22:10:07 -05:00
private function load($module, $file, $language)
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
if (isset(static::$lines[$module][$language.$file])) return;
2011-08-18 23:23:57 -05:00
$lang = array();
foreach (array(LANG_PATH, Module::path($module).'lang/') as $directory)
2011-06-08 23:45:08 -05:00
{
2011-08-18 23:23:57 -05:00
$lang = (file_exists($path = $directory.$language.'/'.$file.EXT)) ? array_merge($lang, require $path) : $lang;
2011-06-08 23:45:08 -05:00
}
2011-08-18 23:23:57 -05:00
if (count($lang) > 0) static::$lines[$module][$language.$file] = $lang;
return isset(static::$lines[$module][$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.
*/
public function __toString()
{
return $this->get();
}
2011-06-08 23:45:08 -05:00
}