Module: Ggtranslate::Translator

Included in:
Ggtranslate
Defined in:
lib/ggtranslate/translator.rb

Instance Method Summary collapse

Instance Method Details

#build_headersObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ggtranslate/translator.rb', line 26

def build_headers
  {

    accept: "*/*",
    "accept-language": "en-US,en;q=0.9,vi;q=0.8",
    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"macOS"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "none",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",
  }
end

#translate(text_query, source_language = "auto", to_language = "en") ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ggtranslate/translator.rb', line 8

def translate(text_query, source_language = "auto", to_language = "en")
  endpoint = "https://clients5.google.com/translate_a/t?"
  # to translate multiple sentences
  text_query = text_query.join("----------") if text_query.is_a? Array

  query_params = {
    client: "dict-chrome-ex", # this is a must
    sl: source_language,
    tl: to_language,
    q: text_query,
  }

  uri = Addressable::URI.new
  uri.query_values = query_params
  response = RestClient.get(endpoint + uri.query, headers = build_headers)
  ResultObject.new(response)
end