Jump to content

multi languege


ctcp

Recommended Posts

common.php


<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.de.php';
  break;

  case 'el':
  $lang_file = 'lang.el.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>

ip2locationlite.class.php

<?php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = '';

public function __construct(){}

public function __destruct(){}

public function setKey($key){
	if(!empty($key)) $this->apiKey = $key;
}

public function getError(){
	return implode("\n", $this->errors);
}

public function getCountry($host){
	return $this->getResult($host, 'ip-country');
}

public function getCity($host){
	return $this->getResult($host, 'ip-city');
}

private function getResult($host, $name){
	$ip = @gethostbyname($host);

	if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
		$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

		try{
			$response = @new SimpleXMLElement($xml);

			foreach($response as $field=>$value){
				$result[(string)$field] = (string)$value;
			}

			return $result;
		}
		catch(Exception $e){
			$this->errors[] = $e->getMessage();
			return;
		}
	}

	$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
	return;
}
}
?>

 

 

 

index.php

 

<?php
include_once('ip2locationlite.class.php');

//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  $ipLite = new ip2location_lite;
  $ipLite->setKey('*****************');

  $visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
  if ($visitorGeolocation['statusCode'] == 'OK') {
    $data = base64_encode(serialize($visitorGeolocation));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
  }
}else{
  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
}
var_dump($visitorGeolocation);
?>


im using this http://ipinfodb.com/

in index.php i got this message 

array(5) { ["statusCode"]=> string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(13) "94.68.211.211" ["countryCode"]=> string(2) "GR" ["countryName"]=> string(6) "GREECE" }

how to switch automatic if GR go to index.php?lang=el

if DE go to  index.php?lang=de etc...

 

 

 

Link to comment
Share on other sites

i don't have any error evrythink is fine i whant just combine common/php + this

 

<?php
include_once('ip2locationlite.class.php');

//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  $ipLite = new ip2location_lite;
  $ipLite->setKey('*****************');

  $visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
  if ($visitorGeolocation['statusCode'] == 'OK') {
    $data = base64_encode(serialize($visitorGeolocation));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
  }
}else{
  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
}
var_dump($visitorGeolocation);
?>

 

i whant automatical redirect. to index.php?lang=el or de or en ...

 

 

Link to comment
Share on other sites

Forum Rules:

Do not bump a thread if it is still on the first page

 

Well, YOU need to determine what the trigger or triggers will be. You can't just redirect them without some sort of specific rule - otherwise the redirect will be caught in a loop.

 

Do you only want to redirect if the language isn't set in the session value in the cookie value, what? If you have a means of allowing the user to select their language you don't want to override their selection based upon their location. So, you need to come up with the logic that makes sense for your implementation, then we can provide some guidance on code. But, for the sake of argument, you could simply add another ifelse()

if(isSet($_GET['lang']))
{
    $lang = $_GET['lang'];
    // register the session and set the cookie
    $_SESSION['lang'] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
    $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
    $lang = $_COOKIE['lang'];
}
elseif(isset($_COOKIE['geolocation']))
{
    $lang = $_COOKIE['geolocation'];
}
else
{
    $lang = 'en';
}

Link to comment
Share on other sites

thank you for your time mate can you explane pls how to get GR only?

 

<?php
include_once('ip2locationlite.class.php');

//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  $ipLite = new ip2location_lite;
  $ipLite->setKey('*********************');

  $visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
  if ($visitorGeolocation['statusCode'] == 'OK') {
    $data = base64_encode(serialize($visitorGeolocation));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
  }
}else{
  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
}
var_dump($visitorGeolocation);
?>

 

here i get this message

 

array(5) { ["statusCode"]=> string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(13) "94.68.211.211" ["countryCode"]=> string(2) "GR" ["countryName"]=> string(6) "GREECE" }

 

 

how to get  GR only?

 

 

Link to comment
Share on other sites

It's right there in the array under the key "countryCode". Use that value in your code to determine the language to use based upon country code. Also, you should change your switch() to convert the value to lowercase because "GR" != "gr"

 

switch (strtolower($lang)) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.