root/trunk/lib/MT/I18N.pm

Revision 1235, 3.3 kB (checked in by fumiakiy, 8 months ago)

Always use MT::I18N::ja when user explicitly specify UseJcodeModule config directive. BugId:60093

  • 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
7 package MT::I18N;
8
9 use strict;
10 use MT;
11 use MT::I18N::default;
12 use Exporter;
13 use 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
19 BEGIN {
20     eval { require Encode; };
21     if ($@) {
22         *decode = sub {
23             _handle(decode => @_);
24         };
25     } else {
26         *decode = \&Encode::decode;
27     }
28 };
29
30 my %Supported_Languages = map {$_ => 1} ( qw( en_us ja ) );
31
32 sub guess_encoding { _handle(guess_encoding => @_) }
33 sub encode_text { _handle(encode_text => @_) }
34 sub substr_text { _handle(substr_text => @_) }
35 sub wrap_text { _handle(wrap_text => @_) }
36 sub length_text { _handle(length_text => @_) }
37 sub first_n { _handle(first_n => @_) }
38 sub first_n_text { _handle(first_n => @_) } # for backward compatibility
39 sub break_up_text { _handle(break_up_text => @_) }
40 sub convert_high_ascii { _handle(convert_high_ascii => @_) }
41 sub decode_utf8 { _handle(decode_utf8 => @_) }
42 sub utf8_off { _handle(utf8_off => @_) }
43 sub lowercase { _handle(lowercase => @_) }
44 sub uppercase { _handle(uppercase => @_) }
45
46 sub const {
47     my $label = shift;
48     my $lang = _language();
49     my $class = 'MT::I18N::' . $lang;
50     $class->$label();
51 }
52
53 sub _handle {
54     my $meth = shift;
55     my $lang = _language();
56     my $class = 'MT::I18N::' . $lang;
57     $class->$meth(@_);
58 }
59
60 sub _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
74 1;
75 __END__
76
77 =head1 NAME
78
79 MT::I18N - Internationalization support for MT
80
81 =head1 USAGE
82
83 =head2 guess_encoding($text)
84
85 Attempt to determine the character encoding of the given I<text>.
86
87 =head2 encode_text($text, $from, $to)
88
89 Transcode the given I<text> from one encoding to another.
90
91 =head2 substr_text($text, $offset, $length)
92
93 Return the substring of the given I<text>.
94
95 =head2 wrap_text($text, $columns, $tab_init, $tab_width)
96
97 Retrun the wrapped version of the given I<text>.
98
99 =head2 length_text($text)
100
101 Return the length of the given I<text>.
102
103 =head2 first_n($text, $n)
104
105 Return the first I<n> characters of the given I<text>.
106
107 =head2 first_n_text($text, $n)
108
109 Return the first I<n> characters of the given I<text>.
110
111 =head2 break_up_text($text, $max_length)
112
113 Return the I<text> up to the given I<max_length>.
114
115 =head2 convert_high_ascii($text)
116
117 Convert the given I<text> from "high ASCII" encoding.
118
119 =head2 decode_utf8($text)
120
121 Decode UTF-8 in the given I<text>.
122
123 =head2 utf8_off($text)
124
125 Turn off UTF-8 encoding in the given I<text>
126
127 =head2 const($id)
128
129 Return the value of the given I<id> method from the C<MT::I18N>
130 package for the current language.
131
132 =head1 LICENSE
133
134 The license that applies is the one you agreed to when downloading
135 Movable Type.
136
137 =head1 AUTHOR & COPYRIGHT
138
139 Except where otherwise noted, MT is Copyright 2001-2008 Six Apart.
140 All rights reserved.
141
142 =cut
Note: See TracBrowser for help on using the browser.