encode.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. module Encode
  2. def self.conv (charset, string)
  3. # return if string is false
  4. return string if !string
  5. # if no charset is given, use LATIN1 as default
  6. if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
  7. charset = 'ISO-8859-15'
  8. end
  9. # validate already existing utf8 strings
  10. if charset.casecmp('utf8').zero? || charset.casecmp('utf-8').zero?
  11. begin
  12. # return if encoding is valid
  13. utf8 = string.dup.force_encoding('UTF-8')
  14. return utf8 if utf8.valid_encoding?
  15. # try to encode from Windows-1252 to utf8
  16. string.encode!('UTF-8', 'Windows-1252')
  17. rescue EncodingError => e
  18. Rails.logger.error "Bad encoding: #{string.inspect}"
  19. string = string.encode!('UTF-8', invalid: :replace, undef: :replace, replace: '?')
  20. end
  21. return string
  22. end
  23. # convert string
  24. begin
  25. string.encode!('UTF-8', charset)
  26. rescue => e
  27. Rails.logger.error 'ERROR: ' + e.inspect
  28. string
  29. end
  30. #Iconv.conv( 'UTF8', charset, string)
  31. end
  32. end