12345678910111213141516171819202122232425262728293031323334353637383940 |
- module Encode
- def self.conv (charset, string)
-
- return string if !string
-
- if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
- charset = 'ISO-8859-15'
- end
-
- if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
- begin
-
- utf8 = string.force_encoding('UTF-8')
- return utf8 if utf8.valid_encoding?
-
- string.encode!( 'UTF-8', 'Windows-1252' )
- rescue EncodingError => e
- Rails.logger.error "Bad encoding: #{string.inspect}"
- string = string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' )
- end
- return string
- end
-
- begin
- string.encode!( 'UTF-8', charset )
- rescue => e
- Rails.logger.error 'ERROR: ' + e.inspect
- string
- end
-
- end
- end
|