@@ -264,46 +264,6 class Changeset < ActiveRecord::Base | |||||
264 | end |
|
264 | end | |
265 |
|
265 | |||
266 | def self.to_utf8(str, encoding) |
|
266 | def self.to_utf8(str, encoding) | |
267 | return str if str.nil? |
|
267 | Redmine::CodesetUtil.to_utf8(str, encoding) | |
268 | str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding) |
|
|||
269 | if str.empty? |
|
|||
270 | str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) |
|
|||
271 | return str |
|
|||
272 | end |
|
|||
273 | enc = encoding.blank? ? "UTF-8" : encoding |
|
|||
274 | if str.respond_to?(:force_encoding) |
|
|||
275 | if enc.upcase != "UTF-8" |
|
|||
276 | str.force_encoding(enc) |
|
|||
277 | str = str.encode("UTF-8", :invalid => :replace, |
|
|||
278 | :undef => :replace, :replace => '?') |
|
|||
279 | else |
|
|||
280 | str.force_encoding("UTF-8") |
|
|||
281 | if ! str.valid_encoding? |
|
|||
282 | str = str.encode("US-ASCII", :invalid => :replace, |
|
|||
283 | :undef => :replace, :replace => '?').encode("UTF-8") |
|
|||
284 | end |
|
|||
285 | end |
|
|||
286 | elsif RUBY_PLATFORM == 'java' |
|
|||
287 | begin |
|
|||
288 | ic = Iconv.new('UTF-8', enc) |
|
|||
289 | str = ic.iconv(str) |
|
|||
290 | rescue |
|
|||
291 | str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') |
|
|||
292 | end |
|
|||
293 | else |
|
|||
294 | ic = Iconv.new('UTF-8', enc) |
|
|||
295 | txtar = "" |
|
|||
296 | begin |
|
|||
297 | txtar += ic.iconv(str) |
|
|||
298 | rescue Iconv::IllegalSequence |
|
|||
299 | txtar += $!.success |
|
|||
300 | str = '?' + $!.failed[1,$!.failed.length] |
|
|||
301 | retry |
|
|||
302 | rescue |
|
|||
303 | txtar += $!.success |
|
|||
304 | end |
|
|||
305 | str = txtar |
|
|||
306 | end |
|
|||
307 | str |
|
|||
308 | end |
|
268 | end | |
309 | end |
|
269 | end |
@@ -34,5 +34,49 module Redmine | |||||
34 | end |
|
34 | end | |
35 | str |
|
35 | str | |
36 | end |
|
36 | end | |
|
37 | ||||
|
38 | def self.to_utf8(str, encoding) | |||
|
39 | return str if str.nil? | |||
|
40 | str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding) | |||
|
41 | if str.empty? | |||
|
42 | str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) | |||
|
43 | return str | |||
|
44 | end | |||
|
45 | enc = encoding.blank? ? "UTF-8" : encoding | |||
|
46 | if str.respond_to?(:force_encoding) | |||
|
47 | if enc.upcase != "UTF-8" | |||
|
48 | str.force_encoding(enc) | |||
|
49 | str = str.encode("UTF-8", :invalid => :replace, | |||
|
50 | :undef => :replace, :replace => '?') | |||
|
51 | else | |||
|
52 | str.force_encoding("UTF-8") | |||
|
53 | if ! str.valid_encoding? | |||
|
54 | str = str.encode("US-ASCII", :invalid => :replace, | |||
|
55 | :undef => :replace, :replace => '?').encode("UTF-8") | |||
|
56 | end | |||
|
57 | end | |||
|
58 | elsif RUBY_PLATFORM == 'java' | |||
|
59 | begin | |||
|
60 | ic = Iconv.new('UTF-8', enc) | |||
|
61 | str = ic.iconv(str) | |||
|
62 | rescue | |||
|
63 | str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |||
|
64 | end | |||
|
65 | else | |||
|
66 | ic = Iconv.new('UTF-8', enc) | |||
|
67 | txtar = "" | |||
|
68 | begin | |||
|
69 | txtar += ic.iconv(str) | |||
|
70 | rescue Iconv::IllegalSequence | |||
|
71 | txtar += $!.success | |||
|
72 | str = '?' + $!.failed[1,$!.failed.length] | |||
|
73 | retry | |||
|
74 | rescue | |||
|
75 | txtar += $!.success | |||
|
76 | end | |||
|
77 | str = txtar | |||
|
78 | end | |||
|
79 | str | |||
|
80 | end | |||
37 | end |
|
81 | end | |
38 | end |
|
82 | end |
General Comments 0
You need to be logged in to leave comments.
Login now