-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.php
140 lines (124 loc) · 4.75 KB
/
language.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*****************************************************************************
*
* Language Class
* ---------------
*
* User language detection and handling class
*
* Copyright (C) 2009 De Smet Nicolas (<http://ndesmet.be>).
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
class zyfra_language{
private $default = 'en';
protected $languages;
protected function get_all_languages(){
//Need to be override by your own method.
$this->languages = [];
$i = 1;
$this->languages['en'] = (object)array('id'=>$i++, 'name'=>'english', url=>'/en/');
$this->languages['fr'] = (object)array('id'=>$i++, 'name'=>'francais', url=>'/fr/');
$this->languages['nl'] = (object)array('id'=>$i++, 'name'=>'nederlands', url=>'/nl/');
$this->languages['de'] = (object)array('id'=>$i++, 'name'=>'deutsch', url=>'/de/');
}
protected function get_cookie(){
//To be override
return '';
}
public function get_languages(){
if (!is_array($this->languages)) $this->get_all_languages();
return $this->languages;
}
public function redirect_if_found(){
$this->default = false;
$lang = $this->auto_detect();
if ($lang === false) return $lang;
if (!isset($this->languages[$lang]->url)) return $lang;
header("location:".$this->languages[$lang]->url);
exit();
}
public function auto_detect(){
if (($lang = $this->check_get()) !== false) return $lang;
if (($lang = $this->check_post()) !== false) return $lang;
if (($lang = $this->check_session()) !== false) return $lang;
if (($lang = $this->check_cookie()) !== false) return $lang;
if (($lang = $this->check_navigator()) !== false) return $lang;
//Really no hint...
return $this->default;
}
public function set_default($abv){
$abv = strtolower($abv);
if ($this->is_a_language($abv)) $this->default = $abv;
}
public function is_a_language($abv){
$abv = strtolower($abv);
if (!is_array($this->languages)) $this->get_all_languages();
return isset($this->languages[strtolower($abv)]);
}
public function get_id($abv){
if(!$this->is_a_language($abv)) $this->languages[$this->default]->id;
return $this->languages[strtolower($abv)]->id;
}
public function get_name($abv){
if(!$this->is_a_language($abv)) $this->languages[$this->default]->name;
return $this->languages[strtolower($abv)]->name;
}
public function get_correct_abv($abv){
if (trim($abv)=='') return false;
if($this->is_a_language($abv)){
return strtolower($abv);
}else{
return $this->default;
}
}
private function check_get(){
if(isset($_GET['lang']))
return $this->get_correct_abv($_GET['lang']);
return false;
}
private function check_post(){
if(isset($_POST['lang']))
return $this->get_correct_abv($_POST['lang']);
return false;
}
private function check_session(){
if(isset($_SESSION['lang']))
return $this->get_correct_abv($_SESSION['lang']);
return false;
}
private function check_cookie(){
return $this->get_correct_abv($this->get_cookie());
}
private function check_navigator(){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$accept_languages = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($accept_languages as $accept_language){
$all_languages = explode(';',$accept_language);
$language = array_shift($all_languages);
preg_match('/([A-Za-z]*)-/', $accept_language, $match, 0, 0);
if (isset($match[1])){
$abv = $match[1];
}else{
$abv = $language;
}
if($this->is_a_language($abv))
return $this->get_correct_abv($abv);
}
}
return false;
}
}