forked from rainlab/translate-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
183 lines (166 loc) · 5.88 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
<?php namespace RainLab\Translate;
use Lang;
use Event;
use Backend;
use Cms\Classes\Page;
use System\Classes\PluginBase;
use RainLab\Translate\Models\Message;
use RainLab\Translate\Classes\EventRegistry;
use Exception;
/**
* Translate Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'rainlab.translate::lang.plugin.name',
'description' => 'rainlab.translate::lang.plugin.description',
'author' => 'Alexey Bobkov, Samuel Georges',
'icon' => 'icon-language',
'homepage' => 'https://github.com/rainlab/translate-plugin'
];
}
public function register()
{
/*
* Defer event with low priority to let others contribute before this registers.
*/
Event::listen('backend.form.extendFieldsBefore', function($widget) {
EventRegistry::instance()->registerFormFieldReplacements($widget);
}, -1);
/*
* Handle translated page URLs
*/
Page::extend(function($page) {
$page->extendClassWith('RainLab\Translate\Behaviors\TranslatablePageUrl');
});
}
public function boot()
{
/*
* 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)
;
});
/*
* Prune localized content files from template list
*/
Event::listen('pages.content.templateList', function($widget, $templates) {
return EventRegistry::instance()
->pruneTranslatedContentTemplates($templates)
;
});
/*
* Look at session for locale using middleware
*/
\Cms\Classes\CmsController::extend(function($controller) {
$controller->middleware(\RainLab\Translate\Classes\LocaleMiddleware::class);
});
/**
* 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);
}
public function registerComponents()
{
return [
'RainLab\Translate\Components\LocalePicker' => 'localePicker',
'RainLab\Translate\Components\AlternateHrefLangElements' => 'alternateHrefLangElements'
];
}
public function registerPermissions()
{
return [
'rainlab.translate.manage_locales' => [
'tab' => 'rainlab.translate::lang.plugin.tab',
'label' => 'rainlab.translate::lang.plugin.manage_locales'
],
'rainlab.translate.manage_messages' => [
'tab' => 'rainlab.translate::lang.plugin.tab',
'label' => 'rainlab.translate::lang.plugin.manage_messages'
]
];
}
public function registerSettings()
{
return [
'locales' => [
'label' => 'rainlab.translate::lang.locale.title',
'description' => 'rainlab.translate::lang.plugin.description',
'icon' => 'icon-language',
'url' => Backend::url('rainlab/translate/locales'),
'order' => 550,
'category' => 'rainlab.translate::lang.plugin.name',
'permissions' => ['rainlab.translate.manage_locales']
],
'messages' => [
'label' => 'rainlab.translate::lang.messages.title',
'description' => 'rainlab.translate::lang.messages.description',
'icon' => 'icon-list-alt',
'url' => Backend::url('rainlab/translate/messages'),
'order' => 551,
'category' => 'rainlab.translate::lang.plugin.name',
'permissions' => ['rainlab.translate.manage_messages']
]
];
}
/**
* Register new Twig variables
* @return array
*/
public function registerMarkupTags()
{
return [
'filters' => [
'_' => [$this, 'translateString'],
'__' => [$this, 'translatePlural']
]
];
}
public function registerFormWidgets()
{
return [
'RainLab\Translate\FormWidgets\MLText' => 'mltext',
'RainLab\Translate\FormWidgets\MLTextarea' => 'mltextarea',
'RainLab\Translate\FormWidgets\MLRichEditor' => 'mlricheditor',
'RainLab\Translate\FormWidgets\MLMarkdownEditor' => 'mlmarkdowneditor',
'RainLab\Translate\FormWidgets\MLRepeater' => 'mlrepeater',
];
}
public function translateString($string, $params = [])
{
return Message::trans($string, $params);
}
public function translatePlural($string, $count = 0, $params = [])
{
return Lang::choice(Message::trans($string, $params), $count, $params);
}
}