root/branches/release-26/lib/MT/Plugin.pm @ 1174

Revision 1174, 25.8 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

  • Property svn:keywords set to Author Date Id Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Plugin;
8
9use strict;
10use base qw( MT::Component );
11
12sub init {
13    my $plugin = shift;
14    $plugin->{__settings} = {};
15    if (exists $plugin->{app_action_links}
16        || exists $plugin->{app_itemset_actions}
17        || exists $plugin->{upgrade_functions}
18        || exists $plugin->{app_methods}
19        || exists $plugin->{junk_filters}
20        || exists $plugin->{object_classes}) {
21        # Found in MT::Compat::v3
22        $plugin->legacy_init();
23    }
24    $plugin->SUPER::init(@_) or return;
25    return $plugin;
26}
27
28sub id {
29    my $plugin = shift;
30    my $id = $plugin->SUPER::id(@_);
31    return $id || $plugin->{plugin_sig};
32}
33
34sub key { &MT::Component::_getset(shift, 'key', @_) }
35sub author_name { &MT::Component::_getset_translate(shift, 'author_name', @_) }
36sub author_link { &MT::Component::_getset(shift, 'author_link', @_) }
37sub plugin_link { &MT::Component::_getset(shift, 'plugin_link', @_) }
38sub config_link { &MT::Component::_getset(shift, 'config_link', @_) }
39sub doc_link { &MT::Component::_getset(shift, 'doc_link', @_) }
40sub description { &MT::Component::_getset_translate(shift, 'description', @_) }
41sub settings {
42    my $plugin = shift;
43    my $s = &MT::Component::_getset($plugin, 'settings', @_);
44    unless (ref($s) eq 'MT::PluginSettings') {
45        $s = MT::PluginSettings->new($s);
46        &MT::Component::_getset($plugin, 'settings', $s);
47    }
48    return $s;
49}
50sub icon { &MT::Component::_getset(shift, 'icon', @_) }
51
52# Plugin-specific: configuration settings and data
53sub load_config {
54    my $plugin = shift;
55    my ($param, $scope) = @_;
56    my $setting_obj = $plugin->get_config_obj($scope);
57    my $settings = $setting_obj->data;
58    %$param = %$settings;
59    foreach my $key (keys %$settings) {
60        next unless defined $key;
61        my $value = $settings->{$key};
62        next if !defined $value or $value =~ m/\s/ or length($value) > 100;
63        $param->{$key.'_'.$value} = 1;
64    }
65}
66
67sub save_config {
68    my $plugin = shift;
69    my ($param, $scope) = @_;
70    my $pdata = $plugin->get_config_obj($scope);
71    $scope =~ s/:.*//;
72    my @vars = $plugin->config_vars($scope);
73    my $data = $pdata->data() || {};
74    foreach (@vars) {
75        $data->{$_} = exists $param->{$_} ? $param->{$_} : undef;
76    }
77    $pdata->data($data);
78    MT->request('plugin_config.'.$plugin->id, undef);
79    $pdata->save() or die $pdata->errstr;
80}
81
82sub reset_config {
83    my $plugin = shift;
84    my ($scope) = @_;
85    my $obj = $plugin->get_config_obj($scope);
86    MT->request('plugin_config.'.$plugin->id, undef);
87    $obj->remove if $obj->id;
88}
89
90sub config_template {
91    my $plugin = shift;
92    my ($param, $scope) = @_;
93    if ($scope) {
94        $scope =~ s/:.*//;
95    } else {
96        $scope = 'system';
97    }
98    my $r = $plugin->registry;
99    if (my $tmpl = $r->{"${scope}_config_template"} ||
100        $r->{"config_template"} ||
101        $plugin->{"${scope}_config_template"} ||
102        $plugin->{'config_template'}) {
103        return $tmpl->($plugin, @_) if ref $tmpl eq 'CODE';
104        if ($tmpl =~ /\s/) {
105            return $tmpl;
106        } else { # no spaces in $tmpl; must be a filename...
107            return $plugin->load_tmpl($tmpl);
108        }
109    }
110    return undef;
111}
112
113sub config_vars {
114    my $plugin = shift;
115    my ($scope) = @_;
116    $scope ||= 'system';
117    my @settings;
118    if (my $s = $plugin->settings) {
119        foreach my $setting (@$s) {
120            my ($name, $param) = @$setting;
121            next if $scope && $param->{scope} && $param->{scope} ne $scope;
122            push @settings, $name;
123        }
124    }
125    @settings;
126}
127
128sub set_config_value {
129    my $plugin = shift;
130    my ($vars, $scope);
131    if (ref $_[0] eq 'HASH') {
132        ($vars, $scope) = @_;
133    } else {
134        my ($variable, $value);
135        ($variable, $value, $scope) = @_;
136        $vars = { $variable => $value };
137    }
138    my $pdata_obj = $plugin->get_config_obj($scope);
139    my $configuration = $pdata_obj->data() || {};
140    $configuration->{$_} = $vars->{$_} for keys %$vars;
141    $pdata_obj->data($configuration);
142    $pdata_obj->save();
143}
144
145sub get_config_obj {
146    my $plugin = shift;
147    my ($scope_id) = @_;
148    my $key;
149    my $scope = $scope_id;
150    if ($scope && $scope ne 'system') {
151        $scope =~ s/:.*//; # strip off id, leave identifier
152        $key = 'configuration:'.$scope_id;
153    } else {
154        $scope_id = 'system';
155        $scope = 'system';
156        $key = 'configuration';
157    }
158    my $cfg = MT->request('plugin_config.'.$plugin->id);
159    unless ($cfg) {
160        $cfg = {};
161        MT->request('plugin_config.'.$plugin->id, $cfg);
162    }
163    return $cfg->{$scope_id} if $cfg->{$scope_id};
164    require MT::PluginData;
165    # calling "name" directly here to avoid localization
166    my $pdata_obj = MT::PluginData->load({plugin => $plugin->key || $plugin->{name},
167                                          key => $key});
168    if (!$pdata_obj) {
169        $pdata_obj = MT::PluginData->new();
170        $pdata_obj->plugin($plugin->key || $plugin->{name});
171        $pdata_obj->key($key);
172    }
173    $cfg->{$scope_id} = $pdata_obj;
174    my $data = $pdata_obj->data() || {};
175    $plugin->apply_default_settings($data, $scope_id);
176    $pdata_obj->data($data);
177    $pdata_obj;
178}
179
180sub apply_default_settings {
181    my $plugin = shift;
182    my ($data, $scope_id) = @_;
183    my $scope = $scope_id;
184    if ($scope =~ m/:/) {
185        $scope =~ s/:.*//;
186    } else {
187        $scope_id = 'system';
188    }
189    my $defaults;
190    my $s = $plugin->settings;
191    if ($s && ($defaults = $s->defaults($scope))) {
192        foreach (keys %$defaults) {
193            $data->{$_} = $defaults->{$_} if !exists $data->{$_};
194        }
195    }
196}
197
198sub get_config_hash {
199    my $plugin = shift;
200    $plugin->get_config_obj(@_)->data() || {};
201}
202
203sub get_config_value {
204    my $plugin = shift;
205    my ($var, $scope) = @_;
206    my $config = $plugin->get_config_hash($scope);
207    return exists $config->{$_[0]} ? $config->{$_[0]} : undef;
208}
209
210
211package MT::PluginSettings;
212
213sub new {
214    my $pkg = shift;
215    my ($self) = @_;
216    $self ||= [];
217    if (ref $self eq 'HASH') {
218        # convert a hash-style setting structure into what PluginSettings
219        # expects
220        my $settings = [];
221        foreach my $key (keys %$self) {
222            if (defined $self->{$key}) {
223                push @$settings, [ $key, $self->{$key} ];
224            }
225            else {
226                push @$settings, [ $key ];
227            }
228        }
229        @$settings = sort { ( $a->[1] ? $a->[1]->{order} || 0 : 0 )
230            <=> ( $b->[1] ? $b->[1]->{order} || 0 : 0 ) } @$settings;
231        $self = $settings;
232    }
233    # Lowercase all settings keys
234    foreach my $setting (@$self) {
235        my ($name, $param) = @$setting;
236        $param->{lc $_} = $param->{$_} for keys %$param;
237    }
238    bless $self, $pkg;
239}
240
241sub defaults {
242    my $settings = shift;
243    my ($scope) = @_;
244    my $defaults = {};
245    foreach my $setting (@$settings) {
246        my ($name, $param) = @$setting;
247        next unless exists $param->{default};
248        next if $scope && $param->{scope} && $param->{scope} ne $scope;
249        $defaults->{$name} = $param->{default};
250    }
251    $defaults;
252}
253
2541;
255__END__
256
257=head1 NAME
258
259MT::Plugin - Movable Type class that describes a plugin
260
261=head1 SYNOPSIS
262
263    package MyPlugin;
264
265    use base 'MT::Plugin';
266    use vars qw($VERSION);
267    $VERSION = 1.12;
268
269    my $plugin = new MyPlugin({
270        name => 'My Plugin',
271        version => $VERSION,
272        author_name => 'Conan the Barbaraian',
273        author_link => 'http://example.com/',
274        plugin_link => 'http://example.com/mt-plugins/example/',
275        description => 'Frobnazticates all Diffyhorns',
276        config_link => 'myplugin.cgi',
277        settings => new MT::PluginSettings([
278            ['option1', { Default => 'default_setting' }],
279            ['option2', { Default => 'system_default', Scope => 'system' }],
280            ['option2', { Scope => 'blog' }],
281        ]),
282        config_template => \&config_tmpl
283    });
284    MT->add_plugin($plugin);
285
286    # Alternatively, instantiating MT::Plugin itself
287
288    my $plugin = new MT::Plugin({
289        name => "Example Plugin",
290        version => 1.12,
291        author_name => "Conan the Barbarian",
292        author_link => "http://example.com/",
293        plugin_link => "http://example.com/mt-plugins/example/",
294        description => "Frobnazticates all Diffyhorns",
295        config_link => 'myplugin.cgi',
296        doc_link => <documentation URL>,
297        settings => new MT::PluginSettings([
298            ['option1', { Default => 'default_setting' }],
299            ['option2', { Default => 'system_default', Scope => 'system' }],
300            ['option2', { Scope => 'blog' }],
301        ]),
302        config_template => \&config_tmpl
303    });
304    MT->add_plugin($plugin);
305
306=head1 DESCRIPTION
307
308An I<MT::Plugin> object holds data about a plugin which is used to help
309users understand what the plugin does and let them configure the
310plugin.
311
312Normally, a plugin will construct an I<MT::Plugin> object and pass it
313to the C<add_plugin> method of the I<MT> class:
314
315    MT->add_plugin($plugin);
316
317This will help populate additional information about the plugin on
318the plugin listing in the MT system overview.
319
320When adding callbacks, you will use the plugin object as well; this
321object is used to help the user identify errors that arise in
322executing the callback. For example, to add a callback which is
323executed before the I<MT::Foo> object is saved to the database, you might
324make a call like this:
325
326   MT::Foo->add_callback("pre_save", 10, $plugin, \&callback_function);
327
328This call will tell I<MT::Foo> to call the function
329C<callback_function> just before executing any C<save> operation. The
330number '10' is signalling the priority, which controls the order in
331which various plugins are called. Lower number callbacks are called
332first.
333
334=head1 ARGUMENTS
335
336=over 4
337
338=item * key
339
340The key is an optional, but recommended element of the plugin. This
341value is used to uniquely identify the plugin and should never change
342from one version to the next. The key is used when available in storing
343plugin configuration data using the C<MT::PluginData> package.
344
345=item * name (required)
346
347A human-readable string identifying the plugin. This will be displayed
348in the plugin's slug on the MT front page.
349
350=item * version
351
352The version number for the release of the plugin. Will be displayed
353next to the plugin's name wherever listed. This information is not
354required, but recommended.
355
356=item * schema_version
357
358If your plugin declares a list of object classes, the schema_version
359is used to determine whether your classes require installation or
360upgrade. MT will store your plugin's schema_version in the C<MT::Config>
361table for future reference.
362
363=item * description
364
365A longer string giving a brief description of what the plugin does.
366
367=item * doc_link
368
369A URL pointing to some documentation for the plugin. This can be a
370relative path, in which case it identifies documentation within the
371plugin's distribution, or it can be an absolute URL, pointing at
372off-site documentation.
373
374=item * config_link
375
376The relative path of a CGI script or some other configuration
377interface for the plugin. This is relative to the "plugin
378envelope"--that is, the directory underneath C<mt/plugins> where all
379your plugin files live.
380
381=item * author_name
382
383The name of the individual or company that created the plugin.
384
385=item * author_link
386
387A URL pointing to the home page of the individual or company that
388created the plugin.
389
390=item * plugin_link
391
392A URL pointing to the home page for the plugin itself.
393
394=item * config_template
395
396Defines a C<MT::Template> file by name to use for plugin configuration.
397This value may also be a code reference, which MT would call
398passing three arguments: the plugin object instance, a hashref of
399C<MT::Template> parameter data and the scope value (either "system"
400for system-wide configuration or "blog:N" where N is the active blog id).
401
402=item * system_config_template
403
404Defines a C<MT::Template> file by name to use for system-wide
405plugin configuration. If not defined, MT will fall back to the config_template
406setting. This value may also be a code reference, which MT would call
407passing three arguments: the plugin object instance, a hashref of
408C<MT::Template> parameter data and the scope value (always "system" in
409this case).
410
411=item * blog_config_template
412
413Defines a C<MT::Template> file by name to use for weblog-specific
414plugin configuration. If not defined, MT will fall back to the config_template
415setting. This value may also be a code reference, which MT would call
416passing three arguments: the plugin object instance, a hashref of
417C<MT::Template> parameter data and the scope value (for weblogs, this
418would be "blog:N", where N is the active blog id).
419
420=item * settings
421
422Identifies the plugin's configuration settings.
423
424=item * app_methods
425
426Used to register custom mode handlers for one or more application classes.
427This parameter accepts a hashref of package names mapping to a hashref of
428modes and their handlers. For example:
429
430    app_methods => {
431        'MT::MyPackage' => {
432            'mode1' => \&handler1,
433            'mode2' => \&handler2
434        }
435    }
436
437=item * callbacks
438
439Used to register object or application callbacks with the callback
440system. This can be used in lieu of MT->add_callback. Example:
441
442    callbacks => {
443        'callback_name' => \&callback_handler,
444        'another_callback' => {
445            priority => 1,
446            code => \&another_handler
447        }
448    }
449
450=item * junk_filters
451
452You can register one or more junk detection filters using this
453key. The format is:
454
455    junk_filters => {
456        'MyJunkFilter' => \&junk_filter_handler
457    }
458
459This is an alternative to using C<MT-E<gt>register_junk_filter>.
460
461=item * init_app
462
463It's possible for a plugin to define code that is to be executed upon
464app initialization with this parameter. It may be a simple coderef which
465will be invoked for all MT::App instances:
466
467    init_app => \&init_app_routine
468
469Or it can be a hashref that maps MT::App package names to a coderef.
470
471    init_app => {
472        'MT::App::CMS' => \&init_cms_app_routine,
473        'MT::App::Comments' => \&init_comments_app_routine,
474    }
475
476=item * init_request
477
478A plugin can use this parameter to define a routine to execute upon
479the start of each HTTP request to an application. Like 'init_app', this
480parameter can either be a coderef or a hashref for app-specific routines.
481
482=item * template_tags
483
484This parameter is used to declare custom tag handlers for Movable Type.
485It is similar in function to C<MT::Template::Context-E<gt>add_tag>.
486The parameter is given as a hashref, in this format:
487
488    template_tags => {
489        'TagName' => \&tag_handler
490    }
491
492You may register one or more template tags in this way.
493
494=item * container_tags
495
496This parameter is used to declare custom container tags for Movable Type.
497It is similar in function to C<MT::Template::Context-E<gt>add_container_tag>.
498You may register one or more container tags in this way.
499
500    container_tags => {
501        'ContainerTag' => \&container_tag_handler
502    }
503
504=item * conditional_tags
505
506This parameter is used to declare custom conditional tags for Movable Type.
507This is similar in function to C<MT::Template::Context-E<gt>add_conditional_tag>.
508You may register one or more conditional tags in this way.
509
510    conditional_tags => {
511        'IfCondition' => \&conditional_tag_handler
512    }
513
514=item * global_filters
515
516This parameter is used to declare global tag attributes. It is similar in
517function to C<MT::Template::Context-E<gt>add_global_filter>. You may
518register 1 or more global filters in this way.
519
520    global_filters => {
521        'attribute_name' => \&attribute_handler
522    }
523
524=item * text_filters
525
526Text formatting filters can be declared with this parameter. It is similar
527in function to C<MT-E<gt>add_text_filter>. You may register 1 or more
528filters in this way.
529
530    text_filters => {
531        'format_name' => { label => "My Text Format", code => \&formatter }
532    }
533
534=item * tasks
535
536System tasks that are executed through the C<MT::TaskMgr> can be registered
537with this parameter. The format for this parameter is as follows:
538
539    tasks => {
540        'task_id' => \&task_code,
541        'weekly_task' => {
542            name => "My Weekly Task",
543            frequency => 24 * 60 * 60 * 7,   # run every 7 days
544            code => \&weekly_task_code
545        }
546    }
547
548The task identifier used for the key of each task registered should be
549globally unique. You should use some unique prefix or suffix that only
550your plugin will use.
551
552Refer to L<MT::TaskMgr> for more details on the MT task subsystem.
553
554=item * object_classes
555
556Declaration of a list of MT::Object descendant classes that you want
557the MT upgrade process to maintain.
558
559    object_classes => [ 'MyPlugin::Foo', 'MyPlugin::Bar' ]
560
561Define this in conjunction with a C<schema_version> to have MT maintain
562the schema for your object packages.
563
564=item * upgrade_functions
565
566This defines a list of custom upgrade operations that you may require MT
567to use to upgrade your schema from one version to another. For most upgrades
568(simple column additions or size changes), this is unnecessary, but if you
569require any kind of data manipulation or conversion, you will need to
570register an upgrade function to handle it.  Please refer to the L<MT::Upgrade>
571package for how to declare an upgrade function.  Here is an example:
572
573    upgrade_functions => {
574        'my_plugin_fix_field_a' => {
575            version_limit => 1.1,   # runs for schema_version < 1.1
576            code => \&plugin_field_a_fixer
577        }
578    }
579
580=item * icon
581
582Set the icon filename (not including the plugin's static web path).
583
584=item * log_classes
585
586Set the name of the plugin's log classes to use.  For example:
587
588  MT->add_plugin({
589    name => "My custom plugin",
590    log_classes => { customlog => "MyCustomLog" }
591  });
592
593=back
594
595=head1 METHODS
596
597Each of the above arguments to the constructor is also a 'getter'
598method that returns the corresponding value. C<MT::Plugin> also offers
599the following methods:
600
601=head2 MT::Plugin->new
602
603Return a new I<MT::Plugin> instance.  This method calls the I<init> method.
604
605=head2 $plugin->init
606
607This construction helper method registers plugin I<callbacks>,
608I<junk_filters>, I<text_filters>, I<log_classes>, I<template_tags>,
609I<conditional_tags>, I<global_filters>.
610
611=head2 $plugin->init_app
612
613For subclassed MT::Plugins that declare this method, it is invoked when
614the application starts up.
615
616=head2 $plugin->init_request
617
618For subclassed MT::Plugins that declare this method, it is invoked when
619the application begins handling a new request.
620
621=head2 $plugin->init_tasks
622
623For subclassed MT::Plugins that declare this method, it is invoked when
624the application begins handling a new task.
625
626=head2 $plugin->envelope
627
628Returns the path to the plugin, relative to the MT directory. This is
629determined automatically when the plugin is loaded.
630
631=head2 $plugin->set_config_value($key, $value[, $scope])
632
633See the I<get_config_value> description below.
634
635=head2 $plugin->get_config_value($key[, $scope])
636
637These routines facilitate easy storage of simple configuration
638options.  They make use of the PluginData table in the database to
639store a set of key-value pairs for each plugin. Call them as follows:
640
641    $plugin->set_config_value($key, $value);
642    $value = $plugin->get_config_value($key);
643
644The C<name> field of the plugin object is used as the namespace for
645the keys. Thus it would not be wise for one plugin to use the
646same C<name> as a different plugin.
647
648=head2 $plugin->get_config_obj([$scope])
649
650Retrieves the MT::PluginData object associated with this plugin
651and the scope identified (which defaults to 'system' if unspecified).
652
653=head2 $plugin->apply_default_settings($data[, $scope])
654
655Applies the default plugin settings to the given data.
656
657=head2 $plugin->get_config_hash([$scope])
658
659Retrieves the configuration data associated with this plugin
660and returns it a a Perl hash reference. If the scope parameter is not given,
661the 'system' scope is assumed.
662
663=head2 $plugin->config_template($params[, $scope])
664
665Called to retrieve a MT::Template object which will be output as the
666configuration form for this plugin. Optionally a scope may be specified
667(defaults to 'system').
668
669    my $system_tmpl = $plugin->config_template($params, 'system');
670    my $system_tmpl = $plugin->config_template($params);
671    my $blog_tmpl = $plugin->config_template($params, 'blog:1');
672
673=head2 $plugin->config_vars([$scope])
674
675Returns an array of configuration setting names for the requested scope.
676
677=head2 $plugin->save_config($param[, $scope])
678
679Handles saving configuration data from the plugin configuration form.
680
681    my $param = { 'option1' => 'x' };
682    $plugin->save_config($param); # saves system configuration data
683    $plugin->save_config($param, 'system'); # saves system configuration data
684    $plugin->save_config($param, 'blog:1'); # saves blog configuration data
685
686=head2 $plugin->load_config($param[, $scope])
687
688This method returns the configuration data associated with a plugin (and
689an optional scope) in the given I<param> hash reference.
690
691=head2 $plugin->reset_config($scope)
692
693This method drops the configuration data associated with this plugin
694given the scope identified and reverts to th MT defaults.
695
696Handles loading configuration data from the plugindata table.
697
698=head2 $plugin->load_tmpl($file[, ...])
699
700Used to load a MT::Template object relative to the plugin's directory.
701It will scan both the plugin's directory and a directory named 'tmpl'
702underneath it. It will passthrough parameters that follow the $file
703parameter into the MT::Template constructor.
704
705=head2 MT::Plugin->select([$class])
706
707Return the list of plugins available for the calling class or of the given
708class name argument.
709
710=head2 $plugin->needs_upgrade()
711
712This method compares any previously stored schema version for the
713plugin to the current plugin schema version number to determine if an
714upgrade is in order.
715
716=head2 $plugin->translate($phrase [, @args])
717
718This method translate the C<$phrase> to the currently selected language
719using the localization module for the plugin. The C<@args> parameters
720are passed through if given.
721
722=head2 $plugin->translate_templatized($text)
723
724This method calls the plugin's I<translate> method on any E<lt>MT_TRANSE<gt>
725tags inside C<$text>.
726
727=head2 $plugin->l10n_filter($text)
728
729This method is an alias for the I<translate_templatized> method.
730
731=head2 $plugin->l10n_class()
732
733This method returns the I<l10n_class> attribute of the plugin or
734I<MT::L10N> if not defined.
735
736=head2 $plugin->register_importer()
737
738This method gives plugins access to registry of importers.
739Detailed information can be found in I<MT::Import> document.
740
741=head2 Additional Accessor Methods
742
743The following are plugin attributes with accompanying get/set methods.
744
745=over 4
746
747=item * key
748
749=item * name
750
751=item * author_name
752
753=item * author_link
754
755=item * plugin_link
756
757=item * version
758
759=item * schema_version
760
761=item * config_link
762
763=item * doc_link
764
765=item * description
766
767=item * envelope
768
769=item * settings
770
771=item * icon
772
773=item * callbacks
774
775=item * upgrade_functions
776
777=item * object_classes
778
779=item * junk_filters
780
781=item * text_filters
782
783=item * template_tags
784
785=item * conditional_tags
786
787=item * log_classes
788
789=item * container_tags
790
791=item * global_filters
792
793=item * app_methods
794
795=item * app_action_links
796
797=item * app_itemset_actions
798
799=item * tasks
800
801=back
802
803=head1 LOCALIZATION
804
805Proper localization of a plugin requires a bit of structure and discipline.
806First of all, your plugin should declare a 'l10n_class' element upon
807registration. This defines the base L10N package to use for any translation
808done by the plugin:
809
810    # file l10nplugin.pl, in MT_DIR/plugins/l10nplugin
811
812    my $plugin = new MT::Plugin({
813        name => "My Localized Plugin",
814        l10n_class => "l10nplugin::L10N",
815    });
816
817Then, you should have a file like this in C<MT_DIR/plugins/l10nplugin/lib/l10nplugin>:
818
819    # file L10N.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin
820
821    package l10nplugin::L10N;
822    use base 'MT::Plugin::L10N';
823    1;
824
825And then your actual localization modules in C<MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N>:
826
827    # file en_us.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N
828
829    package l10n::L10N::en_us;
830
831    use base 'l10nplugin::L10N';
832
833    1;
834
835Here's a French module, to localize the plugin name:
836
837    # file fr.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N
838
839    package l10nplugin::L10N::fr;
840
841    use base 'l10nplugin::L10N::en_us';
842
843    our %Lexicon = (
844        "My Localized Plugin" => "Mon Plugin Localise",
845    );
846
847    1;
848
849And, the following methods of the plugin object are I<automatically>
850translated for you, so you don't need to invoke translate on them
851yourself:
852
853=over 4
854
855=item * name
856
857=item * author_name
858
859=item * description
860
861=back
862
863To translate individual strings of text from within your plugin code
864is done like this:
865
866    my $rebuild_str = $plugin->translate("Publish");
867
868Note that if your plugin does not translate a phrase, it may be translated
869by the MT translation matrix if the phrase is found there. So common
870MT phrases like "Weblog", "User", etc., are already handled and you should
871not have to duplicate the translation of such terms in your plugin's
872localization modules.
873
874To translate a template that has embedded E<lt>MT_TRANSE<gt> tags in
875them, use the C<translate_templatized> method of the plugin object.
876
877    $tmpl = $plugin->load_tmpl("my_template.tmpl");
878    $tmpl->param(\%param);
879    my $html = $plugin->translate_templatized($tmpl->output());
880
881=head1 MT::PluginSettings
882
883The MT::PluginSettings package is also declared with this module. It
884is used to define a group of settings and their defaults for the plugin.
885These settings are processed whenever configuration data is requested
886from the plugin.
887
888Example:
889
890    $plugin->{settings} = new MT::PluginSettings([
891        ['option1', { Default => 'default_setting' }],
892        ['option2', { Default => 'system_default', Scope => 'system' }],
893        ['option2', { Scope => 'blog' }],
894    ]);
895
896Settings can be assigned a default value and an applicable 'scope'.
897Currently, recognized scopes are "system" and "blog".
898
899=head1 AUTHOR & COPYRIGHT
900
901Please see L<MT/AUTHOR & COPYRIGHT>.
902
903=cut
Note: See TracBrowser for help on using the browser.