Google-cloud-ruby: google-cloud-translation translated text is escaped

Created on 5 Feb 2020  ยท  4Comments  ยท  Source: googleapis/google-cloud-ruby

Environment details

  • OS: Mac
  • Ruby version: 2.5.3
  • Gem name and version: google-cloud-translation 1.3.0

Steps to reproduce

[3] pry(main)> ::Google::Cloud::Translate.new.translate('ใŠ่…นใ™ใ„ใŸ', to: 'en')
=> #<Google::Cloud::Translate::Translation:0x00007f82b4b987a8 @detected=true, @from="ja", @model=nil, @origin="ใŠ่…นใ™ใ„ใŸ", @text="I&#39;m hungry", @to="en">

the @text should be "I'm hungry" like below

Screen Shot 2020-02-05 at 22 39 37

I did some debugging, looks like Faraday is returning the escaped string? ๐Ÿค”

From: google-cloud-translate-1.3.0/lib/google/cloud/translate/service.rb @ line 55 Google::Cloud::Translate::Service#translate:

    47: def translate text, to: nil, from: nil, format: nil, model: nil,
    48:               cid: nil
    49:   body = {
    50:     q: Array(text), target: to, source: from, format: format,
    51:     model: model, cid: cid
    52:   }.delete_if { |_k, v| v.nil? }.to_json
    53:
    54:   require 'pry'; binding.pry
 => 55:   post "/language/translate/v2", body
    56: end

[1] pry(#<Google::Cloud::Translate::Service>)> body
=> "{\"q\":[\"ใŠ่…นใ™ใ„ใŸ\"],\"target\":\"en\"}"
[2] pry(#<Google::Cloud::Translate::Service>)> post '/language/translate/v2', body
=> {"translations"=>[{"translatedText"=>"I&#39;m hungry", "detectedSourceLanguage"=>"ja"}]}

Sorry if this is already fixed in 2.X :( I'll try to upgrade the gem to 2.X if so.

translation question

Most helpful comment

@take Thank you, I'll look into this as soon as I can. If you find a solution before I do, please feel free to submit a pull request.

All 4 comments

@take Thank you for reporting this. Can you try google-cloud-translate 2.1.1? If for some reason you can't, let me know and I'll try to check soon if it's fixed in the latest version.

@quartzmo

looks like the problem does exist in 2.1.1 as well :(

2] pry(main)> ::Google::Cloud::Translate.new(version: :v2).translate('ใŠ่…นใ™ใ„ใŸ', to: 'en')
=> #<Google::Cloud::Translate::V2::Translation:0x00007f85d54ffd50 @detected=true, @from="ja", @model=nil, @origin="ใŠ่…นใ™ใ„ใŸ", @text="I&#39;m hungry", @to="en">

@take Thank you, I'll look into this as soon as I can. If you find a solution before I do, please feel free to submit a pull request.

I have also encountered this issue. While looking for a solution I have found this (for api version 3) -

@param mime_type [::String]
#     Optional. The format of the source text, for example, "text/html",
#      "text/plain". If left blank, the MIME type defaults to "text/html".

I have added a param mime_type: 'text/plain' to translate_text method and it works fine now.

Was this page helpful?
0 / 5 - 0 ratings