-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeLanguageWithURL.php
242 lines (209 loc) · 6.86 KB
/
ChangeLanguageWithURL.php
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2024 webtrees development team
* <http://webtrees.net>
*
* ChangeLanguageWithURL (webtrees custom module):
* Copyright (C) 2024 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* ChangeLanguageWithURL
*
* A webtrees 2.1/2.2 custom module to change the webtrees language
* by URL requests with the language provided as an URL parameter.
*
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\ChangeLanguageWithURL;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Session;
use Fisharebest\Webtrees\View;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ChangeLanguageWithURL extends AbstractModule implements ModuleCustomInterface, MiddlewareInterface {
use ModuleCustomTrait;
//Custom module version
public const CUSTOM_VERSION = '1.1.0';
//Github repository
public const GITHUB_REPO = 'Jefferson49/ChangeLanguageWithURL';
//Github API URL to get the information about the latest releases
public const GITHUB_API_LATEST_VERSION = 'https://api.github.com/repos/'. self::GITHUB_REPO . '/releases/latest';
public const GITHUB_API_TAG_NAME_PREFIX = '"tag_name":"v';
//Author of custom module
public const CUSTOM_AUTHOR = 'Markus Hemprich';
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
return 'ChangeLanguageWithURL module';
}
/**
* Where does this module store its resources
*
* @return string
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* {@inheritDoc}
*
* @return string
*
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleAuthorName()
*/
public function customModuleAuthorName(): string
{
return self::CUSTOM_AUTHOR;
}
/**
* {@inheritDoc}
*
* @return string
*
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion()
*/
public function customModuleVersion(): string
{
return self::CUSTOM_VERSION;
}
/**
* {@inheritDoc}
*
* @return string
*
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersion()
*/
public function customModuleLatestVersion(): string
{
// No update URL provided.
if (self::GITHUB_API_LATEST_VERSION === '') {
return $this->customModuleVersion();
}
return Registry::cache()->file()->remember(
$this->name() . '-latest-version',
function (): string {
try {
$client = new Client(
[
'timeout' => 3,
]
);
$response = $client->get(self::GITHUB_API_LATEST_VERSION);
if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
$content = $response->getBody()->getContents();
preg_match_all('/' . self::GITHUB_API_TAG_NAME_PREFIX . '\d+\.\d+\.\d+/', $content, $matches, PREG_OFFSET_CAPTURE);
if(!empty($matches[0]))
{
$version = $matches[0][0][0];
$version = substr($version, strlen(self::GITHUB_API_TAG_NAME_PREFIX));
}
else
{
$version = $this->customModuleVersion();
}
return $version;
}
} catch (GuzzleException $ex) {
// Can't connect to the server?
}
return $this->customModuleVersion();
},
86400
);
}
/**
* {@inheritDoc}
*
* @return string
*
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleSupportUrl()
*/
public function customModuleSupportUrl(): string
{
return 'https://github.com/' . self::GITHUB_REPO;
}
/**
* Show error message in the front end
*
* @return ResponseInterface
*/
private function showErrorMessage(string $text): ResponseInterface
{
return $this->viewResponse($this->name() . '::error', [
'title' => 'Error',
'tree' => null,
'text' => I18N::translate('Custom module') . ': ' . $this->name() . '<br><b>'. $text . '</b>',
]);
}
/**
* Code here is executed before and after we process the request/response.
* We can block access by throwing an exception.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Code here is executed before we process the request/response.
$params = $request->getQueryParams();
$language = $params['language'] ?? '';
if ($language != '') {
$locales = I18N::activeLocales();
$language_found = false;
foreach (I18N::activeLocales() as $locale) {
if ($locale->languageTag() == $language) {
$language_found = true;
}
}
if ($language_found) {
I18N::init($language);
Session::put('language', $language);
}
else {
//Show error message
return $this->showErrorMessage(I18N::translate('Requested language tag not found') . ': ' . $language);
}
}
// Generate the response
return $handler->handle($request);
}
}