/** * Creates the language prefixes wich allows us * to use the short :action and :controller translations * * @param string $action * @return void */ protected function set_language_alias($action) { // create langugage aliases $name = explode('::', $this->name); if (isset($name[1])) { $prefix = $name[0] . '::'; $name = $name[1]; } else { $prefix = ''; $name = $name[0]; } if (empty($action)) { $action = static::$_default_action; } CCLang::alias(':controller', $prefix . 'controller/' . strtolower($name)); CCLang::alias(':action', $prefix . 'controller/' . strtolower($name . '.' . $action)); }
/** * Recive a translated line * * __( 'some/path.to.my.label' ); * __( 'user.welcome', array( 'name' => 'Jeff' ) ) * * @param string $key * @param array $params * @return string */ public static function line($key, $params = array()) { $path = substr($key, 0, strpos($key, '.')); $key = substr($key, strpos($key, '.') + 1); // if there is a namespace replace the path with it if (isset(static::$aliases[$path])) { return static::line(static::$aliases[$path] . '.' . $key, $params); } // find the language file behind the path if (!isset(static::$data[static::$current_language][$path])) { // Autoload the language file // The load function will throw an exception if the // file doesnt exists so we dont have to care for that here. CCLang::load($path); } // Does the line exist in the language file? if (!isset(static::$data[static::$current_language][$path][$key])) { // We simply return the key to the user and log the missing language file line CCLog::add('CCLang::line - No such line "' . $key . '" (' . static::$current_language . ') in file: ' . $path, 'warning'); return $key; } $line = static::$data[static::$current_language][$path][$key]; // replace the params inside the line foreach ($params as $param => $value) { $line = str_replace(':' . $param, $value, $line); } return $line; }
/** * CCLang::alias tests */ public function test_alias() { CCLang::set_current('en'); CCLang::alias(':test', 'CCUnit::phpunit'); $this->assertEquals('Welcome', __(':test.welcome')); $this->assertEquals('Hello John', __(':test.hello', array('name' => 'John'))); }
/** * get the client data * * @param string $key * @return mixed */ public function client($key = null) { // check if we already set the client object if (is_null($this->client)) { // make client $this->client = new \stdClass(); /* * get clients ip address */ // Cloudlfare fix if ($this->has_server('HTTP_CF_CONNECTING_IP')) { $this->client->ip = $this->server('HTTP_CF_CONNECTING_IP'); } elseif ($this->has_server('HTTP_X_FORWARDED_FOR')) { $this->client->ip = $this->server('HTTP_X_FORWARDED_FOR'); } elseif ($this->has_server('HTTP_CLIENT_IP')) { $this->client->ip = $this->server('HTTP_CLIENT_IP'); } else { $this->client->ip = $this->server('REMOTE_ADDR', '127.0.0.1'); } /* * set clients user agent */ $this->client->agent = $this->server('HTTP_USER_AGENT', ''); /* * set clients port */ $this->client->port = $this->server('REMOTE_PORT', ''); /* * set clients fingerprint based on host and agent */ $this->client->fingerprint = CCStr::hash($this->client('agent') . $this->client('ip')); /* * set clients language */ $this->client->language = CCLang::set_current($this->server('HTTP_ACCEPT_LANGUAGE')); } // return the object if (is_null($key)) { return $this->client; } // return a special key return $this->client->{$key}; }
function __($key, $params = array()) { return CCLang::line($key, $params); }
/** * Some default values for our session * * @return array */ public static function default_data_provider() { return array('last_active' => time(), 'current_lang' => \CCLang::current(), 'client_agent' => \CCServer::client('agent'), 'client_ip' => \CCServer::client('ip'), 'client_port' => \CCServer::client('port'), 'client_lang' => \CCServer::client('language')); }