diff --git a/Easydict/Feature/Service/Ali/AliService.swift b/Easydict/Feature/Service/Ali/AliService.swift index 6cce43514..e1a77b9b7 100644 --- a/Easydict/Feature/Service/Ali/AliService.swift +++ b/Easydict/Feature/Service/Ali/AliService.swift @@ -37,6 +37,14 @@ class AliService: QueryService { throw QueryServiceError.notSupported } + override public func autoConvertTraditionalChinese() -> Bool { + // If translate traditionalChinese <--> simplifiedChinese, use Ali API directly. + if EZLanguageManager.shared().onlyContainsChineseLanguages([queryModel.queryFromLanguage, queryModel.queryTargetLanguage]) { + return false + } + return true + } + override func translate(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) { let transType = AliTranslateType.transType(from: from, to: to) guard transType != .unsupported else { diff --git a/Easydict/Feature/Service/Ali/AliTranslateType.swift b/Easydict/Feature/Service/Ali/AliTranslateType.swift index 3227eda91..ccf0cbf11 100644 --- a/Easydict/Feature/Service/Ali/AliTranslateType.swift +++ b/Easydict/Feature/Service/Ali/AliTranslateType.swift @@ -11,14 +11,18 @@ import Foundation struct AliTranslateType: Equatable { var sourceLanguage: String var targetLanguage: String - + static let unsupported = AliTranslateType(sourceLanguage: "unsupported", targetLanguage: "unsupported") - - /// https://help.aliyun.com/document_detail/215387.html?spm=a2c4g.158269.0.0.48d54b8ab1Jeol#h2-url-1 + + /// https://help.aliyun.com/zh/machine-translation/support/supported-languages-and-codes#h2-url-1 static let supportLanguagesDictionary: [Language: String] = [ .auto: "auto", .simplifiedChinese: "zh", - .traditionalChinese: "zh-tw", + + /** + traditionalChinese code is "zh-tw", but Ali only support traditionalChinese <--> simplifiedChinese, so we convert traditionalChinese manually. + */ + .traditionalChinese: "zh", // "zh-tw" .english: "en", .japanese: "ja", .korean: "ko", @@ -63,25 +67,37 @@ struct AliTranslateType: Equatable { .mongolian: "mn", .hebrew: "he", ] - + static func transType(from: Language, to: Language) -> AliTranslateType { /** 文本翻译除繁体中文、蒙语、粤语外,其他212种语言,可支持任意两种语言之间互译。繁体中文、蒙语、粤语仅支持与中文之间的互译。文本翻译支持源语言的自动语言检测,语言代码为auto(粤语为源语言时,不支持使用auto作为语言代码)。 + + https://help.aliyun.com/zh/machine-translation/support/supported-languages-and-codes */ - if from == .traditionalChinese || from == .mongolian, to != .simplifiedChinese { - return .unsupported - } else if to == .traditionalChinese, from != .simplifiedChinese { - return .unsupported - } else if to == .mongolian, from != .simplifiedChinese { + + if from == .mongolian, !to.isKindOfChinese() || to == .mongolian, !from.isKindOfChinese() { return .unsupported } - - guard let fromLanguage = supportLanguagesDictionary[from], - let toLanguage = supportLanguagesDictionary[to] + + guard var fromLanguage = supportLanguagesDictionary[from], + var toLanguage = supportLanguagesDictionary[to] else { return .unsupported } - + + // If translate traditionalChinese <--> simplifiedChinese, use Ali API directly. + if EZLanguageManager.shared().onlyContainsChineseLanguages([from, to]) { + let traditionalLangaugeCode = "zh-tw" + + // Maybe traditionalChinese --> traditionalChinese + if from == .traditionalChinese { + fromLanguage = traditionalLangaugeCode + } + if to == .traditionalChinese { + toLanguage = traditionalLangaugeCode + } + } + return AliTranslateType(sourceLanguage: fromLanguage, targetLanguage: toLanguage) } } diff --git a/Easydict/Feature/Service/Language/EZLanguageModel.h b/Easydict/Feature/Service/Language/EZLanguageModel.h index 7152182a4..6401290f2 100644 --- a/Easydict/Feature/Service/Language/EZLanguageModel.h +++ b/Easydict/Feature/Service/Language/EZLanguageModel.h @@ -69,6 +69,8 @@ FOUNDATION_EXPORT EZLanguage const EZLanguageCroatian; FOUNDATION_EXPORT EZLanguage const EZLanguageMongolian; FOUNDATION_EXPORT EZLanguage const EZLanguageHebrew; +FOUNDATION_EXPORT EZLanguage const EZLanguageUnsupported; + @interface EZLanguageModel : NSObject @property (nonatomic, copy) NSString *chineseName; diff --git a/Easydict/Feature/Service/Language/EZLanguageModel.m b/Easydict/Feature/Service/Language/EZLanguageModel.m index 472072272..cf01f194d 100644 --- a/Easydict/Feature/Service/Language/EZLanguageModel.m +++ b/Easydict/Feature/Service/Language/EZLanguageModel.m @@ -59,6 +59,9 @@ NSString *const EZLanguageMongolian = @"Mongolian"; NSString *const EZLanguageHebrew = @"Hebrew"; +NSString *const EZLanguageUnsupported = @"unsupported"; + + @implementation EZLanguageModel // 目前总计支持 49 种语言:简体中文,繁体中文,文言文,英语,日语,韩语,法语,西班牙语,葡萄牙语,意大利语,德语,俄语,阿拉伯语,瑞典语,罗马尼亚语,泰语,斯洛伐克语,荷兰语,匈牙利语,希腊语,丹麦语,芬兰语,波兰语,捷克语,土耳其语,立陶宛语,拉脱维亚语,乌克兰语,保加利亚语,印尼语,马来语,斯洛文尼亚语,爱沙尼亚语,越南语,波斯语,印地语,泰卢固语,泰米尔语,乌尔都语,菲律宾语,高棉语,老挝语,孟加拉语,缅甸语,挪威语,塞尔维亚语,克罗地亚语,蒙古语,希伯来语。 diff --git a/Easydict/Feature/Service/Tencent/TencentTranslateType.swift b/Easydict/Feature/Service/Tencent/TencentTranslateType.swift index c7df83236..e82753154 100644 --- a/Easydict/Feature/Service/Tencent/TencentTranslateType.swift +++ b/Easydict/Feature/Service/Tencent/TencentTranslateType.swift @@ -77,14 +77,14 @@ struct TencentTranslateType: Equatable { } extension [Language] { - // Contains Chinese language + /// Contains Chinese language, func containsChinese() -> Bool { contains { $0.isKindOfChinese() } } } extension Language { - // Is kind of Chinese language + /// Is kind of Chinese language, means it is simplifiedChinese or traditionalChinese. func isKindOfChinese() -> Bool { self == .simplifiedChinese || self == .traditionalChinese }