forked from langsci/customLocale
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCustomLocalePlugin.php
255 lines (229 loc) · 7.67 KB
/
CustomLocalePlugin.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
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* @file CustomLocalePlugin.php
*
* Copyright (c) 2016-2022 Language Science Press
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CustomLocalePlugin
*/
namespace APP\plugins\generic\customLocale;
use APP\core\Application;
use APP\plugins\generic\customLocale\classes\migration\upgrade\v1_2_0\I15_LocaleMigration;
use APP\plugins\generic\customLocale\controllers\grid\CustomLocaleGridHandler;
use APP\template\TemplateManager;
use Exception;
use PKP\core\PKPApplication;
use PKP\facades\Locale;
use PKP\file\ContextFileManager;
use PKP\i18n\translation\Translator;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\RedirectAction;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class CustomLocalePlugin extends GenericPlugin
{
/** @var string Keeps the folder where custom locale files will be stored */
public const LOCALE_FOLDER = 'customLocale';
/**
* @copydoc Plugin::register
*
* @param null|int $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if (!$success || Application::isUnderMaintenance() || !$this->getEnabled()) {
return $success;
}
$this->upgrade();
// Add custom locale data for already registered locale files.
$this->setupLocalizationOverriding();
$this->setupGridHandler();
$this->callbackShowWebsiteSettingsTabs();
$this->setupDownloadChangesEndpoint();
$this->setupTemplate();
return $success;
}
/**
* Starts the localization overriding
*/
public function setupLocalizationOverriding(): void
{
if (is_dir($path = static::getStoragePath())) Locale::registerPath($path, PHP_INT_MAX);
}
/**
* Setups the template manager
*/
public function setupTemplate(): void
{
$request = Application::get()->getRequest();
TemplateManager::getManager($request)->addJavaScript(
'customLocale',
"{$request->getBaseUrl()}/{$this->getPluginPath()}/js/customLocale.js",
['contexts' => 'backend']
);
}
/**
* Permit requests to the custom locale grid handler
*/
public function setupGridHandler(): void
{
Hook::add('LoadComponentHandler', function (string $hookName, array $args): bool {
$component = $args[0];
if ($component !== 'plugins.generic.customLocale.controllers.grid.CustomLocaleGridHandler') {
return false;
}
// Allow the custom locale grid handler to get the plugin object
CustomLocaleGridHandler::setPlugin($this);
return true;
});
}
/**
* Setup the hook to download the changes
*/
public function setupDownloadChangesEndpoint(): void
{
Hook::add('LoadHandler', function (string $hookName, array $args): bool {
$request = $this->getRequest();
// Get url path components by reference
[&$page, &$op] = $args;
$tail = implode('/', $request->getRequestedArgs());
if ([$page, $op, $tail] === ['management', 'settings', 'printCustomLocaleChanges']) {
$op = 'printCustomLocaleChanges';
define('HANDLER_CLASS', CustomLocaleHandler::class);
}
return false;
});
}
/**
* Extend the website settings tabs to include the custom locale tab
*/
public function callbackShowWebsiteSettingsTabs(): void
{
Hook::add('Template::Settings::website', function (string $hookName, array $args): bool {
[, $templateMgr, &$output] = $args;
$output .= $templateMgr->fetch($this->getTemplateResource('customLocaleTab.tpl'));
// Permit other plugins to continue interacting with this hook
return false;
});
}
/**
* Retrieves the path where custom locales are stored
*/
public static function getStoragePath(): string
{
static $path;
if ($path) {
return $path;
}
$contextFileManager = static::getContextFileManager();
$path = $contextFileManager->getBasePath() . static::LOCALE_FOLDER;
if (!is_dir($path)) {
$contextFileManager->mkdir($path);
}
return realpath($path);
}
/**
* Retrieves a translator instance without the localization overrides
*/
public static function getTranslator(string $locale): Translator
{
static $translator;
if ($translator) {
return $translator;
}
$bundle = Locale::getBundle($locale, false);
$entries = $bundle->getEntries();
$customLocalePath = static::getStoragePath();
// Remove custom locale entries from the bundle in order to retrieve the original translation
$entries = array_filter($entries, fn (string $path) => !str_starts_with($path, $customLocalePath), ARRAY_FILTER_USE_KEY);
$bundle->setEntries($entries);
return $translator = $bundle->getTranslator();
}
/**
* Retrieves an instance of the ContextFileManager
*/
public static function getContextFileManager(): ContextFileManager
{
$context = Application::get()->getRequest()->getContext();
return new ContextFileManager($context ? $context->getId() : Application::CONTEXT_SITE);
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs): array
{
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$dispatcher = $request->getDispatcher();
array_unshift(
$actions,
new LinkAction(
'customize',
new RedirectAction($dispatcher->url(
$request,
PKPApplication::ROUTE_PAGE,
null,
'management',
'settings',
'website',
['uid' => uniqid()], // Force reload
'customLocale' // Anchor for tab
)),
__('plugins.generic.customLocale.customize')
),
new LinkAction(
'printChanges',
new RedirectAction($dispatcher->url(
$request,
PKPApplication::ROUTE_PAGE,
null,
'management',
'settings',
'printCustomLocaleChanges',
['uid' => uniqid()] // Force reload
)),
__('plugins.generic.customLocale.printChanges')
)
);
return $actions;
}
/**
* Attempts to upgrade the plugin
*/
public function upgrade(): void
{
try {
(new I15_LocaleMigration())->up();
} catch (Exception $e) {
error_log("An exception happened while upgrading the customLocale plugin.\n" . $e);
}
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.customLocale.name');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.generic.customLocale.description');
}
/**
* @copydoc PKPPlugin::getSeq()
*/
public function getSeq(): int
{
return -1;
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\customLocale\CustomLocalePlugin', '\CustomLocalePlugin');
}