Skip to content

Commit

Permalink
Add basic implementation for Caiyun Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 10, 2023
1 parent 8a1d6d6 commit f91eb84
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
27 changes: 27 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
03F14A3B2956016B00CB7379 /* EZVolcanoTranslate.m in Sources */ = {isa = PBXBuildFile; fileRef = 03F14A3A2956016B00CB7379 /* EZVolcanoTranslate.m */; };
03F25CB329327BC200E66A12 /* EZShortcut.m in Sources */ = {isa = PBXBuildFile; fileRef = 03F25CB229327BC200E66A12 /* EZShortcut.m */; };
03F639952AA6CFBB009B9914 /* EZBingConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 03F639942AA6CFBB009B9914 /* EZBingConfig.m */; };
2721E4D02AFE920700A059AC /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 2721E4CF2AFE920700A059AC /* Alamofire */; };
2746AEC12AF95138005FE0A1 /* CaiyunService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2746AEC02AF95138005FE0A1 /* CaiyunService.swift */; };
27B7919E2AEC36A1006E07C6 /* Easydict.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919C2AEC36A1006E07C6 /* Easydict.xcconfig */; };
27B7919F2AEC36A1006E07C6 /* Easydict-debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919D2AEC36A1006E07C6 /* Easydict-debug.xcconfig */; };
Expand Down Expand Up @@ -673,6 +674,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
2721E4D02AFE920700A059AC /* Alamofire in Frameworks */,
B87AC7E36367075BA5D13234 /* Pods_Easydict.framework in Frameworks */,
03B63ABF2A86967800E155ED /* CoreServices.framework in Frameworks */,
);
Expand Down Expand Up @@ -1950,6 +1952,9 @@
dependencies = (
);
name = Easydict;
packageProductDependencies = (
2721E4CF2AFE920700A059AC /* Alamofire */,
);
productName = Bob;
productReference = C99EEB182385796700FEE666 /* Easydict-Debug.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -1988,6 +1993,9 @@
"zh-Hans",
);
mainGroup = C99EEB0F2385796700FEE666;
packageReferences = (
2721E4CE2AFE920700A059AC /* XCRemoteSwiftPackageReference "Alamofire" */,
);
productRefGroup = C99EEB192385796700FEE666 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -2710,6 +2718,25 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
2721E4CE2AFE920700A059AC /* XCRemoteSwiftPackageReference "Alamofire" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/Alamofire/Alamofire.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.8.1;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
2721E4CF2AFE920700A059AC /* Alamofire */ = {
isa = XCSwiftPackageProductDependency;
package = 2721E4CE2AFE920700A059AC /* XCRemoteSwiftPackageReference "Alamofire" */;
productName = Alamofire;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = C99EEB102385796700FEE666 /* Project object */;
}
70 changes: 60 additions & 10 deletions Easydict/Feature/Service/Caiyun/CaiyunService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,87 @@
// Copyright © 2023 izual. All rights reserved.
//

import Alamofire
import Foundation

@objc(EZCaiyunService)
public final class CaiyunService: QueryService {

public override func serviceType() -> ServiceType {
override public func serviceType() -> ServiceType {
.caiyun
}

public override func link() -> String? {
override public func link() -> String? {
"https://fanyi.caiyunapp.com"
}

public override func name() -> String {
override public func name() -> String {
NSLocalizedString("caiyun_translate", comment: "The name of Caiyun Translate")
}

public override func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> {
override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> {
// TODO: Replace MMOrderedDictionary in the API
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>()
orderedDict.setObject(Language.auto.rawValue as NSString, forKey: "auto" as NSString)
orderedDict.setObject(Language.simplifiedChinese.rawValue as NSString, forKey: "zh" as NSString)
orderedDict.setObject(Language.english.rawValue as NSString, forKey: "en" as NSString)
orderedDict.setObject(Language.japanese.rawValue as NSString, forKey: "ja" as NSString)
orderedDict.setObject("auto" as NSString, forKey: Language.auto.rawValue as NSString)
orderedDict.setObject("zh" as NSString, forKey: Language.simplifiedChinese.rawValue as NSString)
orderedDict.setObject("en" as NSString, forKey: Language.english.rawValue as NSString)
orderedDict.setObject("ja" as NSString, forKey: Language.japanese.rawValue as NSString)
return orderedDict
}

public override func ocr(_ queryModel: EZQueryModel) async throws -> EZOCRResult {
override public func ocr(_: EZQueryModel) async throws -> EZOCRResult {
NSLog("Caiyun Translate does not support OCR")
throw QueryServiceError.notSupported
}

private var apiEndPoint = "https://api.interpreter.caiyunai.com/v1/translator"
// Official Test Token
private var token = "3975l6lr5pcbvidl6jl2"

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 parameters: [String: Any] = [
"source": text,
"trans_type": "auto2zh",
"detect": true,
]
let headers: HTTPHeaders = [
"content-type": "application/json",
"x-authorization": "token " + token,
]

AF.request(apiEndPoint,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers)
.validate()
.responseDecodable(of: CaiyunResponse.self) { response in
switch response.result {
case let .success(value):
let result = EZQueryResult()
result.from = from
result.to = to
result.queryText = text
result.translatedResults = [value.target]
self.result = result
completion(result, nil)
case let .failure(error):
NSLog("Caiyun lookup error \(error)")
completion(nil, error)
}
}
}
}

struct CaiyunResponse: Codable {
var isdict: Int
var confidence: Double
var rc: Int
var target: String
}

enum QueryServiceError: Error {
Expand Down

0 comments on commit f91eb84

Please sign in to comment.