123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?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 = '')
- {
- if (is_null($language))
- {
- $language = Config::get('application.language');
- }
- list($file, $line) = $this->parse($this->key);
- $this->load($file, $language);
- $line = Arr::get(static::$lines[$language.$file], $line, $default);
- foreach ($this->replacements as $key => $value)
- {
- $line = str_replace(':'.$key, $value, $line);
- }
- return $line;
- }
-
- private function parse($key)
- {
- $segments = explode('.', $key);
- if (count($segments) < 2)
- {
- throw new \Exception("Invalid language key [$key].");
- }
- return array($segments[0], implode('.', array_slice($segments, 1)));
- }
-
- private function load($file, $language)
- {
- if (array_key_exists($language.$file, static::$lines)) return;
- if (file_exists($path = LANG_PATH.$language.'/'.$file.EXT))
- {
- static::$lines[$language.$file] = require $path;
- }
- }
-
- public function __toString()
- {
- return $this->get();
- }
- }
|