123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php namespace System;
- class Lang {
-
- public static $lines = array();
-
- public $key;
-
- public $replacements = array();
-
- public function __construct($key, $replacements = array())
- {
- $this->key = $key;
- $this->replacements = $replacements;
- }
-
- public static function line($key, $replacements = array())
- {
- return new static($key, $replacements);
- }
-
- public function get($language = null, $default = null)
- {
- if (is_null($language))
- {
- $language = Config::get('application.language');
- }
- list($module, $file, $line) = $this->parse($this->key, $language);
- $this->load($module, $file, $language);
- if ( ! isset(static::$lines[$module][$language.$file][$line]))
- {
- return is_callable($default) ? call_user_func($default) : $default;
- }
- $line = static::$lines[$module][$language.$file][$line];
- foreach ($this->replacements as $key => $value)
- {
- $line = str_replace(':'.$key, $value, $line);
- }
- return $line;
- }
-
- private function parse($key, $language)
- {
- $module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
- if ($module != 'application')
- {
- $key = substr($key, strpos($key, ':') + 2);
- }
- if (count($segments = explode('.', $key)) > 1)
- {
- return array($module, $segments[0], $segments[1]);
- }
- throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
- }
-
- private function load($module, $file, $language)
- {
- if (isset(static::$lines[$module][$language.$file])) return;
- $path = ($module === 'application') ? LANG_PATH : MODULE_PATH.$module.'/lang/';
- if (file_exists($path = $path.$language.'/'.$file.EXT))
- {
- static::$lines[$module][$language.$file] = require $path;
- }
- }
-
- public function __toString()
- {
- return $this->get();
- }
- }
|