|
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\Command\GettextCommand; |
|
18
|
|
|
use BEdita\I18n\Middleware\I18nMiddleware; |
|
19
|
|
|
use Cake\Console\CommandCollection; |
|
20
|
|
|
use Cake\Core\BasePlugin; |
|
21
|
|
|
use Cake\Core\Configure; |
|
22
|
|
|
use Cake\Http\MiddlewareQueue; |
|
23
|
|
|
use Cake\Routing\Middleware\RoutingMiddleware; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* I18n plugin class. |
|
27
|
|
|
*/ |
|
28
|
|
|
class I18nPlugin extends BasePlugin |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Plugin name. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string|null |
|
34
|
|
|
*/ |
|
35
|
|
|
protected ?string $name = 'I18n'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Do bootstrapping or not |
|
39
|
|
|
* |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected bool $bootstrapEnabled = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Load routes or not |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected bool $routesEnabled = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Enable middleware |
|
53
|
|
|
* |
|
54
|
|
|
* @var bool |
|
55
|
|
|
*/ |
|
56
|
|
|
protected bool $middlewareEnabled = true; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array<string> |
|
60
|
|
|
* @psalm-var array<class-string<\Cake\Console\BaseCommand>> |
|
61
|
|
|
*/ |
|
62
|
|
|
protected array $i18nCommandsList = [ |
|
63
|
|
|
'gettext' => GettextCommand::class, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function console(CommandCollection $commands): CommandCollection |
|
70
|
|
|
{ |
|
71
|
|
|
return $commands->addMany($this->i18nCommandsList); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Setup the I8nMiddleware. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. |
|
78
|
|
|
* @return \Cake\Http\MiddlewareQueue The updated middleware queue. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue |
|
81
|
|
|
{ |
|
82
|
|
|
$middlewareQueue = parent::middleware($middlewareQueue); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add I18n middleware. |
|
86
|
|
|
* |
|
87
|
|
|
* Define when I18n rules are applied with `/:lang` prefix: |
|
88
|
|
|
* - 'match': array of URL paths, if there's an exact match rule is applied |
|
89
|
|
|
* - 'startWith': array of URL paths, if current URL path starts with one of these rule is applied |
|
90
|
|
|
* - 'switchLangUrl': reserved URL (for example `/lang`) used to switch language and redirect to referer URL. |
|
91
|
|
|
* Disabled by default. |
|
92
|
|
|
* - 'cookie': array for cookie that keeps the locale value. By default no cookie is used. |
|
93
|
|
|
* - 'name': cookie name |
|
94
|
|
|
* - 'create': set to `true` if the middleware is responsible of cookie creation |
|
95
|
|
|
* - 'expire': used when `create` is `true` to define when the cookie must expire |
|
96
|
|
|
*/ |
|
97
|
|
|
$middlewareQueue->insertBefore( |
|
98
|
|
|
RoutingMiddleware::class, |
|
99
|
|
|
new I18nMiddleware((array)Configure::read('I18n', [])) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
return $middlewareQueue; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|