Jaruba
Jaruba

Reputation: 1055

JavaScript force UTF-8 encoding

I found this article: http://ecmanaut.blogspot.ro/2006/07/encoding-decoding-utf8-in-javascript.html

And many others that recommended this method. But unescape(encodeURIComponent(srt)) doesn't seem to work for me. I'm trying to convert the contents of a file that I get with XMLHttpRequest.

Before using unescape(encodeURIComponent(srt)) the unicode characters show as "?", after the conversion, all unicode characters show as "�". So I'm to assume that the conversion failed.

The file itself is encoded with ANSI.

Here's the Fiddle: http://jsfiddle.net/9g6zkmof/1/

I need this to work in pure javascript.

If you are having the same issue with subtitles from OpenSubtitles, please read this answer. Otherwise, the selected answer is the correct one.

Thanks

Upvotes: 0

Views: 3446

Answers (2)

Jaruba
Jaruba

Reputation: 1055

I sent an email to the webmaster at OpenSubtitles, he replied by directing me to one of his posts from just 4 days ago:

http://forum.opensubtitles.org/viewtopic.php?f=1&t=14992&p=30697#p30697

Now you can request any subtitle in whatever encoding you want and the server will use iconv() to encode it. This is not 100% because iconv() can also fail, but is the best case scenario.

So this link:

http://dl.opensubtitles.org/en/download/filead/src-api/vrf-bfafe1c11f/sid-85k8neb5gpmo3npqoog00t6c64/1954590765.srt

now becomes:

http://dl.opensubtitles.org/en/download/subencoding-utf8/file/1954590765

and you receive a subtitle file encoded in UTF-8.

Fiddle

Upvotes: 0

epoch
epoch

Reputation: 16595

As you said, it is not encoded in UTF-8:

var path = 'http://dl.opensubtitles.org/en/download/filead/src-api/vrf-bfafe1c11f/sid-85k8neb5gpmo3npqoog00t6c64/1954590765.srt'
var xhr = new XMLHttpRequest();
xhr.open("GET", path, false);
xhr.overrideMimeType('text/plain; charset=iso-8859-1');
xhr.send();

FIDDLE

Upvotes: 2

Related Questions