How to use google translator from Julia level?

How to use google translator from Julia level? How to impersonate a browser?

to transalte dog for german

I am expecting to get Hund :slight_smile:
Paul

Based on this article:

using HTTP
using JSON

function gtranslate(text, targetlang, sourcelang = "auto")
    url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" * sourcelang * "&tl=" * targetlang * "&dt=t&q=" * HTTP.escapeuri(text)
    JSON.parse(String(HTTP.request("GET", url).body))[1][1][1]
end
1 Like

Thanks, is percect for short text.
I made for long text but somethin is wrong :slight_smile:

julia> x
"The Coronavirus is likely to be in Britain already, health chiefs warned this morning. All Britons brought back
 from the Hubei province are likely to be tested for the virus as a precaution after the Foreign Office promised
 to give Britons stranded there help to return. The government said their..."

julia> function gtranslate(text, targetlang, sourcelang = "auto")
           url = "https://translate.google.pl/?hl="*targetlang*"#view=home&op=translate&sl=en&tl=de&text="* text

               JSON.parse(String(HTTP.request("GET", url).body))[1][1][1]
               end
gtranslate (generic function with 2 methods)

julia> gtranslate(x,"pl")
ERROR: HTTP.ExceptionRequest.StatusError(400, "GET", "/?hl=pl#view=home&op=translate&sl=en&tl=de&text=The Corona
virus is likely to be in Britain already, health chiefs warned this morning. All Britons brought back from the H
ubei province are likely to be tested for the virus as a precaution after the Foreign Office promised to give Br
itons stranded there help to return. The government said their...", HTTP.Messages.Response:
"""
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1555
Date: Mon, 27 Jan 2020 12:31:12 GMT

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}
body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.
com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{col
or:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:
0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo
_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:
url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz
-border-image:url(//ww
?
1555-byte body
""")
Stacktrace:
 [1] #request#1(::Base.Iterators.Pairs{Symbol,Nothing,Tuple{Symbol},NamedTuple{(:iofunction,),Tuple{Nothing}}},
::Function, ::Type{ExceptionLayer{ConnectionPoolLayer{StreamLayer{Union{}}}}}, ::HTTP.URIs.URI, ::Vararg{Any,N}
where N) at C:\Users\PC\.julia\packages\HTTP\lZVI1\src\ExceptionRequest.jl:22
 [2] (::getfield(HTTP, Symbol("#kw##request")))(::NamedTuple{(:iofunction,),Tuple{Nothing}}, ::typeof(HTTP.reque
st), ::Type{ExceptionLayer{ConnectionPoolLayer{StreamLayer{Union{}}}}}, ::HTTP.URIs.URI, ::HTTP.Messages.Request
, ::Array{UInt8,1}) at .\none:0
 [3] (::getfield(Base, Symbol("###44#45#46")){ExponentialBackOff,getfield(HTTP.RetryRequest, Symbol("##2#3")){Bo
ol,HTTP.Messages.Request},typeof(HTTP.request)})(::Base.Iterators.Pairs{Symbol,Nothing,Tuple{Symbol},NamedTuple{
(:iofunction,),Tuple{Nothing}}}, ::Function, ::Type, ::Vararg{Any,N} where N) at .\error.jl:203
 [4] #request#1(::Int64, ::Bool, ::Base.Iterators.Pairs{Symbol,Nothing,Tuple{Symbol},NamedTuple{(:iofunction,),T
uple{Nothing}}}, ::Function, ::Type{RetryLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer{Union{}}}}}}, ::HT
TP.URIs.URI, ::HTTP.Messages.Request, ::Array{UInt8,1}) at .\none:0
 [5] #request at .\none:0 [inlined]
 [6] #request#1(::VersionNumber, ::String, ::Nothing, ::Nothing, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},
NamedTuple{(),Tuple{}}}, ::Function, ::Type{MessageLayer{RetryLayer{ExceptionLayer{ConnectionPoolLayer{StreamLay
er{Union{}}}}}}}, ::String, ::HTTP.URIs.URI, ::Array{Pair{SubString{String},SubString{String}},1}, ::Array{UInt8
,1}) at C:\Users\PC\.julia\packages\HTTP\lZVI1\src\MessageRequest.jl:51
 [7] request at C:\Users\PC\.julia\packages\HTTP\lZVI1\src\MessageRequest.jl:28 [inlined]
 [8] #request#1(::Int64, ::Bool, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Funct
ion, ::Type{RedirectLayer{MessageLayer{RetryLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer{Union{}}}}}}}},
 ::String, ::HTTP.URIs.URI, ::Array{Pair{SubString{String},SubString{String}},1}, ::Array{UInt8,1}) at C:\Users\
PC\.julia\packages\HTTP\lZVI1\src\RedirectRequest.jl:24
 [9] request(::Type{RedirectLayer{MessageLayer{RetryLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer{Union{}
}}}}}}}, ::String, ::HTTP.URIs.URI, ::Array{Pair{SubString{String},SubString{String}},1}, ::Array{UInt8,1}) at C
:\Users\PC\.julia\packages\HTTP\lZVI1\src\RedirectRequest.jl:21
 [10] #request#4(::Array{Pair{SubString{String},SubString{String}},1}, ::Array{UInt8,1}, ::Nothing, ::Base.Itera
tors.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::String, ::String, ::Array{Pair{SubStri
ng{String},SubString{String}},1}, ::Array{UInt8,1}) at C:\Users\PC\.julia\packages\HTTP\lZVI1\src\HTTP.jl:308
 [11] request at C:\Users\PC\.julia\packages\HTTP\lZVI1\src\HTTP.jl:308 [inlined] (repeats 2 times)
 [12] gtranslate(::String, ::String, ::String) at .\REPL[42]:3
 [13] gtranslate(::String, ::String) at .\REPL[42]:2
 [14] top-level scope at none:0

You left out HTTP.escapeuri(...). That said, it doesn’t seem to work for longer texts, as the returned string is split up. You can just join them, though:

function gtranslate(text, targetlang, sourcelang = "auto")
    url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" * sourcelang * "&tl=" * targetlang * "&dt=t&q=" * HTTP.escapeuri(text)
    result = JSON.parse(String(HTTP.request("GET", url).body))
    join([s[1] for s in result[1]], "")
end
3 Likes