Mercurial > hg > soundsoftware-site
comparison lib/redmine/codeset_util.rb @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | cbb26bc654de |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
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') |