33use App ;
44use Lang ;
55use Event ;
6+ use System ;
67use Backend ;
7- use Cms \Classes \Page ;
8- use System \Models \File ;
9- use Cms \Models \ThemeData ;
108use System \Classes \PluginBase ;
119use System \Classes \CombineAssets ;
10+ use System \Classes \SettingsManager ;
1211use RainLab \Translate \Models \Message ;
13- use RainLab \Translate \Classes \EventRegistry ;
12+ use RainLab \Translate \Classes \EventCoreRegistry ;
13+ use RainLab \Translate \Classes \EventPluginRegistry ;
1414use RainLab \Translate \Classes \Translator ;
1515
1616/**
@@ -20,17 +20,16 @@ class Plugin extends PluginBase
2020{
2121 /**
2222 * pluginDetails returns information about this plugin.
23- *
2423 * @return array
2524 */
2625 public function pluginDetails ()
2726 {
2827 return [
29- 'name ' => 'rainlab.translate::lang.plugin.name ' ,
30- 'description ' => 'rainlab.translate::lang.plugin.description ' ,
31- 'author ' => 'Alexey Bobkov, Samuel Georges ' ,
32- 'icon ' => 'icon-language ' ,
33- 'homepage ' => 'https://github.com/rainlab/translate-plugin '
28+ 'name ' => 'Translate ' ,
29+ 'description ' => 'Enables multi-lingual websites. ' ,
30+ 'author ' => 'Alexey Bobkov, Samuel Georges ' ,
31+ 'icon ' => 'icon-language ' ,
32+ 'homepage ' => 'https://github.com/rainlab/translate-plugin '
3433 ];
3534 }
3635
@@ -39,151 +38,30 @@ public function pluginDetails()
3938 */
4039 public function register ()
4140 {
42- // Load localized version of mail templates (akin to localized CMS content files)
43- Event::listen ('mailer.beforeAddContent ' , function ($ mailer , $ message , $ view , $ data , $ raw , $ plain ) {
44- return EventRegistry::instance ()->findLocalizedMailViewContent ($ mailer , $ message , $ view , $ data , $ raw , $ plain );
45- }, 1 );
46-
47- // Defer event with low priority to let others contribute before this registers.
48- Event::listen ('backend.form.extendFieldsBefore ' , function ($ widget ) {
49- EventRegistry::instance ()->registerFormFieldReplacements ($ widget );
50- }, -1 );
51-
52- // Handle translated page URLs
53- Page::extend (function ($ model ) {
54- if (!$ model ->propertyExists ('translatable ' )) {
55- $ model ->addDynamicProperty ('translatable ' , []);
56- }
57- $ model ->translatable = array_merge ($ model ->translatable , ['title ' , 'description ' , 'meta_title ' , 'meta_description ' ]);
58- if (!$ model ->isClassExtendedWith (\RainLab \Translate \Behaviors \TranslatablePageUrl::class)) {
59- $ model ->extendClassWith (\RainLab \Translate \Behaviors \TranslatablePageUrl::class);
60- }
61- if (!$ model ->isClassExtendedWith (\RainLab \Translate \Behaviors \TranslatablePage::class)) {
62- $ model ->extendClassWith (\RainLab \Translate \Behaviors \TranslatablePage::class);
63- }
64- });
65-
66- // Extension logic for October CMS v1.0
67- if (!class_exists ('System ' )) {
68- $ this ->extendLegacyPlatform ();
69- }
70- // Extension logic for October CMS v2.0
71- else {
72- Event::listen ('cms.theme.createThemeDataModel ' , function ($ attributes ) {
73- return new \RainLab \Translate \Models \MLThemeData ($ attributes );
74- });
75-
76- Event::listen ('cms.template.getTemplateToolbarSettingsButtons ' , function ($ extension , $ dataHolder ) {
77- if ($ dataHolder ->templateType === 'page ' ) {
78- EventRegistry::instance ()->extendEditorPageToolbar ($ dataHolder );
79- }
80- });
81- }
41+ EventCoreRegistry::instance ()->registerEvents ();
42+ EventPluginRegistry::instance ()->registerEvents ();
8243
8344 // Register console commands
8445 $ this ->registerConsoleCommand ('translate.scan ' , \Rainlab \Translate \Console \ScanCommand::class);
46+ $ this ->registerConsoleCommand ('translate.migratev1 ' , \Rainlab \Translate \Console \MigrateV1Command::class);
8547
8648 // Register asset bundles
8749 $ this ->registerAssetBundles ();
8850 }
8951
90- /**
91- * extendLegacyPlatform will add the legacy features expected in v1.0
92- */
93- protected function extendLegacyPlatform ()
94- {
95- // Adds translation support to file models
96- File::extend (function ($ model ) {
97- if (!$ model ->propertyExists ('translatable ' )) {
98- $ model ->addDynamicProperty ('translatable ' , []);
99- }
100- $ model ->translatable = array_merge ($ model ->translatable , ['title ' , 'description ' ]);
101- if (!$ model ->isClassExtendedWith (\October \Rain \Database \Behaviors \Purgeable::class)) {
102- $ model ->extendClassWith (\October \Rain \Database \Behaviors \Purgeable::class);
103- }
104- if (!$ model ->isClassExtendedWith (\RainLab \Translate \Behaviors \TranslatableModel::class)) {
105- $ model ->extendClassWith (\RainLab \Translate \Behaviors \TranslatableModel::class);
106- }
107- });
108-
109- // Adds translation support to theme settings
110- ThemeData::extend (static function ($ model ) {
111- if (!$ model ->propertyExists ('translatable ' )) {
112- $ model ->addDynamicProperty ('translatable ' , []);
113- }
114-
115- if (!$ model ->isClassExtendedWith (\October \Rain \Database \Behaviors \Purgeable::class)) {
116- $ model ->extendClassWith (\October \Rain \Database \Behaviors \Purgeable::class);
117- }
118- if (!$ model ->isClassExtendedWith (\RainLab \Translate \Behaviors \TranslatableModel::class)) {
119- $ model ->extendClassWith (\RainLab \Translate \Behaviors \TranslatableModel::class);
120- }
121-
122- $ model ->bindEvent ('model.afterFetch ' , static function () use ($ model ) {
123- foreach ($ model ->getFormFields () as $ id => $ field ) {
124- if (!empty ($ field ['translatable ' ])) {
125- $ model ->translatable [] = $ id ;
126- }
127- }
128- });
129- });
130- }
131-
13252 /**
13353 * boot the plugin
13454 */
13555 public function boot ()
13656 {
137- // Set the page context for translation caching with high priority.
138- Event::listen ('cms.page.init ' , function ($ controller , $ page ) {
139- EventRegistry::instance ()->setMessageContext ($ page );
140- }, 100 );
57+ EventCoreRegistry::instance ()->bootEvents ();
58+ EventPluginRegistry::instance ()->bootEvents ();
14159
142- // Populate MenuItem properties with localized values if available
143- Event::listen ('pages.menu.referencesGenerated ' , function (&$ items ) {
144- $ locale = App::getLocale ();
145- $ iterator = function ($ menuItems ) use (&$ iterator , $ locale ) {
146- $ result = [];
147- foreach ($ menuItems as $ item ) {
148- $ localeFields = array_get ($ item ->viewBag , "locale. $ locale " , []);
149- foreach ($ localeFields as $ fieldName => $ fieldValue ) {
150- if ($ fieldValue ) {
151- $ item ->$ fieldName = $ fieldValue ;
152- }
153- }
154- if ($ item ->items ) {
155- $ item ->items = $ iterator ($ item ->items );
156- }
157- $ result [] = $ item ;
158- }
159- return $ result ;
160- };
161- $ items = $ iterator ($ items );
162- });
163-
164- // Import messages defined by the theme
165- Event::listen ('cms.theme.setActiveTheme ' , function ($ code ) {
166- EventRegistry::instance ()->importMessagesFromTheme ($ code );
167- });
168-
169- // Adds language suffixes to content files.
170- Event::listen ('cms.page.beforeRenderContent ' , function ($ controller , $ fileName ) {
171- return EventRegistry::instance ()
172- ->findTranslatedContentFile ($ controller , $ fileName )
173- ;
174- });
175-
176- // Prune localized content files from template list
177- Event::listen ('pages.content.templateList ' , function ($ widget , $ templates ) {
178- return EventRegistry::instance ()
179- ->pruneTranslatedContentTemplates ($ templates )
180- ;
181- });
182-
183- // Look at session for locale using middleware
184- \Cms \Classes \CmsController::extend (function ($ controller ) {
185- $ controller ->middleware (\RainLab \Translate \Classes \LocaleMiddleware::class);
186- });
60+ if (System::checkDebugMode ()) {
61+ App::after (function () {
62+ Message::saveObserver ();
63+ });
64+ }
18765
18866 // Append current locale to static page's cache keys
18967 $ modifyKey = function (&$ key ) {
@@ -226,30 +104,15 @@ public function boot()
226104 }
227105 }
228106
229- /**
230- * registerComponents
231- */
232- public function registerComponents ()
233- {
234- return [
235- \RainLab \Translate \Components \LocalePicker::class => 'localePicker ' ,
236- \RainLab \Translate \Components \AlternateHrefLangElements::class => 'alternateHrefLangElements '
237- ];
238- }
239-
240107 /**
241108 * registerPermissions
242109 */
243110 public function registerPermissions ()
244111 {
245112 return [
246- 'rainlab.translate.manage_locales ' => [
247- 'tab ' => 'rainlab.translate::lang.plugin.tab ' ,
248- 'label ' => 'rainlab.translate::lang.plugin.manage_locales '
249- ],
250113 'rainlab.translate.manage_messages ' => [
251- 'tab ' => 'rainlab.translate::lang.plugin.tab ' ,
252- 'label ' => 'rainlab.translate::lang.plugin.manage_messages '
114+ 'tab ' => 'Translation ' ,
115+ 'label ' => 'Manage messages '
253116 ]
254117 ];
255118 }
@@ -260,23 +123,13 @@ public function registerPermissions()
260123 public function registerSettings ()
261124 {
262125 return [
263- 'locales ' => [
264- 'label ' => 'rainlab.translate::lang.locale.title ' ,
265- 'description ' => 'rainlab.translate::lang.plugin.description ' ,
266- 'icon ' => 'icon-language ' ,
267- 'url ' => Backend::url ('rainlab/translate/locales ' ),
268- 'order ' => 550 ,
269- 'category ' => 'rainlab.translate::lang.plugin.name ' ,
270- 'permissions ' => ['rainlab.translate.manage_locales ' ],
271- 'keywords ' => 'translate ' ,
272- ],
273126 'messages ' => [
274- 'label ' => 'rainlab.translate::lang.messages.title ' ,
275- 'description ' => 'rainlab.translate::lang. messages.description ' ,
127+ 'label ' => 'Translate Messages ' ,
128+ 'description ' => 'Update messages used by the theme ' ,
276129 'icon ' => 'icon-list-alt ' ,
277130 'url ' => Backend::url ('rainlab/translate/messages ' ),
278131 'order ' => 551 ,
279- 'category ' => ' rainlab.translate::lang.plugin.name ' ,
132+ 'category ' => SettingsManager:: CATEGORY_CMS ,
280133 'permissions ' => ['rainlab.translate.manage_messages ' ],
281134 'keywords ' => 'translate ' ,
282135 ]
@@ -300,34 +153,13 @@ public function registerMarkupTags()
300153 ];
301154 }
302155
303- /**
304- * registerFormWidgets for multi-lingual
305- */
306- public function registerFormWidgets ()
307- {
308- $ mediaFinderClass = class_exists ('System ' )
309- ? \RainLab \Translate \FormWidgets \MLMediaFinderv2::class
310- : \RainLab \Translate \FormWidgets \MLMediaFinder::class;
311-
312- return [
313- \RainLab \Translate \FormWidgets \MLText::class => 'mltext ' ,
314- \RainLab \Translate \FormWidgets \MLTextarea::class => 'mltextarea ' ,
315- \RainLab \Translate \FormWidgets \MLRichEditor::class => 'mlricheditor ' ,
316- \RainLab \Translate \FormWidgets \MLMarkdownEditor::class => 'mlmarkdowneditor ' ,
317- \RainLab \Translate \FormWidgets \MLRepeater::class => 'mlrepeater ' ,
318- \RainLab \Translate \FormWidgets \MLNestedForm::class => 'mlnestedform ' ,
319- $ mediaFinderClass => 'mlmediafinder ' ,
320- ];
321- }
322-
323156 /**
324157 * registerAssetBundles for compilation
325158 */
326159 protected function registerAssetBundles ()
327160 {
328161 CombineAssets::registerCallback (function ($ combiner ) {
329162 $ combiner ->registerBundle ('$/rainlab/translate/assets/less/messages.less ' );
330- $ combiner ->registerBundle ('$/rainlab/translate/assets/less/multilingual.less ' );
331163 });
332164 }
333165
0 commit comments