Jump to content

Select language script


regoch

Recommended Posts

I have this code for change language on my page. But now I'm trying to add to this script to check ip address and get country code and see if there is language for this country code,  if not select english language. I put hr in $ip variable, just for test, but there will be visitors country code getting it from script. Anybody have any idea? language index goes www.site.hr/hr, www.site.hr/en etc

<?php
function pronadiJezik() {

	$jezici = array('hr'=>'Hrvatski', 'en'=>'Engleski', 
	  'de'=>'Njemački', 'it'=>'Talijanski', 'fr'=>'Francuski', 'cz'=>'Češki', 'pl'=>'Poljski');


	if(isset($_SERVER["REDIRECT_URL"]) && $_SERVER["REDIRECT_URL"] != "/") {
		$url = $_SERVER["REDIRECT_URL"];
	} else if(isset($_SERVER["REDIRECT_URI"]) && $_SERVER["REDIRECT_URI"] != "/") {
		$url = $_SERVER["REQUEST_URI"];
	} else {
		$url = "";
	}



	$jeziciA = array_keys($jezici);

	$url_parts = explode("/", $url);


	if(is_array($url_parts)) {

		foreach($url_parts as $n=>$v) {

			if(trim($v)!="") {

				if(in_array($v, $jeziciA)) {

					$jezik = $v;	

				}  

			} // end if $v!=""

		} // end foreach

	} // end if is_array

return $jezik;

}
//----------------------------------------------------------
session_start();
$set_period = (60*60*24);

$ip="hr";
$ip = strtolower($ip);

if($_GET['lang']) {
$_SESSION['jezik'] = $_GET['lang'];
setcookie("jezik", $_GET['lang'], time()+ $set_period); 
} else {
$_SESSION['jezik'] = pronadiJezik();
setcookie("jezik", $_SESSION['lang'], time()+ $set_period); 
} 

if(!$_SESSION['jezik'] or $_SESSION['jezik'] == "") {
$_SESSION['jezik'] = "en";
setcookie("jezik", "en", time() + $set_period); 
}
/////////////////////////////////////////////////////////////////////////HR
if($_SESSION['jezik'] == "hr") {
include "include/jezici/hr.php";
}
/////////////////////////////////////////////////////////////////////////EN
elseif($_SESSION['jezik'] == "en") {
include "include/jezici/en.php";
}
/////////////////////////////////////////////////////////////////////////DE
elseif($_SESSION['jezik'] == "de") {
include "include/jezici/de.php";
}
/////////////////////////////////////////////////////////////////////////IT
elseif($_SESSION['jezik'] == "it") {
include "include/jezici/it.php";
}
/////////////////////////////////////////////////////////////////////////FR
elseif($_SESSION['jezik'] == "fr") {
include "include/jezici/fr.php";
}
/////////////////////////////////////////////////////////////////////////CZ
elseif($_SESSION['jezik'] == "cz") {
include "include/jezici/cz.php";
}
/////////////////////////////////////////////////////////////////////////CZ
elseif($_SESSION['jezik'] == "pl") {
include "include/jezici/pl.php";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/250804-select-language-script/
Share on other sites

solved it with this, beginners mistake!

$ip = strtolower($ip);
$jezici = array("hr", "en", "de", "it", "fr", "cz", "pl");

    if (in_array($ip, $jezici)) {
        if(!$_SESSION['jezik'] or $_SESSION['jezik'] == "") {
	$_SESSION['jezik'] = $ip;
	setcookie("jezik", $ip, time() + $set_period); 
	}
    } else {       
	if(!$_SESSION['jezik'] or $_SESSION['jezik'] == "") {
	$_SESSION['jezik'] = "en";
	setcookie("jezik", "en", time() + $set_period); 
	}	
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.