forked from rainlab/translate-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
287 lines (256 loc) · 8.79 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
<?php namespace RainLab\Translate;
use App;
use Str;
use Lang;
use File;
use Event;
use Backend;
use Cms\Classes\Page;
use Cms\Classes\Content;
use System\Classes\PluginBase;
use RainLab\Translate\Models\Message;
use RainLab\Translate\Models\Locale as LocaleModel;
use RainLab\Translate\Classes\Translator;
use RainLab\Translate\Classes\ThemeScanner;
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 2 levels deep to let others contribute before this registers.
*/
Event::listen('backend.form.extendFieldsBefore', function($widget) {
$widget->bindEvent('form.extendFieldsBefore', function() use ($widget) {
$this->registerModelTranslation($widget);
});
});
}
public function boot()
{
/*
* Set the page context for translation caching.
*/
Event::listen('cms.page.beforeDisplay', function($controller, $url, $page) {
if (!$page) {
return;
}
$translator = Translator::instance();
Message::setContext($translator->getLocale(), $page->url);
});
/*
* Adds language suffixes to content files.
*/
Event::listen('cms.page.beforeRenderContent', function($controller, $fileName) {
if (!strlen(File::extension($fileName))) {
$fileName .= '.htm';
}
/*
* Splice the active locale in to the filename
* - content.htm -> content.en.htm
*/
$locale = Translator::instance()->getLocale();
$fileName = substr_replace($fileName, '.'.$locale, strrpos($fileName, '.'), 0);
if (($content = Content::loadCached($controller->getTheme(), $fileName)) !== null) {
return $content;
}
});
/*
* Import messages defined by the theme
*/
Event::listen('cms.theme.setActiveTheme', function($code) {
try {
(new ThemeScanner)->scanThemeConfigForMessages();
}
catch (Exception $ex) {}
});
/*
* Prune localized content files from template list
*/
Event::listen('pages.content.templateList', function($widget, $templates) {
return $this->pruneTranslatedContentTemplates($templates);
});
}
public function registerComponents()
{
return [
'RainLab\Translate\Components\LocalePicker' => 'localePicker'
];
}
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' => [
'label' => 'Text (ML)',
'code' => 'mltext'
],
'RainLab\Translate\FormWidgets\MLTextarea' => [
'label' => 'Textarea (ML)',
'code' => 'mltextarea'
],
'RainLab\Translate\FormWidgets\MLRichEditor' => [
'label' => 'Rich Editor (ML)',
'code' => 'mlricheditor'
],
'RainLab\Translate\FormWidgets\MLMarkdownEditor' => [
'label' => 'Markdown Editor (ML)',
'code' => 'mlmarkdowneditor'
]
];
}
public function translateString($string, $params = [])
{
return Message::trans($string, $params);
}
public function translatePlural($string, $count = 0, $params = [])
{
return Lang::choice($string, $count, $params);
}
/**
* Automatically replace form fields for multi lingual equivalents
*/
protected function registerModelTranslation($widget)
{
if (!$model = $widget->model) {
return;
}
if (!method_exists($model, 'isClassExtendedWith')) {
return;
}
if (
!$model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel') &&
!$model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableCmsObject')
) {
return;
}
if (!is_array($model->translatable)) {
return;
}
if (!empty($widget->config->fields)) {
$widget->fields = $this->processFormMLFields($widget->fields, $model);
}
if (!empty($widget->config->tabs['fields'])) {
$widget->tabs['fields'] = $this->processFormMLFields($widget->tabs['fields'], $model);
}
if (!empty($widget->config->secondaryTabs['fields'])) {
$widget->secondaryTabs['fields'] = $this->processFormMLFields($widget->secondaryTabs['fields'], $model);
}
}
/**
* Helper function to replace standard fields with multi lingual equivalents
* @param array $fields
* @param Model $model
* @return array
*/
protected function processFormMLFields($fields, $model)
{
$translatable = array_flip($model->translatable);
/*
* Special: A custom field "markup_html" is used for Content templates.
*/
if ($model instanceof Content && array_key_exists('markup', $translatable)) {
$translatable['markup_html'] = true;
}
foreach ($fields as $name => $config) {
if (!array_key_exists($name, $translatable)) {
continue;
}
$type = array_get($config, 'type', 'text');
if ($type == 'text') {
$fields[$name]['type'] = 'mltext';
}
elseif ($type == 'textarea') {
$fields[$name]['type'] = 'mltextarea';
}
elseif ($type == 'richeditor') {
$fields[$name]['type'] = 'mlricheditor';
}
elseif ($type == 'markdown') {
$fields[$name]['type'] = 'mlmarkdowneditor';
}
}
return $fields;
}
/**
* Removes localized content files from templates collection
* @param \October\Rain\Database\Collection $templates
* @return \October\Rain\Database\Collection
*/
protected function pruneTranslatedContentTemplates($templates)
{
$locales = LocaleModel::listAvailable();
$extensions = array_map(function($ext) {
return '.'.$ext;
}, array_keys($locales));
return $templates->filter(function($template) use ($extensions) {
return !Str::endsWith($template->getBaseFileName(), $extensions);
});
}
}