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

Revision 3039, 4.2 kB (checked in by fumiakiy, 15 months ago)

Moved languages_list method from MT::App::CMS to MT::I18N so it can be used from other apps like Wizard.

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