<?php
class language{
public function __construct($defaultlang){
$this->default = $defaultlang;
if(!$this->exists($defaultlang)){
die('Can\'t find default language!');
}
}
public function load($lang){
for($i = 0; $i<= $this->getlangs(0, 0 , 2) - 1; $i++){
if($lang == $this->getlangs($i, 1, 1)){
$lang = $this->getlangs($i, 0 , 1).'-'.$this->getlangs($i, 1 , 1);
}
}
$this->lang = $lang;
$default = $this->default;
$langpath = 'languages/'.$lang;
if(!file_exists($langpath.'/index.php')){
$lang = $default;
$langpath = 'languages/'.$lang;
}
if(file_exists($langpath.'/header.php') && file_exists($langpath.'/content.php')){
include_once $langpath.'/header.php';
include_once $langpath.'/content.php';
}
else{
setcookie('lang', $default); // If language it's deleted or can't load it's files, try to use default language
die('Can\'t load language files!');
}
}
public function check(){
if(isset($_COOKIE['lang'])){
return strtolower($_COOKIE['lang']);
}
else{
return $this->default;
}
}
// Get language form options and show first the current language
public function language_options(){
$return = '';
// Return first language(Language that you use now)
for($i = 0; $i<= $this->getlangs(0, 0, 2) - 1; $i++){
if($this->getlangs($i, 1, 1) == $this->check()){
$return .= '<option value="'.$this->getlangs($i, 1, 1).'">'.$this->getlangs($i, 0 , 1).'</option>';
}
}
// Return the other languages(That you can select)
for($i = 0; $i<= $this->getlangs(0, 0, 2) - 1; $i++){
if($this->getlangs($i, 1, 1) != $this->check()){
$return .= '<option value="'.$this->getlangs($i, 1, 1).'">'.$this->getlangs($i, 0 , 1).'</option>';
}
}
return $return;
}
public function getlangs($y, $z, $t){
$langs = array();
$dir = "languages/";
$list = @opendir($dir);
$i = 0;
$s = 0;
while ($file = readdir($list))
{
if(is_dir($dir.'/'.$file) && $file != '.' && $file != '..'){
if($i <= $s){
$s = $i;
}
else{
$s++;
}
$name = explode('-', $file);
if(isset($name[0]) && isset($name[1])){
$langs[$s - 1][0] = $name[0];
$langs[$s - 1][1] = $name[1];
}
else{
die('Can\'t load languages ! Make sure that language folder name it\'s in this format: \' LangName-LangTag \' . Ex: English-en');
}
}
$i++;
}
closedir($list);
switch($t){
case 1 : return $langs[$y][$z]; break;
case 2 : return count($langs); break;
default : return $langs[$y][$z];
}
}
public function exists($l){
for($i = 0; $i<= $this->getlangs(0, 0 , 2) - 1; $i++){
if($l == $this->getlangs($i, 1, 1)){
return true;
}
}
}
}
?>
|