piler/webui/system/language.php

84 lines
1.6 KiB
PHP
Raw Normal View History

2012-02-08 23:14:28 +01:00
<?php
class Language {
public $data = array();
public function __construct () {
global $langs;
$lang = '';
2013-03-17 21:43:40 +01:00
if(isset($_SESSION['lang']) && $_SESSION['lang'] != '') {
2012-02-08 23:14:28 +01:00
$file = DIR_LANGUAGE . $_SESSION['lang'] . '/messages.php';
2012-09-21 20:26:49 +02:00
define('LANG', $_SESSION['lang']);
2012-02-08 23:14:28 +01:00
}
else {
$pref_langs = $this->get_preferred_languages();
while(list($k, $v) = each($pref_langs)) {
if(in_array($v, $langs)) {
$lang = $v;
define('LANG', $lang);
break;
}
}
if($lang == '') { $lang = DEFAULT_LANG; }
2012-02-08 23:14:28 +01:00
$_SESSION['lang'] = $lang;
$file = DIR_LANGUAGE . $lang . '/messages.php';
}
if(file_exists($file)) {
2012-02-08 23:14:28 +01:00
$_ = array();
require($file);
$this->data = array_merge($this->data, $_);
}
else {
exit('Error: Could not load language ' . $file . '!');
}
}
private function get_preferred_languages() {
$langs = array();
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$l = explode(";", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
while(list($k, $v) = each($l)) {
$a = explode(",", $v);
if(isset($a[0]) && substr($a[0], 0, 2) != 'q=') {
array_push($langs, $a[0]);
}
if(isset($a[1])) { array_push($langs, $a[1]); }
}
}
array_push($langs, DEFAULT_LANG);
return $langs;
}
2012-02-08 23:14:28 +01:00
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}
}
?>