123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php namespace Laravel; use Closure;
- class Lang {
-
- protected static $lines = array();
-
- protected $key;
-
- protected $replacements;
-
- protected $language;
-
- protected $paths;
-
- protected function __construct($key, $replacements = array(), $language = null, $paths = array())
- {
- $this->key = $key;
- $this->paths = $paths;
- $this->language = $language;
- $this->replacements = $replacements;
- }
-
- public static function line($key, $replacements = array(), $language = null, $paths = array())
- {
- if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH);
- return new static($key, $replacements, $language, $paths);
- }
-
- public function get($language = null, $default = null)
- {
-
- if ( ! is_null($language)) $this->language = $language;
- list($file, $line) = $this->parse($this->key);
- if ( ! $this->load($file))
- {
- return ($default instanceof Closure) ? call_user_func($default) : $default;
- }
- $line = Arr::get(static::$lines[$this->language.$file], $line, $default);
- foreach ($this->replacements as $key => $value)
- {
- $line = str_replace(':'.$key, $value, $line);
- }
- return $line;
- }
-
- protected function parse($key)
- {
- if (count($segments = explode('.', $key)) > 1)
- {
- return array($segments[0], implode('.', array_slice($segments, 1)));
- }
- throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
- }
-
- protected function load($file)
- {
- if (isset(static::$lines[$this->language.$file])) return;
- $language = array();
-
-
-
- foreach ($this->paths as $directory)
- {
- if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
- {
- $language = array_merge($language, require $path);
- }
- }
-
-
-
- if (count($language) > 0) static::$lines[$this->language.$file] = $language;
-
- return isset(static::$lines[$this->language.$file]);
- }
-
- public function __toString() { return $this->get(); }
- }
|