|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Language detector |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\langdetect; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\Module, |
|
13
|
|
|
gplcart\core\Config; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Main class for Language detector module |
|
17
|
|
|
*/ |
|
18
|
|
|
class LangDetect extends Module |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Config $config |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Config $config) |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct($config); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Implements hook "route.list" |
|
31
|
|
|
* @param array $routes |
|
32
|
|
|
*/ |
|
33
|
|
|
public function hookRouteList(array &$routes) |
|
34
|
|
|
{ |
|
35
|
|
|
$routes['admin/module/settings/langdetect'] = array( |
|
36
|
|
|
'access' => 'module_edit', |
|
37
|
|
|
'handlers' => array( |
|
38
|
|
|
'controller' => array('gplcart\\modules\\langdetect\\controllers\\Settings', 'editSettings') |
|
39
|
|
|
) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Implements hook "language.set.before" |
|
45
|
|
|
* @param string $langcode |
|
46
|
|
|
*/ |
|
47
|
|
|
public function hookLanguageSetBefore($langcode) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->setDetectedLanguage($langcode); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets detected language |
|
54
|
|
|
* @param string $langcode |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function setDetectedLanguage($langcode) |
|
57
|
|
|
{ |
|
58
|
|
|
$settings = $this->config->getFromModule('langdetect'); |
|
59
|
|
|
|
|
60
|
|
|
if (empty($settings['redirect'])) { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/* @var $session_helper \gplcart\core\helpers\Session */ |
|
65
|
|
|
$session_helper = $this->getHelper('Session'); |
|
66
|
|
|
$saved = $session_helper->get('langdetect'); |
|
67
|
|
|
|
|
68
|
|
|
if (isset($saved)) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* @var $request_helper \gplcart\core\helpers\Request */ |
|
73
|
|
|
$request_helper = $this->getHelper('Request'); |
|
74
|
|
|
$detected_langcode = $request_helper->language(); |
|
75
|
|
|
|
|
76
|
|
|
if (!in_array($detected_langcode, $settings['redirect'])) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$language = $this->getLanguage()->get($detected_langcode); |
|
81
|
|
|
|
|
82
|
|
|
if (empty($language['status'])) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* @var $url_helper \gplcart\core\helpers\Url */ |
|
87
|
|
|
$url_helper = $this->getHelper('Url'); |
|
88
|
|
|
$session_helper->set('langdetect', $detected_langcode); |
|
89
|
|
|
|
|
90
|
|
|
if ($detected_langcode !== $langcode) { |
|
91
|
|
|
$redirect = $url_helper->language($detected_langcode, $url_helper->path()); |
|
92
|
|
|
$url_helper->redirect($redirect, array(), true, true); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|