<?php
class LangClass {
var $connection;
public function get_lang() {
$user_lang = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$http_lang_str = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$http_lang_array = explode(",",$http_lang_str);
$tag_lang_array = explode("-",$http_lang_array[0]);
}
if (isset($_COOKIE['lang_pref'])) {
$user_lang[] = $_COOKIE['lang_pref'];
} elseif (isset($http_lang_array)) {
$user_lang[] = $http_lang_array[0];
} else {
$user_lang[] = "en";
}
$primary_lang = explode("-",$user_lang[0]);
if (!isset($tag_lang_array[0])) {
$user_lang[] = $user_lang[0];
} elseif ($tag_lang_array[0] == $primary_lang[0]) {
if (isset($tag_lang_array[1])) {
$user_lang[] = $tag_lang_array[0]."-".strtoupper($tag_lang_array[1]);
} else {
$user_lang[] = $tag_lang_array[0];
}
} else {
$user_lang[] = $user_lang[0];
}
return $user_lang;
}
function load_db($db) {
$this->connection = mysql_connect($db['host'], $db['user'], $db['pass'], "SET NAMES 'utf8'") or die ("Unable to connect!");
mysql_select_db($db['db_lang'], $this->connection) or die ("Unable to select database!");
$utf8 = mysql_query("SET NAMES UTF8");
return $this->connection;
}
public function load_lang($lang, $page) {
$lang = mysql_real_escape_string($lang, $this->connection);
$query = mysql_query("SELECT * FROM `$page` WHERE `lang`='$lang'", $this->connection);
if (mysql_num_rows($query) == 0) {
$lang_array = explode("-",$lang);
$lang = $lang_array[0];
$query = mysql_query("SELECT * FROM `$page` WHERE `lang`='$lang'", $this->connection);
if (mysql_num_rows($query) == 0) {
$query = mysql_query("SELECT * FROM `$page` WHERE `lang`='en'", $this->connection) or die("Unable to load lang set: en");
}
}
$lang_array = mysql_fetch_array($query);
return $lang_array;
}
public function close_db() {
mysql_close($this->connection);
}
}
?>