diff --git a/Easydict/Feature/Service/Caiyun/CaiyunService.swift b/Easydict/Feature/Service/Caiyun/CaiyunService.swift index 64226c4d8..bc2af3088 100644 --- a/Easydict/Feature/Service/Caiyun/CaiyunService.swift +++ b/Easydict/Feature/Service/Caiyun/CaiyunService.swift @@ -39,18 +39,25 @@ public final class CaiyunService: QueryService { } private var apiEndPoint = "https://api.interpreter.caiyunai.com/v1/translator" - // Official Test Token - private var token = "3975l6lr5pcbvidl6jl2" + + /// Official Test Token for Caiyun + private var token = "3975l6lr5" + "pcbvidl6jl2" override public func translate(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult?, Error?) -> Void) { if prehandleQueryTextLanguage(text, autoConvertChineseText: false, from: from, to: to, completion: completion) { return } - // TODO: + let transType = transType(from: from, to: to) + guard transType != .unsupported else { + result.errorType = .unsupportedLanguage + result.errorMessage = "不支持的翻译类型" + completion(result, nil) + return + } let parameters: [String: Any] = [ "source": text, - "trans_type": "auto2zh", + "trans_type": transType.rawValue, "detect": true, ] let headers: HTTPHeaders = [ @@ -80,6 +87,56 @@ public final class CaiyunService: QueryService { } } } + enum TranslateType: String { + case en2zh + case ja2zh + case zh2en + case zh2ja + case auto2zh + case auto2en + case auto2ja + case unsupported + } + + private func transType(from: Language, to: Language) -> TranslateType { + if from.isChinese { + switch to { + case .english: return .zh2en + case .japanese: return .zh2ja + default: return .unsupported + } + } else if from == .english { + if to.isChinese { + return .en2zh + } else { + return .unsupported + } + } else if from == .japanese { + if to.isChinese { + return .ja2zh + } else { + return .unsupported + } + } else if from == .auto { + if to.isChinese { + return .auto2zh + } else if to == .english { + return .auto2en + } else if to == .japanese { + return .auto2ja + } else { + return .unsupported + } + } else { + return .unsupported + } + } +} + +extension Language { + var isChinese: Bool { + [Language.classicalChinese, .simplifiedChinese, .traditionalChinese].contains(self) + } } struct CaiyunResponse: Codable {