I need your help, I trying write script in oop php to change and set current languages. At this moment I have a part for changing lang and to get your IP check where you are. Now I need a part to set current lang and this is my nightmare. This is my first step in OOP and I don't have any idea how I can do that. This is my script:
<?php
class IP{
static $fields = 24578; //65535; // refer to http://ip-api.com/docs/api:returned_values#field_generator
static $use_xcache = false; // set this to false unless you have XCache installed (http://xcache.lighttpd.net/)
static $api = "http://ip-api.com/php/";
public $status, $country, $countryCode, $region, $regionName, $city, $zip, $lat, $lon, $timezone, $isp, $org, $as, $reverse, $query, $message;
public static function query($q){
$data = self::communicate($q);
$result = new self;
foreach($data as $key => $val){
$result->$key = $val;
}
return $result;
}
private static function communicate($q){
$q_hash = md5('ipapi'.$q);
if(self::$use_xcache && xcache_isset($q_hash)){
return xcache_get($q_hash);
}
if(is_callable('curl_init')){
$c = curl_init();
curl_setopt($c, CURLOPT_URL, self::$api.$q.'?fields='.self::$fields);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_TIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result_array = unserialize(curl_exec($c));
curl_close($c);
} else{
$result_array = unserialize(file_get_contents(self::$api.$q.'?fields='.self::$fields));
}
if(self::$use_xcache){
xcache_set($q_hash, $result_array, 86400);
}
return $result_array;
}
public $client_ip;
public function getIP(){
return "173.194.78.94";
/*
if (getenv("HTTP_CLIENT_IP")) $this->client_ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR")) $this->client_ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR")) $this->client_ip = getenv("REMOTE_ADDR");
else $this->ip = "UNKNOWN";
return $this->client_ip;
*/
}
}
class Lang{
private $languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
public function showIP(){
$query = IP::query('');
echo 'Country code: '.$query->countryCode.' your ip: '.$query->query;
if($query->countryCode == 'PL'){
$this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
return $this->languages;
}
elseif($query->countryCode == 'RU' || $query->countryCode == 'BY' || $query->countryCode == 'KZ' || $query->countryCode == 'KG'){
$this->languages = array('ru' => 'Russian', 'en' => 'English', 'pl' => 'Polish');
return $this->languages;
}
else{
$this->languages = array('en' => 'English', 'ru' => 'Russian', 'pl' => 'Polish');
return $this->languages;
}
}
private $current_language = false;
private $lines = array();
private static $instance = false;
public function __construct(){
$this->set_language();
}
public static function instance(){
if(self::$instance == FALSE){self::$instance = new Lang;}
return self::$instance;
}
private function set_language(){
if(isset($_GET['lang']) AND array_key_exists(($lang = strtolower($_GET['lang'])), $this->languages)){
$this->current_language = $lang;
}
else{
$this->current_language = $this->default_language();
}
$this->load_lang_file();
}
private function load_lang_file(){
if(file_exists('langs/'.$this->current_language.'.php')){
include 'langs/'.$this->current_language.'.php';
$this->lines = $lang;
}
}
private function default_language(){
return current(array_keys($this->languages));
}
public static function line($name = FALSE, $params = array()){
if(isset(self::instance()->lines[$name]))
return vsprintf(self::instance()->lines[$name], $params);
return FALSE;
}
public static function get_language(){
return self::instance()->current_language;
}
public static function get_languages(){
return self::instance()->languages;
}
}
function __($name = FALSE, $params = array()){
return Lang::line($name, $params);
}
?>
I have a problem with this part:
private $languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
public function showIP(){
$query = IP::query('');
echo 'Country code: '.$query->countryCode.' your ip: '.$query->query;
if($query->countryCode == 'PL'){
$this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
return $this->languages;
}
elseif($query->countryCode == 'RU' || $query->countryCode == 'BY' || $query->countryCode == 'KZ' || $query->countryCode == 'KG'){
$this->languages = array('ru' => 'Russian', 'en' => 'English', 'pl' => 'Polish');
return $this->languages;
}
else{
$this->languages = array('en' => 'English', 'ru' => 'Russian', 'pl' => 'Polish');
return $this->languages;
}
}
I try change keys in array, example now this working like that: if first element in array is english so you current lang is english, if first element is polish so your current lang in this website is polish. I try change that and add geolocalization, but I dont have idea how I can change sequence in array or maybe is a simpler way.