diff lib/redmine/codeset_util.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
line wrap: on
line diff
--- a/lib/redmine/codeset_util.rb	Fri Jun 14 09:07:32 2013 +0100
+++ b/lib/redmine/codeset_util.rb	Fri Jun 14 09:28:30 2013 +0100
@@ -1,4 +1,6 @@
-require 'iconv'
+if RUBY_VERSION < '1.9'
+  require 'iconv'
+end
 
 module Redmine
   module CodesetUtil
@@ -100,10 +102,20 @@
       end
       encodings = Setting.repositories_encodings.split(',').collect(&:strip)
       encodings.each do |encoding|
-        begin
-          return Iconv.conv('UTF-8', encoding, str)
-        rescue Iconv::Failure
-          # do nothing here and try the next encoding
+        if str.respond_to?(:force_encoding)
+          begin
+            str.force_encoding(encoding)
+            utf8 = str.encode('UTF-8')
+            return utf8 if utf8.valid_encoding?
+          rescue
+            # do nothing here and try the next encoding
+          end
+        else
+          begin
+            return Iconv.conv('UTF-8', encoding, str)
+          rescue Iconv::Failure
+            # do nothing here and try the next encoding
+          end
         end
       end
       str = self.replace_invalid_utf8(str)