public async Task <string> TranslateAsync(string[] files, string contractId, FileTranslationOption option) { string url = baseUrl + "/file-translate"; Dictionary <string, object> headers = HttpUtils.BuildHeaders( new RozettaApiUser { AccessKey = option.AccessKey, SecretKey = option.SecretKey }, url); Dictionary <string, string> body = new Dictionary <string, string> { { "fieldId", option.FieldId.ToString() }, { "targetLangs", JsonConvert.SerializeObject(option.Langs) } }; if (!string.IsNullOrEmpty(contractId)) { body.Add("contractId", contractId); } Dictionary <string, string> fileDict = new Dictionary <string, string>(); foreach (string file in files) { fileDict.Add("files", file); } var content = await HttpUtils.PostFileAsync(url, headers, body, fileDict); var byteArray = await content.ReadAsByteArrayAsync(); var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); var definition = new { status = "", data = new { translateId = "" } }; var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition); if (serverResp.status != ResponseStatus.Success) { throw new Exception("upload file failed"); } return(serverResp.data.translateId); }
public async Task <bool> RozettaApiFlowAsync(RozettaApiUser rozettaApiUser, string[] files, string[] langs, int fieldId) { this.rozettaApiUser = rozettaApiUser; long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); FileTranslationOption option = new FileTranslationOption { Langs = langs, AccessKey = rozettaApiUser.AccessKey, SecretKey = rozettaApiUser.SecretKey, Nonce = timeStamp.ToString(), FieldId = fieldId }; translateId = await client.TranslateAsync(files, rozettaApiUser.ContractId, option); if (aTimer != null) { StopTimer(aTimer); } aTimer = new System.Timers.Timer(); aTimer.Interval = Interval; aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true; while (!done) { Thread.Sleep(1000); } return(true); }