Google Translation constructURL and fetch with JS

Hi,
ConstructUrl and fetch later with Js is not clear to me. Can someone help us in this way using translate.googleapis

https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=Hello

I see:

Later
pass the ConstructUrl to Js is not working

JAVA column
func translate(str: String, lang1: String, lang2: String) {

let escapedStr = str.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
let lastPart = lang1 + "&tl=" + lang2 + "&dt=t&dt=t&q=" + escapedStr!
let urlStr: String = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + lastPart
let url = URL(string: urlStr)

let task = URLSession.shared.downloadTask(with: url!) { localURL, urlResponse, error in
    if let localURL = localURL {
        if let string = try? String(contentsOf: localURL) {
            let index = string.firstIndex(of: "\"")
            let index2 = string.index(after: index!)
            let subst = string.substring(from: index2)
            let indexf = subst.firstIndex(of: "\"")
            let result = subst.substring(to: indexf!)
            DispatchQueue.main.async {
                if flag1country != flag2country {
                    self.texto.text = result
                }
            }
        }
    }
}
task.resume()

}

Here’s how you should set it up.

1 Like

Thanks very much I have my results, and how to copy the result in another column as value (to fix the value forever) to prevent more usage of this api call?

thanks for your help