Skip to content

Commit

Permalink
perf: make Ali support traditional Chinese
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 21, 2023
1 parent 6c7694e commit 94cf2e0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
8 changes: 8 additions & 0 deletions Easydict/Feature/Service/Ali/AliService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 30 additions & 14 deletions Easydict/Feature/Service/Ali/AliTranslateType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions Easydict/Feature/Service/Language/EZLanguageModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Easydict/Feature/Service/Language/EZLanguageModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
NSString *const EZLanguageMongolian = @"Mongolian";
NSString *const EZLanguageHebrew = @"Hebrew";

NSString *const EZLanguageUnsupported = @"unsupported";


@implementation EZLanguageModel

// 目前总计支持 49 种语言:简体中文,繁体中文,文言文,英语,日语,韩语,法语,西班牙语,葡萄牙语,意大利语,德语,俄语,阿拉伯语,瑞典语,罗马尼亚语,泰语,斯洛伐克语,荷兰语,匈牙利语,希腊语,丹麦语,芬兰语,波兰语,捷克语,土耳其语,立陶宛语,拉脱维亚语,乌克兰语,保加利亚语,印尼语,马来语,斯洛文尼亚语,爱沙尼亚语,越南语,波斯语,印地语,泰卢固语,泰米尔语,乌尔都语,菲律宾语,高棉语,老挝语,孟加拉语,缅甸语,挪威语,塞尔维亚语,克罗地亚语,蒙古语,希伯来语。
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Tencent/TencentTranslateType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 94cf2e0

Please sign in to comment.