key = $key; $this->paths = $paths; $this->language = $language; $this->replacements = $replacements; } /** * Create a new language line instance. * * @param string $key * @param array $replacements * @param string $language * @param array $paths * @return Lang */ 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); } /** * Get the language line. * * @param string $language * @param string $default * @return string */ 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; } /** * Parse a language key. * * @param string $key * @return array */ 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."); } /** * Load a language file. * * @param string $file * @return bool */ 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]); } /** * Get the string content of the language line. */ public function __toString() { return $this->get(); } }