-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic support for CaiyunService Add basic implementation for Caiyun Service Fix CR suggestion Add caiyun service transtype support perf: split caiyun test token to more parts perf: improve caiyun supported langauges dict Update Easydict/Feature/Service/Caiyun/CaiyunService.swift Co-authored-by: Kyle <[email protected]> * Add basic support for CaiyunService Add basic implementation for Caiyun Service Fix CR suggestion Add caiyun service transtype support perf: split caiyun test token to more parts perf: improve caiyun supported langauges dict Update Easydict/Feature/Service/Caiyun/CaiyunService.swift Co-authored-by: Kyle <[email protected]> * feat: initiate Tencent Translate support * feat: continue building Tencent Translate * fix: error when detecting unsupported languages in transType * style: code formatting * feat: proceed to Authentication header * fix: re-add TencentAPIKek * Fix header issue * fix: remove duplicated EZTencentAPIKey * perf: optimize logic in TencentTranslateType.swift * perf: optimize and localize error handling in unsupported languages * perf(UI): add English localization for Tencent Translate * perf: optimize MMOrderedDictionary logic * perf(UI): revert TMT icon * perf: initialize support for secrets customization * perf: implement demo for authentication * fix: proper json decoding * feat: implement secrets customization * style: format code Format with SwiftLint * perf: add self support language * fix: build error * perf(UI): adopt official name of Tencent Translate Co-Authored-By: Tisfeng <[email protected]> * perf: create headerAuth func for authorization * perf(UI): delete Tencent TTS in dropdown * refactor: improve tencent sign * refractor: improve tencent sign Co-Authored-By: Tisfeng <[email protected]> * fix: tencent api endpoint * Revert "perf: add self support language" This reverts commit 0bc2ab1. * perf: update handling of language * fix: correct supportedTypes * style: format code fixed extraneous white spaces with SwiftLint * perf: prepares TencentSigning for other services * perf: enable tencent from == to * perf: add missing traditional Chinese target language * perf: improve tencent error message * perf: map error response --> Response --------- Co-authored-by: Kyle <[email protected]> Co-authored-by: tisfeng <[email protected]>
- Loading branch information
1 parent
215fb29
commit 9fc8d0b
Showing
11 changed files
with
428 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// TencentResponse.swift | ||
// Easydict | ||
// | ||
// Created by Jerry on 2023-11-25. | ||
// Copyright © 2023 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct TencentResponse: Codable { | ||
struct Response: Codable { | ||
var RequestId: String | ||
var Source: String | ||
var Target: String | ||
var TargetText: String | ||
} | ||
|
||
var Response: Response | ||
} | ||
|
||
/** | ||
{ | ||
"Response": { | ||
"Error": { | ||
"Code": "InvalidParameterValue", | ||
"Message": "不支持的语种:hi_to_zh" | ||
}, | ||
"RequestId": "eb6d17f2-6771-4653-af6f-6b2edbf07294" | ||
} | ||
} | ||
*/ | ||
struct TencentErrorResponse: Codable { | ||
struct Response: Codable { | ||
var error: Error | ||
var requestId: String | ||
|
||
// CodingKeys 枚举用于映射字段名 | ||
private enum CodingKeys: String, CodingKey { | ||
case error = "Error" // error --> Error | ||
case requestId = "RequestId" // requestId --> RequestId | ||
} | ||
} | ||
|
||
struct Error: Codable { | ||
var code: String | ||
var message: String | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case code = "Code" // code --> Code | ||
case message = "Message" // message --> Message | ||
} | ||
} | ||
|
||
var response: Response | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case response = "Response" // response --> Response | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// TencentService.swift | ||
// Easydict | ||
// | ||
// Created by Jerry on 2023-11-25. | ||
// Copyright © 2023 izual. All rights reserved. | ||
// | ||
|
||
import Alamofire | ||
import Foundation | ||
|
||
@objc(EZTencentService) | ||
public final class TencentService: QueryService { | ||
override public func serviceType() -> ServiceType { | ||
.tencent | ||
} | ||
|
||
override public func link() -> String? { | ||
"https://fanyi.qq.com" | ||
} | ||
|
||
override public func name() -> String { | ||
NSLocalizedString("tencent_translate", comment: "The name of Tencent Translate") | ||
} | ||
|
||
override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> { | ||
// TODO: Replace MMOrderedDictionary in the API | ||
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>() | ||
TencentTranslateType.supportLanguagesDictionary.forEach { key, value in | ||
orderedDict.setObject(value as NSString, forKey: key.rawValue as NSString) | ||
} | ||
return orderedDict | ||
} | ||
|
||
override public func ocr(_: EZQueryModel) async throws -> EZOCRResult { | ||
NSLog("Tencent Translate currently does not support OCR") | ||
throw QueryServiceError.notSupported | ||
} | ||
|
||
// MARK: API Request | ||
private static let defaultSecretId = "" | ||
private static let defaultSecretKey = "" | ||
|
||
|
||
// easydict://writeKeyValue?EZTencentSecretId=xxx | ||
private var secretId: String { | ||
let secretId = UserDefaults.standard.string(forKey: EZTencentSecretId) | ||
if let secretId, !secretId.isEmpty { | ||
return secretId | ||
} else { | ||
return TencentService.defaultSecretId | ||
} | ||
} | ||
|
||
// easydict://writeKeyValue?EZTencentSecretKey=xxx | ||
private var secretKey: String { | ||
let secretKey = UserDefaults.standard.string(forKey: EZTencentSecretKey) | ||
if let secretKey, !secretKey.isEmpty { | ||
return secretKey | ||
} else { | ||
return TencentService.defaultSecretKey | ||
} | ||
} | ||
|
||
public override func translate(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) { | ||
if prehandleQueryTextLanguage(text, from: from, to: to, completion: completion) { | ||
return | ||
} | ||
|
||
translateText(text, from: from, to: to, completion: completion) | ||
} | ||
|
||
func translateText(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) { | ||
let transType = TencentTranslateType.transType(from: from, to: to) | ||
guard transType != .unsupported else { | ||
result.errorType = .unsupportedLanguage | ||
let unsupportedType = NSLocalizedString("unsupported_translation_type", comment: "") | ||
result.errorMessage = "\(unsupportedType): \(from.rawValue) --> \(to.rawValue)" | ||
completion(result, nil) | ||
return | ||
} | ||
|
||
let parameters: [String: Any] = [ | ||
"SourceText": text, | ||
"Source": transType.sourceLanguage, | ||
"Target": transType.targetLanguage, | ||
"ProjectId": 0 | ||
] | ||
|
||
let endpoint = "https://tmt.tencentcloudapi.com" | ||
|
||
let service = "tmt" | ||
let action = "TextTranslate" | ||
let version = "2018-03-21" | ||
|
||
let headers = tencentSignHeader(service: service, action: action, version: version, parameters: parameters, secretId: secretId, secretKey: secretKey) | ||
|
||
let request = AF.request(endpoint, | ||
method: .post, | ||
parameters: parameters, | ||
encoding: JSONEncoding.default, | ||
headers: headers) | ||
.validate() | ||
.responseDecodable(of: TencentResponse.self) { [weak self] response in | ||
guard let self else { return } | ||
let result = self.result | ||
switch response.result { | ||
case let .success(value): | ||
result.from = from | ||
result.to = to | ||
result.queryText = text | ||
result.translatedResults = value.Response.TargetText.components(separatedBy: "\n") | ||
completion(result, nil) | ||
case let .failure(error): | ||
NSLog("Tencent lookup error \(error)") | ||
if let data = response.data { | ||
do { | ||
let errorResponse = try JSONDecoder().decode(TencentErrorResponse.self, from: data) | ||
result.errorMessage = errorResponse.response.error.message | ||
} catch { | ||
NSLog("Failed to decode error response: \(error)") | ||
} | ||
} | ||
completion(result, error) | ||
} | ||
} | ||
queryModel.setStop({ | ||
request.cancel() | ||
}, serviceType: serviceType().rawValue) | ||
} | ||
} |
Oops, something went wrong.