multilingual_seo.php
6.41 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/************************************
- GeekoDev SEO Package - multilingual handler -
*************************************
contact : sales@geekodev.com
************************************/
final class multilingual_seo extends Controller {
public function detect() {
$url_alias = version_compare(VERSION, '3', '>=') ? 'seo_url' : 'url_alias';
// prefix mode
if ($this->config->get('mlseo_store_mode')) {
$lang_to_store = $this->config->get('mlseo_lang_to_store');
foreach ($lang_to_store as $lang => $store) {
if (isset($this->session->data['language']) && isset($store['config_url']) && strpos($store['config_url'], $this->request->server['HTTP_HOST']) !== false) {
$this->session->data['language'] = $lang;
}
}
// suffix mode
} else if ($this->config->get('mlseo_flag')) {
if (empty($this->request->get['_route_'])) {
if (empty($this->request->get['route'])) {
if (!$this->config->get('mlseo_flag_detect')) {
// redirect if detected language (session, cookie, or browser detect) is not equal to default language
$detect = $this->detectLanguage();
if ($detect != $this->config->get('mlseo_default_lang')) {
if ($this->config->get('mlseo_flag_short')) {
$detect = substr($detect, 0, 2);
}
header('CSP-Redir: lang autodetect', false);
header('Location: '.$this->config->get('config_url') . $detect);
}
} else if (false) {
// detect but don't do anything
return;
} else {
// force default language if no tag
$this->session->data['language'] = $this->config->get('mlseo_default_lang');
}
}
return;
}
$parts = explode('/', $this->request->get['_route_']);
$lgCode = array_shift($parts);
$lgCodes = (array) $this->config->get('mlseo_lang_codes');
$customSeoFlag = $this->config->get('mlseo_flag_custom');
if (is_array($customSeoFlag)) {
$customSeoFlag = array_flip($customSeoFlag);
}
if (!empty($customSeoFlag[$lgCode])) {
$this->session->data['language'] = $customSeoFlag[$lgCode];
$this->request->get['_route_'] = implode('/', $parts);
} else {
if ($this->config->get('mlseo_flag_short')) {
$lgSearch = array_map(array($this, 'langCode'), $lgCodes);
} else {
$lgSearch = array_map(array($this, 'lowercaseCode'), $lgCodes);
}
if ($lgKey = array_search(strtolower($lgCode), $lgSearch)) {
$this->session->data['language'] = $lgCodes[$lgKey];
$this->request->get['_route_'] = implode('/', $parts);
} else {
if ($this->config->get('mlseo_flag_default') || ($this->config->get('mlseo_flag_detect') && !empty($this->request->get['route']))) {
$this->session->data['language'] = $this->config->get('mlseo_default_lang');
}
}
}
// multilingual
} else if($this->config->get('mlseo_ml_mode')) {
$get = array();
if (isset($this->request->get['_route_'])) {
$parts = explode('/', $this->request->get['_route_']);
foreach ($parts as $part) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . $url_alias . " WHERE keyword = '" . $this->db->escape($part) . "'");
if ($query->num_rows) {
$url = explode('=', $query->row['query']);
if ($url[0] == 'product_id') {
$get['product_id'] = $url[1];
}
if ($url[0] == 'category_id') {
if (!isset($this->request->get['path'])) {
$get['path'] = $url[1];
} else {
$get['path'] .= '_' . $url[1];
}
}
if ($url[0] == 'manufacturer_id') {
$get['manufacturer_id'] = $url[1];
}
if ($url[0] == 'information_id') {
$get['information_id'] = $url[1];
}
} else {
$get['route'] = 'error/not_found';
}
}
if (isset($get['product_id'])) {
$get['route'] = 'product/product';
} elseif (isset($get['path'])) {
$get['route'] = 'product/category';
} elseif (isset($get['manufacturer_id'])) {
$get['route'] = 'product/manufacturer/product';
} elseif (isset($get['information_id'])) {
$get['route'] = 'information/information';
}
if (isset($get['route'])) {
if (isset($query->row['language_id']) && ($query->row['language_id'] != 0) && (count($query->rows) === 1)) {
$lgCodes = (array) $this->config->get('mlseo_lang_codes');
if (isset($lgCodes[$query->row['language_id']])) {
$this->session->data['language'] = $lgCodes[$query->row['language_id']];
}
}
}
}
}
}
private function detectLanguage() {
$code = '';
$this->load->model('localisation/language');
$languages = $this->model_localisation_language->getLanguages();
if (isset($this->session->data['language'])) {
$code = $this->session->data['language'];
}
if (isset($this->request->cookie['language']) && !array_key_exists($code, $languages)) {
$code = $this->request->cookie['language'];
}
// Language Detection
if (!empty($this->request->server['HTTP_ACCEPT_LANGUAGE']) && !array_key_exists($code, $languages)) {
$detect = '';
$browser_languages = explode(',', $this->request->server['HTTP_ACCEPT_LANGUAGE']);
// Try using local to detect the language
foreach ($browser_languages as $browser_language) {
foreach ($languages as $key => $value) {
if ($value['status']) {
$locale = explode(',', $value['locale']);
if (in_array($browser_language, $locale)) {
$detect = $key;
break 2;
}
}
}
}
if (!$detect) {
// Try using language folder to detect the language
foreach ($browser_languages as $browser_language) {
if (array_key_exists(strtolower($browser_language), $languages)) {
$detect = strtolower($browser_language);
break;
}
}
}
$code = $detect ? $detect : '';
}
if (!array_key_exists($code, $languages)) {
$code = $this->config->get('config_language');
}
return $code;
}
private function langCode($code) {
$code = substr($code, 0, 2);
return strtolower($code);
}
private function lowercaseCode($code) {
return strtolower($code);
}
}