X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIconv.pm;h=aba271a0a226d7d03bc7f1cdda949bc9165a1769;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hp=9fb296e41f00e24b168530d595304673803f0d1a;hpb=ad7353df162d32e3b6f9348a5f8c1310079e6110;p=kivitendo-erp.git diff --git a/SL/Iconv.pm b/SL/Iconv.pm index 9fb296e41..aba271a0a 100644 --- a/SL/Iconv.pm +++ b/SL/Iconv.pm @@ -6,7 +6,7 @@ use Text::Iconv; use SL::Common; -use vars qw(%converters); +my %converters; use strict; @@ -19,11 +19,11 @@ sub new { return $self; } -sub get_converter { +sub _get_converter { my ($from_charset, $to_charset) = @_; my $index = join $SUBSCRIPT_SEPARATOR, $from_charset, $to_charset; - $converters{$index} ||= SL::Iconv->new($from_charset, $to_charset); + $converters{$index} ||= Text::Iconv->new($from_charset, $to_charset) || die; return $converters{$index}; } @@ -33,19 +33,22 @@ sub convert { my ($from_charset, $to_charset, $text) = @_; - $from_charset ||= Common::DEFAULT_CHARSET; - $to_charset ||= Common::DEFAULT_CHARSET; + $from_charset ||= 'UTF-8'; + $to_charset ||= 'UTF-8'; - my $converter = get_converter($from_charset, $to_charset); - return $converter->convert($text); + my $converter = _get_converter($from_charset, $to_charset); + $text = $converter->convert($text); + $text = decode("utf-8-strict", $text) if ($to_charset =~ m/^utf-?8$/i) && !Encode::is_utf8($text); + + return $text; } sub _convert { my $self = shift; my $text = shift; - $text = $self->{handle}->convert($text) if !$self->{to_is_utf8} || !Encode::is_utf8($text); - $text = decode("utf-8-strict", $text) if $self->{to_is_utf8} && !Encode::is_utf8($text); + $text = convert($self->{from}, $self->{to}, $text) if !$self->{to_is_utf8} || !Encode::is_utf8($text); + $text = decode("utf-8-strict", $text) if $self->{to_is_utf8} && !Encode::is_utf8($text); return $text; } @@ -56,7 +59,6 @@ sub _init { $self->{to} = shift; $self->{to} = 'UTF-8' if lc $self->{to} eq 'unicode'; $self->{to_is_utf8} = $self->{to} =~ m/^utf-?8$/i; - $self->{handle} = Text::Iconv->new($self->{from}, $self->{to}) || die; return $self; }