Google Translate API (paid) vs Google Translate API (free)?
Of course it is free and you can implement it (I included an example below). However, DO NOT USE IT because after a while Google can detect suspicious traffic –it happened to me, sadly- so you will receive an error message. I'm not sure how long can you use it until they can detect your activity so my advice for you is to test extensively your app with the "free service" and if you see some troubles perhaps you should buy the service or look for another API. https://api.mymemory.translated.net is a free but limited alternative.
In iOS Swift 4 you can implement the free service by using the following function:
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()
}
(Perhaps this is not the best implementation, but it works).