1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. |
18
|
|
|
*/ |
19
|
|
|
namespace LearnZF2I18n; |
20
|
|
|
|
21
|
|
|
use Zend\EventManager\EventInterface; |
22
|
|
|
use Zend\Http; |
23
|
|
|
use Zend\I18n\Translator\Translator; |
24
|
|
|
use Zend\Loader\StandardAutoloader; |
25
|
|
|
use Zend\ModuleManager\Feature\AutoloaderProviderInterface; |
26
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
27
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
28
|
|
|
use Zend\Mvc\MvcEvent; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Module. |
32
|
|
|
* |
33
|
|
|
* @author Alejandro Celaya Alastrué <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class Module implements |
36
|
|
|
ConfigProviderInterface, |
37
|
|
|
AutoloaderProviderInterface, |
38
|
|
|
BootstrapListenerInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getConfig() |
44
|
|
|
{ |
45
|
|
|
return include __DIR__.'/config/module.config.php'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getAutoloaderConfig() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
55
|
|
|
StandardAutoloader::LOAD_NS => [ |
56
|
|
|
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__, |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function onBootstrap(EventInterface $e) |
66
|
|
|
{ |
67
|
|
|
/* @var MvcEvent $e */ |
68
|
|
|
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager(); |
69
|
|
|
$sharedManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'initCurrentLocale'], 10); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function initCurrentLocale(MvcEvent $e) |
73
|
|
|
{ |
74
|
|
|
$request = $e->getRequest(); |
75
|
|
|
if (!$request instanceof Http\Request) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @var Translator $translator */ |
80
|
|
|
$translator = $e->getApplication()->getServiceManager()->get('translator'); |
81
|
|
|
$lang = $request->getQuery('lang', 'en_US'); |
82
|
|
|
$translator->setLocale($lang); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// Inject current language in view model |
85
|
|
|
$e->getViewModel()->setVariable('lang', $lang); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.