-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPlugin.php
427 lines (388 loc) · 16.1 KB
/
Plugin.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
namespace Winter\Translate;
use Backend;
use Backend\Models\UserRole;
use Cms\Classes\Page;
use Cms\Classes\Theme;
use Cms\Models\ThemeData;
use DOMDocument;
use DOMElement;
use Event;
use Lang;
use Model;
use System\Classes\CombineAssets;
use System\Classes\PluginBase;
use System\Classes\PluginManager;
use System\Models\File;
use System\Models\MailTemplate;
use Winter\Sitemap\Classes\DefinitionItem;
use Winter\Sitemap\Models\Definition;
use Winter\Translate\Classes\EventRegistry;
use Winter\Translate\Classes\MLPage;
use Winter\Translate\Classes\Translator;
use Winter\Translate\Models\Locale;
use Winter\Translate\Models\Message;
/**
* Translate Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*/
public function pluginDetails(): array
{
return [
'name' => 'winter.translate::lang.plugin.name',
'description' => 'winter.translate::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-language',
'homepage' => 'https://github.com/wintercms/wn-translate-plugin',
'replaces' => ['RainLab.Translate' => '<= 1.9.0'],
];
}
/**
* Registers the components provided by this plugin.
*/
public function registerComponents(): array
{
return [
\Winter\Translate\Components\LocalePicker::class => 'localePicker',
\Winter\Translate\Components\AlternateHrefLangElements::class => 'alternateHrefLangElements'
];
}
/**
* Registers the permissions provided by this plugin.
*/
public function registerPermissions(): array
{
return [
'winter.translate.manage_locales' => [
'tab' => 'winter.translate::lang.plugin.tab',
'label' => 'winter.translate::lang.plugin.manage_locales',
'roles' => [UserRole::CODE_DEVELOPER],
],
'winter.translate.manage_messages' => [
'tab' => 'winter.translate::lang.plugin.tab',
'label' => 'winter.translate::lang.plugin.manage_messages',
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
]
];
}
/**
* Registers the settings provided by this plugin
*/
public function registerSettings(): array
{
return [
'locales' => [
'label' => 'winter.translate::lang.locale.title',
'description' => 'winter.translate::lang.plugin.description',
'icon' => 'icon-language',
'url' => Backend::url('winter/translate/locales'),
'order' => 550,
'category' => 'winter.translate::lang.plugin.name',
'permissions' => ['winter.translate.manage_locales']
],
'messages' => [
'label' => 'winter.translate::lang.messages.title',
'description' => 'winter.translate::lang.messages.description',
'icon' => 'icon-list-alt',
'url' => Backend::url('winter/translate/messages'),
'order' => 551,
'category' => 'winter.translate::lang.plugin.name',
'permissions' => ['winter.translate.manage_messages']
]
];
}
/**
* Register Twig extensions provided by this plugin
*/
public function registerMarkupTags(): array
{
return [
'filters' => [
'_' => function ($string, $params = [], $locale = null) {
return Message::trans($string, $params, $locale);
},
'__' => function ($string, $count = 0, $params = [], $locale = null) {
return Lang::choice(Message::trans($string, $params, $locale), $count, $params);
},
'transRaw' => function ($string, $params = [], $locale = null) {
return Message::transRaw($string, $params, $locale);
},
'transRawPlural' => function ($string, $count = 0, $params = [], $locale = null) {
return Lang::choice(Message::transRaw($string, $params, $locale), $count, $params);
},
'localeUrl' => function ($url, $locale) {
$translator = Translator::instance();
$parts = parse_url($url);
$path = array_get($parts, 'path');
return http_build_url($parts, [
'path' => '/' . $translator->getPathInLocale($path, $locale)
]);
},
]
];
}
/**
* Registers FormWidgets provided by this plugin
*/
public function registerFormWidgets(): array
{
return [
\Winter\Translate\FormWidgets\MLText::class => 'mltext',
\Winter\Translate\FormWidgets\MLTextarea::class => 'mltextarea',
\Winter\Translate\FormWidgets\MLRichEditor::class => 'mlricheditor',
\Winter\Translate\FormWidgets\MLMarkdownEditor::class => 'mlmarkdowneditor',
\Winter\Translate\FormWidgets\MLRepeater::class => 'mlrepeater',
\Winter\Translate\FormWidgets\MLMediaFinder::class => 'mlmediafinder',
\Winter\Translate\FormWidgets\MLNestedForm::class => 'mlnestedform',
];
}
/**
* Registers Asset Combiner bundles provided by this plugin
*/
protected function registerAssetBundles()
{
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('$/winter/translate/assets/less/messages.less');
$combiner->registerBundle('$/winter/translate/assets/less/multilingual.less');
});
}
/**
* Registers the plugin
*/
public function register(): void
{
/*
* Register console commands
*/
$this->registerConsoleCommand('translate.scan', \Winter\Translate\Console\ScanCommand::class);
$this->registerAssetBundles();
}
/**
* Boots the plugin
*/
public function boot(): void
{
$this->extendBackendModule();
$this->extendCmsModule();
$this->extendSystemModule();
$this->extendWinterPagesPlugin();
$this->extendWinterSitemapPlugin();
}
/**
* Extends the Backend module with translation support
*/
protected function extendBackendModule(): void
{
// Defer event with low priority to let others contribute before this registers.
Event::listen('backend.form.extendFieldsBefore', function ($widget) {
EventRegistry::instance()->registerFormFieldReplacements($widget);
}, -1);
}
/**
* Extends the CMS module with translation support
*/
protected function extendCmsModule(): void
{
// Verify that the CMS module is installed and enabled before extending it
if (!class_exists('\Cms\Classes\Page') || !in_array('Cms', config('cms.loadModules'))) {
return;
}
/*
* Handle translated page URLs
*/
Page::extend(function($model) {
$this->extendModel($model, 'page', ['title', 'description', 'meta_title', 'meta_description']);
});
/*
* Add translation support to theme settings
*/
ThemeData::extend(function ($model) {
$model->bindEvent('model.afterFetch', function() use ($model) {
$translatable = [];
foreach ($model->getFormFields() as $id => $field) {
if (!empty($field['translatable'])) {
$translatable[] = $id;
}
}
$this->extendModel($model, 'model', $translatable);
});
});
// Look at session for locale using middleware
\Cms\Classes\CmsController::extend(function($controller) {
$controller->middleware(\Winter\Translate\Classes\LocaleMiddleware::class);
});
// Set the page context for translation caching with high priority.
Event::listen('cms.page.init', function($controller, $page) {
EventRegistry::instance()->setMessageContext($page);
}, 100);
// Import messages defined by the theme
Event::listen('cms.theme.setActiveTheme', function($code) {
EventRegistry::instance()->importMessagesFromTheme();
});
// Adds language suffixes to content files.
Event::listen('cms.page.beforeRenderContent', function($controller, $fileName) {
return EventRegistry::instance()
->findTranslatedContentFile($controller, $fileName)
;
});
}
/**
* Extends the System module with translation support
*/
protected function extendSystemModule(): void
{
// Add translation support to file models
File::extend(function ($model) {
$this->extendModel($model, 'model', ['title', 'description']);
});
MailTemplate::extend(function ($model) {
$this->extendModel($model, 'model', ['subject', 'description', 'content_html', 'content_text']);
});
// Load localized version of mail templates (akin to localized CMS content files)
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) {
if (!empty($data['_current_locale'])) {
$translator = Translator::instance();
$translator->setLocale($data['_current_locale']);
}
return EventRegistry::instance()->findLocalizedMailViewContent($mailer, $message, $view, $data, $raw, $plain);
}, 1);
}
/**
* Extends the Winter.Pages plugin with translation support
*/
protected function extendWinterPagesPlugin(): void
{
// Populate MenuItem properties with localized values if available
Event::listen('pages.menu.referencesGenerated', function (&$items) {
$locale = $this->app->getLocale();
$iterator = function ($menuItems) use (&$iterator, $locale) {
$result = [];
foreach ($menuItems as $item) {
$localeFields = array_get($item->viewBag, "locale.$locale", []);
foreach ($localeFields as $fieldName => $fieldValue) {
if ($fieldValue) {
$item->$fieldName = $fieldValue;
}
}
if ($item->items) {
$item->items = $iterator($item->items);
}
$result[] = $item;
}
return $result;
};
$items = $iterator($items);
});
// Prune localized content files from template list
Event::listen('pages.content.templateList', function($widget, $templates) {
return EventRegistry::instance()
->pruneTranslatedContentTemplates($templates)
;
});
/**
* Append current locale to static page's cache keys
*/
$modifyKey = function (&$key) {
$key = $key . '-' . Lang::getLocale();
};
Event::listen('pages.router.getCacheKey', $modifyKey);
Event::listen('pages.page.getMenuCacheKey', $modifyKey);
Event::listen('pages.snippet.getMapCacheKey', $modifyKey);
Event::listen('pages.snippet.getPartialMapCacheKey', $modifyKey);
if (class_exists('\Winter\Pages\Classes\SnippetManager')) {
$handler = function ($controller, $template, $type) {
if (!$template->methodExists('getDirtyLocales')) {
return;
}
// Get the locales that have changed
$dirtyLocales = $template->getDirtyLocales();
if (!empty($dirtyLocales)) {
$theme = Theme::getEditTheme();
$currentLocale = Lang::getLocale();
foreach ($dirtyLocales as $locale) {
if (!$template->isTranslateDirty(null, $locale)) {
continue;
}
// Clear the Winter.Pages caches for each dirty locale
$this->app->setLocale($locale);
\Winter\Pages\Classes\Page::clearMenuCache($theme);
\Winter\Pages\Classes\SnippetManager::clearCache($theme);
}
// Restore the original locale for this request
$this->app->setLocale($currentLocale);
}
};
Event::listen('cms.template.save', $handler);
Event::listen('pages.object.save', $handler);
}
}
/**
* Extend the Winter.Sitemap plugin
* ref. document: https://developers.google.com/search/blog/2012/05/multilingual-and-multinational-site
*/
protected function extendWinterSitemapPlugin(): void
{
$pluginManager = PluginManager::instance();
if (!$pluginManager->exists('Winter.Sitemap')) {
return;
}
// Ensure that CMS Pages include their alternateLinks data when generating the sitemap
Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
if ($item->type === 'cms-page') {
return MLPage::resolveMenuItem($item, $url, $theme);
}
}, 1);
$defaultLocale = Locale::getDefault();
Event::listen('winter.sitemap.addItem',
function (DefinitionItem $item, array $itemInfo, Definition $definition, DOMDocument $xml, DOMElement $urlSet, DOMElement $urlElement) use ($defaultLocale) {
if (isset($itemInfo['alternateLinks'])) {
foreach ($itemInfo['alternateLinks'] as $locale => $altUrl) {
$linkElement = $xml->createElement('xhtml:link');
$linkElement->setAttribute('rel', 'alternate');
$linkElement->setAttribute('hreflang', $locale);
$linkElement->setAttribute('href', $altUrl);
$urlElement->appendChild($linkElement);
}
foreach ($itemInfo['alternateLinks'] as $locale => $altUrl) {
if ($locale === $defaultLocale->code) {
$loc = $urlElement->getElementsByTagName('loc')->item(0);
$loc->nodeValue = $altUrl;
continue;
}
$newElement = $urlElement->cloneNode(true);
$loc = $newElement->getElementsByTagName('loc')->item(0);
$loc->nodeValue = $itemInfo['alternateLinks'][$locale];
$urlSet->appendChild($newElement);
}
}
}
);
}
/**
* Helper method to extend the provided model with translation support
*/
public function extendModel($model, string $type, array $translatableAttributes = [])
{
if (!$model->propertyExists('translatable')) {
$model->addDynamicProperty('translatable', $translatableAttributes);
} else {
$model->translatable = array_merge($model->translatable, $translatableAttributes);
}
if ($type === 'page') {
if (!$model->isClassExtendedWith('Winter\Translate\Behaviors\TranslatablePageUrl')) {
$model->extendClassWith('Winter\Translate\Behaviors\TranslatablePageUrl');
}
if (!$model->isClassExtendedWith('Winter\Translate\Behaviors\TranslatablePage')) {
$model->extendClassWith('Winter\Translate\Behaviors\TranslatablePage');
}
} elseif ($type === 'model') {
if (!$model->isClassExtendedWith('Winter\Translate\Behaviors\TranslatableModel')) {
$model->extendClassWith('Winter\Translate\Behaviors\TranslatableModel');
}
}
}
}