Skip to content

Commit

Permalink
Add caiyun service transtype support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 12, 2023
1 parent a90da71 commit 077bd1c
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions Easydict/Feature/Service/Caiyun/CaiyunService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 077bd1c

Please sign in to comment.