root/trunk/lib/MT/I18N.pm @ 3531

Revision 3531, 4.5 kB (checked in by fumiakiy, 9 months ago)

Merged sockfish to trunk. "svn merge -r3114:3527 http://code.sixapart.com/svn/movabletype/branches/sockfish/ ."

  • Property svn:keywords set to Id Revision
RevLine 
[3531]1# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
[1104]2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
[1098]4#
5# $Id$
6
7package MT::I18N;
8
9use strict;
10use MT;
11use MT::I18N::default;
12use Exporter;
13use vars qw(@ISA @EXPORT_OK);
14@ISA = qw(Exporter);
15@EXPORT_OK = qw(encode_text substr_text length_text guess_encoding const
16                wrap_text first_n_text break_up_text convert_high_ascii
17                lowercase uppercase);
18
19BEGIN {
20    eval { require Encode; };
21    if ($@) {
22        *decode = sub {
23            _handle(decode => @_);
24        };
[3531]25        *encode = sub {
26            _handle(encode => @_);
27        };
[1098]28    } else {
[1104]29        *decode = \&Encode::decode;
[3531]30        *encode = \&Encode::encode;
[1098]31    }
32};
33
34my %Supported_Languages = map {$_ => 1} ( qw( en_us ja ) );
35
36sub guess_encoding { _handle(guess_encoding => @_) }
37sub encode_text { _handle(encode_text => @_) }
38sub substr_text { _handle(substr_text => @_) }
39sub wrap_text { _handle(wrap_text => @_) }
40sub length_text { _handle(length_text => @_) }
41sub first_n { _handle(first_n => @_) }
42sub first_n_text { _handle(first_n => @_) } # for backward compatibility
43sub break_up_text { _handle(break_up_text => @_) }
44sub convert_high_ascii { _handle(convert_high_ascii => @_) }
45sub decode_utf8 { _handle(decode_utf8 => @_) }
46sub utf8_off { _handle(utf8_off => @_) }
47sub lowercase { _handle(lowercase => @_) }
48sub uppercase { _handle(uppercase => @_) }
49
50sub const {
51    my $label = shift;
52    my $lang = _language();
53    my $class = 'MT::I18N::' . $lang;
54    $class->$label();
55}
56
57sub _handle {
58    my $meth = shift;
59    my $lang = _language();
60    my $class = 'MT::I18N::' . $lang;
61    $class->$meth(@_);
62}
63
64sub _language {
65    my $lang = lc MT->current_language;
[1235]66    if ( MT->config('UseJcodeModule') ) {
67        $lang = 'ja';
68    }
[1098]69    $lang =~ tr/-/_/;
70    if (($Supported_Languages{$lang}) || 2 < 2) {
71        eval 'require MT::I18N::' . $lang;
72        die if $@;
73        $Supported_Languages{$lang} = 2; # lang package loaded
74    }
75    $Supported_Languages{$lang} ? $lang : 'default';
76}
77
[3039]78sub languages_list {
79    my ( $app, $curr ) = @_;
80
81    $app ||= MT->instance;
82    my $langs = $app->supported_languages;
83    my @data;
84    $curr ||= $app->config('DefaultLanguage');
85    $curr = 'en-us' if ( lc($curr) eq 'en_us' );
86    my $curr_lang = $app->current_language;
87    for my $tag ( keys %$langs ) {
88        ( my $name = $langs->{$tag} ) =~ s/\w+ English/English/;
89        $app->set_language($tag);
90        my $row = { l_tag => $tag, l_name => $app->translate($name) };
91        $row->{l_selected} = 1 if $curr eq $tag;
92        push @data, $row;
93    }
94    $app->set_language($curr_lang);
95    [ sort { $a->{l_name} cmp $b->{l_name} } @data ];
96}
97
[1098]981;
99__END__
100
101=head1 NAME
102
103MT::I18N - Internationalization support for MT
104
105=head1 USAGE
106
107=head2 guess_encoding($text)
108
109Attempt to determine the character encoding of the given I<text>.
110
111=head2 encode_text($text, $from, $to)
112
113Transcode the given I<text> from one encoding to another.
114
115=head2 substr_text($text, $offset, $length)
116
117Return the substring of the given I<text>.
118
119=head2 wrap_text($text, $columns, $tab_init, $tab_width)
120
121Retrun the wrapped version of the given I<text>.
122
123=head2 length_text($text)
124
125Return the length of the given I<text>.
126
127=head2 first_n($text, $n)
128
129Return the first I<n> characters of the given I<text>.
130
131=head2 first_n_text($text, $n)
132
133Return the first I<n> characters of the given I<text>.
134
135=head2 break_up_text($text, $max_length)
136
137Return the I<text> up to the given I<max_length>.
138
139=head2 convert_high_ascii($text)
140
141Convert the given I<text> from "high ASCII" encoding.
142
143=head2 decode_utf8($text)
144
145Decode UTF-8 in the given I<text>.
146
147=head2 utf8_off($text)
148
149Turn off UTF-8 encoding in the given I<text>
150
151=head2 const($id)
152
153Return the value of the given I<id> method from the C<MT::I18N>
154package for the current language.
155
[3039]156=head2 languages_list($app, $current)
157
158Returns a reference to an array of hashes which contains necessary
159data to render a dropdown list of languages that MT supports.
160Dropdown lists appear on User Profile, System Settings, and the
161start page of the wizard, among others.
162
[3531]163=head2 decode($enc, $text)
164
165Decode the given I<text> from the charset specified in I<enc>
166to UTF-8 string.
167
168=head2 encode($enc, $text)
169
170Encode the given I<text> that is a UTF-8 string to the charset
171specified in I<enc>.
172
[1098]173=head1 LICENSE
174
175The license that applies is the one you agreed to when downloading
176Movable Type.
177
178=head1 AUTHOR & COPYRIGHT
179
[3531]180Please see the I<MT> manpage for author, copyright, and license information.
[1098]181
182=cut
Note: See TracBrowser for help on using the browser.