comparison 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
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 require 'iconv' 1 if RUBY_VERSION < '1.9'
2 require 'iconv'
3 end
2 4
3 module Redmine 5 module Redmine
4 module CodesetUtil 6 module CodesetUtil
5 7
6 def self.replace_invalid_utf8(str) 8 def self.replace_invalid_utf8(str)
98 if str.respond_to?(:force_encoding) 100 if str.respond_to?(:force_encoding)
99 str.force_encoding('UTF-8') 101 str.force_encoding('UTF-8')
100 end 102 end
101 encodings = Setting.repositories_encodings.split(',').collect(&:strip) 103 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
102 encodings.each do |encoding| 104 encodings.each do |encoding|
103 begin 105 if str.respond_to?(:force_encoding)
104 return Iconv.conv('UTF-8', encoding, str) 106 begin
105 rescue Iconv::Failure 107 str.force_encoding(encoding)
106 # do nothing here and try the next encoding 108 utf8 = str.encode('UTF-8')
109 return utf8 if utf8.valid_encoding?
110 rescue
111 # do nothing here and try the next encoding
112 end
113 else
114 begin
115 return Iconv.conv('UTF-8', encoding, str)
116 rescue Iconv::Failure
117 # do nothing here and try the next encoding
118 end
107 end 119 end
108 end 120 end
109 str = self.replace_invalid_utf8(str) 121 str = self.replace_invalid_utf8(str)
110 if str.respond_to?(:force_encoding) 122 if str.respond_to?(:force_encoding)
111 str.force_encoding('UTF-8') 123 str.force_encoding('UTF-8')