0

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.

3
  • 1
    Related: stackoverflow.com/a/2316375/476 Commented Feb 25, 2014 at 7:45
  • ok so better is buy 3 domains example com pl and ru and preparing website in three languages? Maybe this is solution, but which one version is friendly for seo Commented Feb 25, 2014 at 8:07
  • That's a different topic and depends on many factors. If you're something like Facebook where URLs hardly matter and it's all a user setting, not differentiating languages by URL is perfectly feasible. Otherwise example.com/en, example.com/ru etc. is a possibility as well. Different top level domains are particularly useful if you really want to appeal to a certain market and appearing "local" is important. That's completely independent of the question of detecting languages based on IP. Commented Feb 25, 2014 at 8:10

1 Answer 1

1

If you just want to "resort" the keys in your array - which I believe is your actual question, then you have to extract the key you want from the array and re-add it to the beginning of the list with array_unshift. You cannot 'sort' them in any classic way, as you're looking for a very specific order, not a standard alphabetical (or other sortable) method. You're prioritising associated keys.

Easiest way to do this is:

$lang = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
$selectedLang = $lang['en'];
unset($lang['en']);
array_unshift($lang,$selectedLang);
var_dump($lang);

If you want to apply that to your code example (I would change the if to a switch):

$this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
switch($query->countryCode) {
    case 'PL':          
      $selectedLang = $this->languages['pl'];
      unset($this->languages['pl']); 
      array_unshift($this->languages,$selectedLang);
      return $this->languages;
    break;   

    case 'RU':
      $selectedLang = $this->languages['ru'];
      unset($this->languages['ru']); 
      array_unshift($this->languages,$selectedLang);
      return $this->languages;
    break; 

     default:
      $selectedLang = $this->languages['en'];
      unset($this->languages['en']); 
      array_unshift($this->languages,$selectedLang);
      return $this->languages;
    break; 

}

Even better would be refactoring the code in a function and using your countrycode variable as an argument

function selectLang($langstring,$lang) {
  $selectedLang = $lang[$langstring];
  unset($lang[$langstring]);

  array_unshift($lang,$selectedLang);
  return $lang; 
}

$this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
return selectLang( strtolower ($query->countryCode), $this->languages),

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.