views:

31

answers:

2

Hi,

In my rails app I work a lot with cyrillic characters. Thats no problem, I store them in the db, I can display it in html.

But I have a problem exporting them in a plain txt file. A string like "элиас" gets "—ç–ª–∏–∞—Å" if I let rails put in in a txt file and download it. Whats wrong here? What has to be done?

Regards,

Elias

A: 

Obviously, there's a problem with your encoding. Make sure you text is in Unicode before writing it to the text file. You may use something like this:

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
your_unicode_text = ic.iconv(your_text + ' ')[0..-2]

Also, double check that your database encoding is UTF-8. Cyrillic characters can display fine in DB and in html with non-unicode encoding, e.g. KOI8-RU, but you're guaranteed to have problems with them elsewhere.

buru
A: 

Doesn't seem to work.

Before I had

headers['Content-Type'] = "text/plain"
headers['Content-Disposition'] = 'attachment; filename="export.txt"'
headers['Cache-Control'] = ''

render :text => txt

now I have

require "iconv"

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
txt_unicode = ic.iconv(txt + ' ')[0..-2]

headers['Content-Type'] = "text/plain"
headers['Content-Disposition'] = 'attachment; filename="export.txt"'
headers['Cache-Control'] = ''

render :text => txt_unicode
Elias