Files
spa-api/system/lang.php

153 lines
3.4 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
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-07-12 20:04:14 -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-03 22:10:07 -05:00
$this->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][$line]))
2011-08-02 20:20:17 -05:00
{
return is_callable($default) ? call_user_func($default) : $default;
}
2011-08-03 22:10:07 -05:00
$line = static::$lines[$module][$language.$file][$line];
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.
*
2011-07-07 23:48:05 -05:00
* The value on the left side of the dot is the language file name,
* while the right side of the dot is the item within that file.
*
2011-06-08 23:45:08 -05:00
* @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-03 22:10:07 -05:00
// Check for a module qualifier. If a module name is present, we need to extract it from
// the language line, otherwise, we will use "application" as the module.
$module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
// If the language line is stored in a module, we need to strip the module qualifier
// off of the language key before continuing.
if ($module != 'application')
{
$key = substr($key, strpos($key, ':') + 2);
}
2011-06-08 23:45:08 -05:00
$segments = explode('.', $key);
2011-08-03 22:10:07 -05:00
if (count($segments) > 1)
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
return array($module, $segments[0], $segments[1]);
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
* @return void
*/
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 the language lines for the given module, file, and language have already been
// loaded, we can bail out of this method.
if (isset(static::$lines[$module][$language.$file])) return;
$path = ($module === 'application') ? LANG_PATH : MODULE_PATH.$module.'/lang/';
2011-07-26 23:23:34 -05:00
2011-08-03 22:10:07 -05:00
if (file_exists($path = $path.$language.'/'.$file.EXT))
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
static::$lines[$module][$language.$file] = require $path;
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
}