// Add cell to the view.
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(FUISimplePropertyFormCell.self, forCellReuseIdentifier: FUISimplePropertyFormCell.reuseIdentifier)
tableView.register(FUIListPickerFormCell.self, forCellReuseIdentifier: FUIListPickerFormCell.reuseIdentifier)
tableView.register(FUISimplePropertyFormCell.self, forCellReuseIdentifier: FUISimplePropertyFormCell.reuseIdentifier)
tableView.register(FUIButtonFormCell.self, forCellReuseIdentifier: FUIButtonFormCell.reuseIdentifier)
}
//Provide Cell Details and values
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
switch row{
case 0:
// source text
let cell = tableView.dequeueReusableCell(withIdentifier:FUISimplePropertyFormCell.reuseIdentifier, for: indexPath) as! FUISimplePropertyFormCell
cell.keyName = "Input Text "
cell.value = self.translationModel.inputData
cell.onChangeHandler = {
self.str = $0
}
return cell
case 1:
// target language
let cell = tableView.dequeueReusableCell(withIdentifier:FUIListPickerFormCell.reuseIdentifier, for: indexPath) as! FUIListPickerFormCell
cell.keyName = "Target Language"
cell.valueOptions = TranslationModel.langList
cell.allowsMultipleSelection = false
cell.allowsEmptySelection = false
cell.isEditable = true
if(cell.value.count == 0){
cell.value.append(self.translationModel.targetLang)
}
else{
cell.value[0] = self.translationModel.targetLang
}
cell.onChangeHandler = {
self.translationModel.targetLang = $0[0]
}
return cell
case 2:
// Translated Text
let cell = tableView.dequeueReusableCell(withIdentifier:FUISimplePropertyFormCell.reuseIdentifier, for: indexPath) as! FUISimplePropertyFormCell
cell.keyName = "Translation"
cell.value = self.translationModel.outputData
cell.onChangeHandler = {
self.str = $0
}
return cell
case 3:
// Button
let cell = tableView.dequeueReusableCell(withIdentifier:FUIButtonFormCell.reuseIdentifier, for: indexPath) as! FUIButtonFormCell
cell.button.setTitle("Translate", for: [])
cell.button.tintColor = UIColor.black
//Button Actions
cell.button.addTarget(self, action: #selector(TableViewController.getTranslation(_:)), for: .touchUpInside)
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier:FUISimplePropertyFormCell.reuseIdentifier, for: indexPath) as! FUISimplePropertyFormCell
return cell
}
}
// Action for getting translation
@objc func getTranslation(_ sender:UIButton!){
var index = IndexPath(row: 0, section: 0)
var cell = self.tableView.cellForRow(at: index)
// Read current input
self.translationModel.inputData = cell?.value(forKey: "value") as! String
//adding request headers
//API Key for API Sandbox
let headers = [
"Content-Type": "application/json",
"Accept": "application/json;charset=UTF-8",
"APIKey": "a4hcjXcHKxMeCZg0Af9y7GA5YUbqv6os"]
let parameters = [
"sourceLanguage": "en",
"targetLanguages": [TranslationModel.langList[self.translationModel.targetLang]],
"units": [["value": self.translationModel.inputData]]]
as [String : Any]
var postData: NSData? = nil
do {
postData = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions(rawValue:0)) as NSData
}
catch {
print(error)
}
//API endpoint for API sandbox
let request = NSMutableURLRequest(url: NSURL(string: "https://sandbox.api.sap.com/ml/translation/translate")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
//setting request method
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData! as Data
let urlSession = SAPURLSession()
//sending request
let dataTask = urlSession.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) -> Void in
// Response Handling in Challenges part
})
dataTask.resume()
}
do {
guard let responseObject = try JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers)as? NSDictionary else {
print("error trying to convert data to JSON")
return
}
let root = responseObject["units"] as? NSMutableArray
let contentDictionary : NSDictionary = root![0] as! NSDictionary
let translationArray = contentDictionary["translations"] as? NSMutableArray
let decoded : NSDictionary = translationArray![0] as! NSDictionary
self.translationModel.outputData = decoded["value"] as! String
DispatchQueue.main.async {
self.tableView.reloadData()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |