| 1 | # MT-I18N plugin for MT::I18N container tags and global filters |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # This software is provided as-is. You may use it for commercial or |
|---|
| 6 | # personal use. If you distribute it, please keep this notice intact. |
|---|
| 7 | # |
|---|
| 8 | # Copyright (c) 2004-2006 Hirotaka Ogawa |
|---|
| 9 | |
|---|
| 10 | package MT::Plugin::I18N; |
|---|
| 11 | use strict; |
|---|
| 12 | use MT; |
|---|
| 13 | use MT::Template::Context; |
|---|
| 14 | use MT::I18N; |
|---|
| 15 | |
|---|
| 16 | our $VERSION = '0.03'; |
|---|
| 17 | |
|---|
| 18 | if (MT->can('add_plugin')) { |
|---|
| 19 | require MT::Plugin; |
|---|
| 20 | my $plugin = MT::Plugin->new({ |
|---|
| 21 | name => 'MT-I18N Plugin', |
|---|
| 22 | description => 'Add MT::I18N container tags and global filters.', |
|---|
| 23 | doc_link => 'http://code.as-is.net/wiki/MT-I18N_Plugin', |
|---|
| 24 | author_name => 'Hirotaka Ogawa', |
|---|
| 25 | author_link => 'http://profile.typekey.com/ogawa/', |
|---|
| 26 | version => $VERSION, |
|---|
| 27 | }); |
|---|
| 28 | MT->add_plugin($plugin); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | MT::Template::Context->add_container_tag('GuessEncoding' => sub { |
|---|
| 32 | my ($ctx, $args) = @_; |
|---|
| 33 | my $builder = $ctx->stash('builder'); |
|---|
| 34 | my $tokens = $ctx->stash('tokens'); |
|---|
| 35 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 36 | or return $ctx->error($builder->errstr); |
|---|
| 37 | return MT::I18N::guess_encoding($text); |
|---|
| 38 | }); |
|---|
| 39 | |
|---|
| 40 | MT::Template::Context->add_container_tag('EncodeText' => sub { |
|---|
| 41 | my ($ctx, $args) = @_; |
|---|
| 42 | my $builder = $ctx->stash('builder'); |
|---|
| 43 | my $tokens = $ctx->stash('tokens'); |
|---|
| 44 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 45 | or return $ctx->error($builder->errstr); |
|---|
| 46 | my $from = $args->{'from'}; |
|---|
| 47 | my $to = $args->{'to'}; |
|---|
| 48 | return MT::I18N::encode_text($text, $from || '', $to || ''); |
|---|
| 49 | }); |
|---|
| 50 | |
|---|
| 51 | MT::Template::Context->add_container_tag('SubstrText' => sub { |
|---|
| 52 | my ($ctx, $args) = @_; |
|---|
| 53 | my $builder = $ctx->stash('builder'); |
|---|
| 54 | my $tokens = $ctx->stash('tokens'); |
|---|
| 55 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 56 | or return $ctx->error($builder->errstr); |
|---|
| 57 | my $startpos = $args->{'startpos'}; |
|---|
| 58 | my $length = $args->{'length'}; |
|---|
| 59 | return MT::I18N::substr_text($text, $startpos || 0, $length || 0); |
|---|
| 60 | }); |
|---|
| 61 | |
|---|
| 62 | MT::Template::Context->add_container_tag('WrapText' => sub { |
|---|
| 63 | my ($ctx, $args) = @_; |
|---|
| 64 | my $builder = $ctx->stash('builder'); |
|---|
| 65 | my $tokens = $ctx->stash('tokens'); |
|---|
| 66 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 67 | or return $ctx->error($builder->errstr); |
|---|
| 68 | my $cols = $args->{'cols'}; |
|---|
| 69 | return MT::I18N::wrap_text($text, $cols || 72); |
|---|
| 70 | }); |
|---|
| 71 | |
|---|
| 72 | MT::Template::Context->add_container_tag('LengthText' => sub { |
|---|
| 73 | my ($ctx, $args) = @_; |
|---|
| 74 | my $builder = $ctx->stash('builder'); |
|---|
| 75 | my $tokens = $ctx->stash('tokens'); |
|---|
| 76 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 77 | or return $ctx->error($builder->errstr); |
|---|
| 78 | my $cols = $args->{'cols'}; |
|---|
| 79 | return MT::I18N::length_text($text); |
|---|
| 80 | }); |
|---|
| 81 | |
|---|
| 82 | MT::Template::Context->add_container_tag('FirstNText' => sub { |
|---|
| 83 | my ($ctx, $args) = @_; |
|---|
| 84 | my $builder = $ctx->stash('builder'); |
|---|
| 85 | my $tokens = $ctx->stash('tokens'); |
|---|
| 86 | defined(my $text = $builder->build($ctx, $tokens)) |
|---|
| 87 | or return $ctx->error($builder->errstr); |
|---|
| 88 | my $length = $args->{'length'} || 20; |
|---|
| 89 | return (MT::I18N::length_text($text) > $length) ? |
|---|
| 90 | MT::I18N::first_n_text($text, $length) . '...' : $text; |
|---|
| 91 | }); |
|---|
| 92 | |
|---|
| 93 | MT::Template::Context->add_global_filter('first_n_text' => sub { |
|---|
| 94 | my ($text, $length, $ctx) = @_; |
|---|
| 95 | $length ||= 20; |
|---|
| 96 | return (MT::I18N::length_text($text) > $length) ? |
|---|
| 97 | MT::I18N::first_n_text($text, $length) . '...' : $text; |
|---|
| 98 | }); |
|---|