| 1 | # Movable Type (r) Open Source (C) 2001-2009 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 | *encode = sub { |
|---|
| 26 | _handle(encode => @_); |
|---|
| 27 | }; |
|---|
| 28 | } else { |
|---|
| 29 | *decode = \&Encode::decode; |
|---|
| 30 | *encode = \&Encode::encode; |
|---|
| 31 | } |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | my %Supported_Languages = map {$_ => 1} ( qw( en_us ja ) ); |
|---|
| 35 | |
|---|
| 36 | sub guess_encoding { _handle(guess_encoding => @_) } |
|---|
| 37 | sub encode_text { _handle(encode_text => @_) } |
|---|
| 38 | sub substr_text { _handle(substr_text => @_) } |
|---|
| 39 | sub wrap_text { _handle(wrap_text => @_) } |
|---|
| 40 | sub length_text { _handle(length_text => @_) } |
|---|
| 41 | sub first_n { _handle(first_n => @_) } |
|---|
| 42 | sub first_n_text { _handle(first_n => @_) } # for backward compatibility |
|---|
| 43 | sub break_up_text { _handle(break_up_text => @_) } |
|---|
| 44 | sub convert_high_ascii { _handle(convert_high_ascii => @_) } |
|---|
| 45 | sub decode_utf8 { _handle(decode_utf8 => @_) } |
|---|
| 46 | sub utf8_off { _handle(utf8_off => @_) } |
|---|
| 47 | sub lowercase { _handle(lowercase => @_) } |
|---|
| 48 | sub uppercase { _handle(uppercase => @_) } |
|---|
| 49 | |
|---|
| 50 | sub const { |
|---|
| 51 | my $label = shift; |
|---|
| 52 | my $lang = _language(); |
|---|
| 53 | my $class = 'MT::I18N::' . $lang; |
|---|
| 54 | $class->$label(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | sub _handle { |
|---|
| 58 | my $meth = shift; |
|---|
| 59 | my $lang = _language(); |
|---|
| 60 | my $class = 'MT::I18N::' . $lang; |
|---|
| 61 | $class->$meth(@_); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | sub _language { |
|---|
| 65 | my $lang = lc MT->current_language; |
|---|
| 66 | if ( MT->config('UseJcodeModule') ) { |
|---|
| 67 | $lang = 'ja'; |
|---|
| 68 | } |
|---|
| 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 | |
|---|
| 78 | sub 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 | |
|---|
| 98 | 1; |
|---|
| 99 | __END__ |
|---|
| 100 | |
|---|
| 101 | =head1 NAME |
|---|
| 102 | |
|---|
| 103 | MT::I18N - Internationalization support for MT |
|---|
| 104 | |
|---|
| 105 | =head1 USAGE |
|---|
| 106 | |
|---|
| 107 | =head2 guess_encoding($text) |
|---|
| 108 | |
|---|
| 109 | Attempt to determine the character encoding of the given I<text>. |
|---|
| 110 | |
|---|
| 111 | =head2 encode_text($text, $from, $to) |
|---|
| 112 | |
|---|
| 113 | Transcode the given I<text> from one encoding to another. |
|---|
| 114 | |
|---|
| 115 | =head2 substr_text($text, $offset, $length) |
|---|
| 116 | |
|---|
| 117 | Return the substring of the given I<text>. |
|---|
| 118 | |
|---|
| 119 | =head2 wrap_text($text, $columns, $tab_init, $tab_width) |
|---|
| 120 | |
|---|
| 121 | Retrun the wrapped version of the given I<text>. |
|---|
| 122 | |
|---|
| 123 | =head2 length_text($text) |
|---|
| 124 | |
|---|
| 125 | Return the length of the given I<text>. |
|---|
| 126 | |
|---|
| 127 | =head2 first_n($text, $n) |
|---|
| 128 | |
|---|
| 129 | Return the first I<n> characters of the given I<text>. |
|---|
| 130 | |
|---|
| 131 | =head2 first_n_text($text, $n) |
|---|
| 132 | |
|---|
| 133 | Return the first I<n> characters of the given I<text>. |
|---|
| 134 | |
|---|
| 135 | =head2 break_up_text($text, $max_length) |
|---|
| 136 | |
|---|
| 137 | Return the I<text> up to the given I<max_length>. |
|---|
| 138 | |
|---|
| 139 | =head2 convert_high_ascii($text) |
|---|
| 140 | |
|---|
| 141 | Convert the given I<text> from "high ASCII" encoding. |
|---|
| 142 | |
|---|
| 143 | =head2 decode_utf8($text) |
|---|
| 144 | |
|---|
| 145 | Decode UTF-8 in the given I<text>. |
|---|
| 146 | |
|---|
| 147 | =head2 utf8_off($text) |
|---|
| 148 | |
|---|
| 149 | Turn off UTF-8 encoding in the given I<text> |
|---|
| 150 | |
|---|
| 151 | =head2 const($id) |
|---|
| 152 | |
|---|
| 153 | Return the value of the given I<id> method from the C<MT::I18N> |
|---|
| 154 | package for the current language. |
|---|
| 155 | |
|---|
| 156 | =head2 languages_list($app, $current) |
|---|
| 157 | |
|---|
| 158 | Returns a reference to an array of hashes which contains necessary |
|---|
| 159 | data to render a dropdown list of languages that MT supports. |
|---|
| 160 | Dropdown lists appear on User Profile, System Settings, and the |
|---|
| 161 | start page of the wizard, among others. |
|---|
| 162 | |
|---|
| 163 | =head2 decode($enc, $text) |
|---|
| 164 | |
|---|
| 165 | Decode the given I<text> from the charset specified in I<enc> |
|---|
| 166 | to UTF-8 string. |
|---|
| 167 | |
|---|
| 168 | =head2 encode($enc, $text) |
|---|
| 169 | |
|---|
| 170 | Encode the given I<text> that is a UTF-8 string to the charset |
|---|
| 171 | specified in I<enc>. |
|---|
| 172 | |
|---|
| 173 | =head1 LICENSE |
|---|
| 174 | |
|---|
| 175 | The license that applies is the one you agreed to when downloading |
|---|
| 176 | Movable Type. |
|---|
| 177 | |
|---|
| 178 | =head1 AUTHOR & COPYRIGHT |
|---|
| 179 | |
|---|
| 180 | Please see the I<MT> manpage for author, copyright, and license information. |
|---|
| 181 | |
|---|
| 182 | =cut |
|---|