Skip to content

Commit

Permalink
perf: add missing traditional Chinese target language
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 3, 2023
1 parent b117339 commit 6ab6c89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
4 changes: 0 additions & 4 deletions Easydict/Feature/Service/Tencent/TencentService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public final class TencentService: QueryService {
throw QueryServiceError.notSupported
}

override public func autoConvertTraditionalChinese() -> Bool {
return true
}

// MARK: API Request
private static let defaultSecretId = ""
private static let defaultSecretKey = ""
Expand Down
36 changes: 26 additions & 10 deletions Easydict/Feature/Service/Tencent/TencentTranslateType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct TencentTranslateType: Equatable {

static let unsupported = TencentTranslateType(sourceLanguage: "unsupported", targetLanguage: "unsupported")

// Docs: https://cloud.tencent.com/document/api/551/15619
// This docs missed traditionalChinese as target language if target languages contains simplifiedChinese. https://cloud.tencent.com/document/api/551/15619
static let supportedTypes: [Language: [Language]] = [
.simplifiedChinese: [.english, .japanese, .korean, .french, .spanish, .italian, .german, .turkish, .russian, .portuguese, .vietnamese, .indonesian, .thai, .malay],
.traditionalChinese: [.english, .japanese, .korean, .french, .spanish, .italian, .german, .turkish, .russian, .portuguese, .vietnamese, .indonesian, .thai, .malay],
Expand Down Expand Up @@ -60,16 +60,32 @@ struct TencentTranslateType: Equatable {
]

static func transType(from: Language, to: Language) -> TencentTranslateType {
if (supportedTypes[from] != nil && to == .traditionalChinese) ||
from == to ||
(supportedTypes[from]?.contains(to) == true) {
guard let from = supportLanguagesDictionary[from],
let to = supportLanguagesDictionary[to] else {
return .unsupported
}
return TencentTranslateType(sourceLanguage: from, targetLanguage: to)
} else {
// !!!: Tencent translate support traditionalChinese as target language if target languages contain simplifiedChinese.
guard let targetLanguages = supportedTypes[from],
(targetLanguages.containsChinese() || targetLanguages.contains(to) || from == to || from.isKindOfChinese()) else {
return .unsupported
}

guard let fromLanguage = supportLanguagesDictionary[from],
let toLanguage = supportLanguagesDictionary[to] else {
return .unsupported
}

return TencentTranslateType(sourceLanguage: fromLanguage, targetLanguage: toLanguage)
}
}


extension Array where Element == Language {
// Contains Chinese language
func containsChinese() -> Bool {
contains { $0.isKindOfChinese() }
}
}

extension Language {
// Is kind of Chinese language
func isKindOfChinese() -> Bool {
self == .simplifiedChinese || self == .traditionalChinese
}
}

0 comments on commit 6ab6c89

Please sign in to comment.