|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace BEdita\I18n; |
|
16
|
|
|
|
|
17
|
|
|
use BEdita\I18n\Middleware\I18nMiddleware; |
|
18
|
|
|
use Cake\Core\BasePlugin; |
|
19
|
|
|
use Cake\Core\Configure; |
|
20
|
|
|
use Cake\Http\MiddlewareQueue; |
|
21
|
|
|
use Cake\Routing\Middleware\RoutingMiddleware; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Plugin class for BEdita\WebTools. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Plugin extends BasePlugin |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Do bootstrapping or not |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $bootstrapEnabled = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Load routes or not |
|
37
|
|
|
* |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $routesEnabled = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Setup the I8nMiddleware. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. |
|
46
|
|
|
* @return \Cake\Http\MiddlewareQueue The updated middleware queue. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue |
|
49
|
|
|
{ |
|
50
|
|
|
$middlewareQueue = parent::middleware($middlewareQueue); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add I18n middleware. |
|
54
|
|
|
* |
|
55
|
|
|
* Define when I18n rules are applied with `/:lang` prefix: |
|
56
|
|
|
* - 'match': array of URL paths, if there's an exact match rule is applied |
|
57
|
|
|
* - 'startWith': array of URL paths, if current URL path starts with one of these rule is applied |
|
58
|
|
|
* - 'switchLangUrl': reserved URL (for example `/lang`) used to switch language and redirect to referer URL. |
|
59
|
|
|
* Disabled by default. |
|
60
|
|
|
* - 'cookie': array for cookie that keeps the locale value. By default no cookie is used. |
|
61
|
|
|
* - 'name': cookie name |
|
62
|
|
|
* - 'create': set to `true` if the middleware is responsible of cookie creation |
|
63
|
|
|
* - 'expire': used when `create` is `true` to define when the cookie must expire |
|
64
|
|
|
*/ |
|
65
|
|
|
$middlewareQueue->insertBefore( |
|
66
|
|
|
RoutingMiddleware::class, |
|
67
|
|
|
new I18nMiddleware((array)Configure::read('I18n', [])) |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
return $middlewareQueue; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|