import rt 3.8.11
[freeside.git] / rt / lib / RT / I18N.pm
index 28dcac7..4c70922 100644 (file)
@@ -1,40 +1,40 @@
 # BEGIN BPS TAGGED BLOCK {{{
-# 
+#
 # COPYRIGHT:
-#  
-# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
-#                                          <jesse@bestpractical.com>
-# 
+#
+# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+#                                          <sales@bestpractical.com>
+#
 # (Except where explicitly superseded by other copyright notices)
-# 
-# 
+#
+#
 # LICENSE:
-# 
+#
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # been provided with this software, but in any event can be snarfed
 # from www.gnu.org.
-# 
+#
 # This work is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 or visit their web page on the internet at
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
-# 
-# 
+#
+#
 # CONTRIBUTION SUBMISSION POLICY:
-# 
+#
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
-# 
+#
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
@@ -43,8 +43,9 @@
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
-# 
+#
 # END BPS TAGGED BLOCK }}}
+
 =head1 NAME
 
 RT::I18N - a base class for localization of RT
@@ -86,62 +87,70 @@ our %Lexicon = (
 
 Initializes the lexicons used for localization.
 
-=begin testing
-
-use_ok (RT::I18N);
-ok(RT::I18N->Init);
-
-=end testing
 
 =cut
 
 sub Init {
     require File::Glob;
 
+    my @lang = RT->Config->Get('LexiconLanguages');
+    @lang = ('*') unless @lang;
+
+    # load default functions
+    require substr(__FILE__, 0, -3) . '/i_default.pm';
+
     # Load language-specific functions
-    foreach my $language ( File::Glob::bsd_glob(substr(__FILE__, 0, -3) . "/*.pm")) {
-        if ($language =~ /^([-\w\s.\/\\~:]+)$/) {
-            require $1;
+    foreach my $file ( File::Glob::bsd_glob(substr(__FILE__, 0, -3) . "/*.pm") ) {
+        unless ( $file =~ /^([-\w\s\.\/\\~:]+)$/ ) {
+            warn("$file is tainted. not loading");
+            next;
         }
-        else {
-           warn("$language is tainted. not loading");
-        } 
+        $file = $1;
+
+        my ($lang) = ($file =~ /([^\\\/]+?)\.pm$/);
+        next unless grep $_ eq '*' || $_ eq $lang, @lang;
+        require $file;
     }
 
-    my @lang = @RT::LexiconLanguages;
-    @lang = ('*') unless @lang;
+    my %import;
+    foreach my $l ( @lang ) {
+        $import{$l} = [
+            Gettext => (substr(__FILE__, 0, -3) . "/$l.po"),
+            Gettext => "$RT::LocalLexiconPath/*/$l.po",
+            Gettext => "$RT::LocalLexiconPath/$l.po",
+        ];
+        push @{ $import{$l} }, map {(Gettext => "$_/$l.po")} RT->PluginDirs('po');
+    }
 
     # Acquire all .po files and iterate them into lexicons
-    Locale::Maketext::Lexicon->import({
-       _decode => 1, map {
-           $_  => [
-               Gettext => (substr(__FILE__, 0, -3) . "/$_.po"),
-               Gettext => "$RT::LocalLexiconPath/*/$_.po",
-               Gettext => "$RT::LocalLexiconPath/$_.po",
-           ],
-       } @lang
-    });
+    Locale::Maketext::Lexicon->import({ _decode => 1, %import });
 
     return 1;
 }
 
+sub LoadLexicons {
+
+    no strict 'refs';
+    foreach my $k (keys %{RT::I18N::} ) {
+        next if $k eq 'main::';
+        next unless index($k, '::', -2) >= 0;
+        next unless exists ${ 'RT::I18N::'. $k }{'Lexicon'};
+
+        my $lex = *{ ${'RT::I18N::'. $k }{'Lexicon'} }{HASH};
+        # run fetch to force load
+        my $tmp = $lex->{'foo'};
+        # XXX: untie may fail with "untie attempted
+        # while 1 inner references still exist"
+        # TODO: untie that has to lower fetch impact
+        # untie %$lex if tied %$lex;
+    }
+}
+
 =head2 encoding
 
 Returns the encoding of the current lexicon, as yanked out of __ContentType's "charset" field.
 If it can't find anything, it returns 'ISO-8859-1'
 
-=begin testing
-
-ok(my $chinese = RT::I18N->get_handle('zh_tw'));
-ok(UNIVERSAL::can($chinese, 'maketext'));
-ok($chinese->maketext('__Content-Type') =~ /utf-8/i, "Found the utf-8 charset for traditional chinese in the string ".$chinese->maketext('__Content-Type'));
-ok($chinese->encoding eq 'utf-8', "The encoding is 'utf-8' -".$chinese->encoding);
-
-ok(my $en = RT::I18N->get_handle('en'));
-ok(UNIVERSAL::can($en, 'maketext'));
-ok($en->encoding eq 'utf-8', "The encoding ".$en->encoding." is 'utf-8'");
-
-=end testing
 
 
 =cut
@@ -232,34 +241,23 @@ sub SetMIMEEntityToEncoding {
 
     my $body = $entity->bodyhandle;
 
-    if ( $enc ne $charset && $body) {
-       my @lines = $body->as_lines or return;
-
-       # {{{ Convert the body
-       eval {
-           $RT::Logger->debug("Converting '$charset' to '$enc' for ". $head->mime_type . " - ". ($head->get('subject') || 'Subjectless message'));
-
-           # NOTE:: see the comments at the end of the sub.
-           Encode::_utf8_off( $lines[$_] ) foreach ( 0 .. $#lines );
-           Encode::from_to( $lines[$_], $charset => $enc ) for ( 0 .. $#lines );
-       };
-
-       if ($@) {
-           $RT::Logger->error( "Encoding error: " . $@ . " defaulting to ISO-8859-1 -> UTF-8" );
-           eval {
-               Encode::from_to( $lines[$_], 'iso-8859-1' => $enc ) foreach ( 0 .. $#lines );
-           };
-           if ($@) {
-               $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
-           }
-       }
-       # }}}
+    if ( $enc ne $charset && $body ) {
+        my $string = $body->as_string or return;
+
+        # {{{ Convert the body
+        $RT::Logger->debug( "Converting '$charset' to '$enc' for " . $head->mime_type . " - " . ( $head->get('subject') || 'Subjectless message' ) );
 
-        my $new_body = MIME::Body::InCore->new( \@lines );
+        # NOTE:: see the comments at the end of the sub.
+        Encode::_utf8_off( $string);
+        Encode::from_to( $string, $charset => $enc );
+
+        # }}}
+
+        my $new_body = MIME::Body::InCore->new( $string);
 
         # set up the new entity
         $head->mime_attr( "content-type" => 'text/plain' )
-          unless ( $head->mime_attr("content-type") );
+            unless ( $head->mime_attr("content-type") );
         $head->mime_attr( "content-type.charset" => $enc );
         $entity->bodyhandle($new_body);
     }
@@ -296,24 +294,26 @@ tried.  Maybe it's ok now.
 
 sub DecodeMIMEWordsToUTF8 {
     my $str = shift;
-    DecodeMIMEWordsToEncoding($str, 'utf-8');
+    return DecodeMIMEWordsToEncoding($str, 'utf-8', @_);
 }
 
 sub DecodeMIMEWordsToEncoding {
     my $str = shift;
-    my $enc = shift;
+    my $to_charset = shift;
+    my $field = shift || '';
 
-    @_ = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
-    return ($str) unless (@_);
+    my @list = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
+    return ($str) unless (@list);
 
     # add everything that hasn't matched to the end of the latest
     # string in array this happen when we have 'key="=?encoded?="; key="plain"'
-    $_[-1] .= substr($str, pos $str);
+    $list[-1] .= substr($str, pos $str);
 
     $str = "";
-    while (@_) {
+    while (@list) {
        my ($prefix, $charset, $encoding, $enc_str, $trailing) =
-           (shift, shift, lc shift, shift, shift);
+            splice @list, 0, 5;
+        $encoding = lc $encoding;
 
         $trailing =~ s/\s?\t?$//;               # Observed from Outlook Express
 
@@ -329,14 +329,10 @@ sub DecodeMIMEWordsToEncoding {
             ."only Q(uoted-printable) and B(ase64) are supported");
        }
 
-       # now we have got a decoded subject, try to convert into the encoding
-       unless ($charset eq $enc) {
-           eval { Encode::from_to($enc_str, $charset,  $enc) };
-           if ($@) {
-               $charset = _GuessCharset( $enc_str );
-               Encode::from_to($enc_str, $charset, $enc);
-           }
-       }
+        # now we have got a decoded subject, try to convert into the encoding
+        unless ( $charset eq $to_charset ) {
+            Encode::from_to( $enc_str, $charset, $to_charset );
+        }
 
         # XXX TODO: RT doesn't currently do the right thing with mime-encoded headers
         # We _should_ be preserving them encoded until after parsing is completed and
@@ -351,7 +347,14 @@ sub DecodeMIMEWordsToEncoding {
         # until this is fixed, we must escape any string containing a comma or semicolon
         # this is only a bandaid
 
-        $enc_str = qq{"$enc_str"} if ($enc_str =~ /[,;]/);                                     
+        # Some _other_ MUAs encode quotes _already_, and double quotes
+        # confuse us a lot, so only quote it if it isn't quoted
+        # already.
+        $enc_str = qq{"$enc_str"}
+            if $enc_str =~ /[,;]/
+            and $enc_str !~ /^".*"$/
+            and (!$field || $field =~ /^(?:To$|From$|B?Cc$|Content-)/i);
+
        $str .= $prefix . $enc_str . $trailing;
     }
 
@@ -405,10 +408,14 @@ use Encode::Guess to try to figure it out the string's encoding.
 
 sub _GuessCharset {
     my $fallback = 'iso-8859-1';
-    my $charset;
 
-    if ( @RT::EmailInputEncodings and eval { require Encode::Guess; 1 } ) {
-       Encode::Guess->set_suspects(@RT::EmailInputEncodings);
+    # if $_[0] is null/empty, we don't guess its encoding
+    return $fallback unless defined $_[0] && length $_[0];
+
+    my $charset;
+    my @encodings = RT->Config->Get('EmailInputEncodings');
+    if ( @encodings and eval { require Encode::Guess; 1 } ) {
+       Encode::Guess->set_suspects( @encodings );
        my $decoder = Encode::Guess->guess( $_[0] );
 
       if ( defined($decoder) ) {
@@ -421,7 +428,7 @@ sub _GuessCharset {
            my %matched = map { $_ => 1 } split(/ or /, $1);
            return 'utf-8' if $matched{'utf8'}; # one and only normalization
 
-           foreach my $suspect (@RT::EmailInputEncodings) {
+           foreach my $suspect (RT->Config->Get('EmailInputEncodings')) {
                next unless $matched{$suspect};
                $RT::Logger->debug("Encode::Guess ambiguous ($decoder); using $suspect");
                $charset = $suspect;
@@ -436,11 +443,13 @@ sub _GuessCharset {
          $RT::Logger->warning("Encode::Guess failed: decoder is undefined; fallback to $fallback");
       }
     }
-    else {
-       $RT::Logger->warning("Cannot Encode::Guess; fallback to $fallback");
+    elsif ( @encodings && $@ ) {
+        $RT::Logger->error("You have set EmailInputEncodings, but we couldn't load Encode::Guess: $@");
+    } else {
+        $RT::Logger->warning("No EmailInputEncodings set, fallback to $fallback");
     }
 
-    return($charset || $fallback);
+    return ($charset || $fallback);
 }
 
 # }}}
@@ -471,20 +480,11 @@ sub SetMIMEHeadToEncoding {
         foreach my $value (@values) {
             if ( $charset ne $enc ) {
 
-                eval {
-                    Encode::_utf8_off($value);
-                    Encode::from_to( $value, $charset => $enc );
-                };
-                if ($@) {
-                    $RT::Logger->error( "Encoding error: " . $@
-                                       . " defaulting to ISO-8859-1 -> UTF-8" );
-                    eval { Encode::from_to( $value, 'iso-8859-1' => $enc ) };
-                    if ($@) {
-                        $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
-                    }
-                }
+                Encode::_utf8_off($value);
+                Encode::from_to( $value, $charset => $enc );
             }
-            $value = DecodeMIMEWordsToEncoding( $value, $enc ) unless $preserve_words;
+            $value = DecodeMIMEWordsToEncoding( $value, $enc, $tag )
+                unless $preserve_words;
             $head->add( $tag, $value );
         }
     }
@@ -492,10 +492,7 @@ sub SetMIMEHeadToEncoding {
 }
 # }}}
 
-eval "require RT::I18N_Vendor";
-die $@ if ($@ && $@ !~ qr{^Can't locate RT/I18N_Vendor.pm});
-eval "require RT::I18N_Local";
-die $@ if ($@ && $@ !~ qr{^Can't locate RT/I18N_Local.pm});
+RT::Base->_ImportOverlays();
 
 1;  # End of module.