0

I need to generate a file in rails with encoding iso-8859-15. To make this, I am using the following code:

File.open( "#{ Rails.root }/tmp/#{ id }", 'w:iso-8859-15:iso-8859-15' ) { |f| f << Model.info_in_iso-8859-15 }
respond_to do |format|
  format.text { send_file( route, filename: 'info', type: 'text/plain; charset=iso-8859-15; header=present' ) }
end

The problem is that when I see the encoding of the file, it is using iso-8859-1, so I have problems with some symbols like € and so on. I have been surfing on Intenet for hours but I can not find anything for ruby.

Thanks in advance.

EDIT:

This is the method that I use to convert to iso-8859-15:

def normalize_to_iso8859( string )
  if string and string.class == String
    string.gsub(/[\u201c\u201d]/, '"').gsub(/[\u2013]/, '-').encode( "ISO8859-15" )
  else
    string
  end
end
  • What do yu mean cannot save? Where is the result? – Малъ Скрылевъ Apr 11 '14 at 08:03
  • `Model.info_in_iso-8859-15` is obviously not a real method name. Presumably you are attempting to convert a string within that method in the real code. How you are doing that is relevant to your question (it may also not be necessary BTW, but we can get to that later) - could you show your line that does the conversion? – Neil Slater Apr 11 '14 at 08:05
  • I can save the file but when I use the command "file -i", I see that the charset is iso-8859-1. I need iso-8859-15 because it has € symbol. The above code appears in a controller and I have added the code that I use to convert to iso-8859-15. – jesusgonzalezrivera Apr 11 '14 at 08:06
  • Your conversion routine works OK for me, and handles `s = '€'; s_converted = normalize_to_iso8859( s )` as I'd expect. I would not trust output of `file -i` here, it has to guess likely encoding, there is no unambiguous flag in a text file. What you should do is inspect the text file and view the hex byte of `€` which should be `A4` if your file has saved correctly. – Neil Slater Apr 11 '14 at 08:22
  • could you show `hexdump -C file` on the file? on on part containing $ – Малъ Скрылевъ Apr 11 '14 at 08:34
  • 1
    After a few attemps, I have been able to check that the file has the iso-8859-15 charset and "file -i" command is not giving the correct charset. Thanks all for your help. – jesusgonzalezrivera Apr 11 '14 at 09:29

0 Answers0