3

I have a gem in ruby that does translations using google API and I am "translating" it to Elixir.

For example, I get from API some like this: api-data

And in Ruby today I do this:

  encoded = rawdata.force_encoding("UTF-8")

I would like to know if there is a way to "force_encode" (like Ruby does) but with Elixir?

UPDATE SOLUTION

I reached a solution based in your answers guys thanks a lot!

As Elixir handle it as binaries then that is the trick: I get the response body: body |> IO.iodata_to_binary ...

defmodule Request do
  alias Extract
  use HTTPotion.Base

  def process_url(url) do
    "https://translate.google.com/translate_a/" <> url
  end

  def process_response_body(body) do
    body |> IO.iodata_to_binary |> Extract.extract
  end
end

Here is entire code

4
  • 1
    Elixir strings (which are more properly called binaries) are UTF-8 by default. Or am I misunderstanding your question? Commented Oct 4, 2015 at 0:06
  • You are right. I saw that in documentation. The problem is I receive a file in this URL (that I've posted) and this file has some strange encoding so when I parse it for a text I get things like "Olá". I've solved whit ruby by force_encoding. Any thought? Commented Oct 4, 2015 at 0:58
  • 1
    Hmm. I don't know where you're getting that file from but according to UChardet (on my Linux box) the encoding is TIS-620. en.wikipedia.org/wiki/Thai_Industrial_Standard_620-2533 I'd look for some sort of utility to convert that to UTF-8. Commented Oct 4, 2015 at 1:15
  • Thanks @OnorioCatenacci I found a solution! Commented Oct 4, 2015 at 15:46

1 Answer 1

4

You use force encoding in Ruby when the data is tagged as binary but really is UTF-8. In Elixir, they are both at the same time, because all strings are binaries, we don't tag them in anyway. In other words, you shouldn't need to force encoding.

However, if the data is not in UTF-8, then you need to find a way to convert it to UTF-8 in the first place.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @JoséValim. I found a solution based in your answer! I updated the post with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.