123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php namespace Laravel; use Closure;
- class Lang {
-
- protected $key;
-
- protected $replacements;
-
- protected $language;
-
- protected static $lines = array();
-
- const loader = 'laravel.language.loader';
-
- protected function __construct($key, $replacements = array(), $language = null)
- {
- $this->key = $key;
- $this->language = $language;
- $this->replacements = (array) $replacements;
- }
-
- public static function line($key, $replacements = array(), $language = null)
- {
- if (is_null($language)) $language = Config::get('application.language');
- return new static($key, $replacements, $language);
- }
-
- public static function has($key, $language = null)
- {
- return static::line($key, array(), $language)->get() !== $key;
- }
-
- public function get($language = null, $default = null)
- {
-
-
-
- if (is_null($default)) $default = $this->key;
- if (is_null($language)) $language = $this->language;
- list($bundle, $file, $line) = $this->parse($this->key);
-
-
-
- if ( ! static::load($bundle, $language, $file))
- {
- return value($default);
- }
- $lines = static::$lines[$bundle][$language][$file];
- $line = array_get($lines, $line, $default);
-
-
-
- if (is_string($line))
- {
- foreach ($this->replacements as $key => $value)
- {
- $line = str_replace(':'.$key, $value, $line);
- }
- }
- return $line;
- }
-
- protected function parse($key)
- {
- $bundle = Bundle::name($key);
- $segments = explode('.', Bundle::element($key));
-
-
-
- if (count($segments) >= 2)
- {
- $line = implode('.', array_slice($segments, 1));
- return array($bundle, $segments[0], $line);
- }
- else
- {
- return array($bundle, $segments[0], null);
- }
- }
-
- public static function load($bundle, $language, $file)
- {
- if (isset(static::$lines[$bundle][$language][$file]))
- {
- return true;
- }
-
-
-
- $lines = Event::first(static::loader, func_get_args());
- static::$lines[$bundle][$language][$file] = $lines;
- return count($lines) > 0;
- }
-
- public static function file($bundle, $language, $file)
- {
- $lines = array();
-
-
-
- $path = static::path($bundle, $language, $file);
- if (file_exists($path))
- {
- $lines = require $path;
- }
- return $lines;
- }
-
- protected static function path($bundle, $language, $file)
- {
- return Bundle::path($bundle)."language/{$language}/{$file}".EXT;
- }
-
- public function __toString()
- {
- return (string) $this->get();
- }
- }
|