Skip to content

Commit

Permalink
add ali translate support (#294)
Browse files Browse the repository at this point in the history
* add ali translate support

* use camel case to name AliAPIResponse

* use prefix func to get a substring
  • Loading branch information
choykarl authored and tisfeng committed Jan 6, 2024
1 parent 619bd59 commit 788d6f4
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@
6295DE312A84D82E006145F4 /* EZBingTranslateModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE302A84D82E006145F4 /* EZBingTranslateModel.m */; };
6295DE342A84EF76006145F4 /* EZBingLookupModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE332A84EF76006145F4 /* EZBingLookupModel.m */; };
62A2D03F2A82967F007EEB01 /* EZBingRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A2D03E2A82967F007EEB01 /* EZBingRequest.m */; };
62E2BF4A2B4082BA00E42D38 /* AliService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF472B4082BA00E42D38 /* AliService.swift */; };
62E2BF4B2B4082BA00E42D38 /* AliResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF482B4082BA00E42D38 /* AliResponse.swift */; };
62E2BF4C2B4082BA00E42D38 /* AliTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF492B4082BA00E42D38 /* AliTranslateType.swift */; };
62ED29A22B15F1F500901F51 /* EZWrapView.m in Sources */ = {isa = PBXBuildFile; fileRef = 62ED29A12B15F1F500901F51 /* EZWrapView.m */; };
9672D7D22B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 9672D7D12B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m */; };
A0B65CA0F31AC8ECFB8347CC /* Pods_EasydictTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 378E73A7EA8FC8FB9C975A63 /* Pods_EasydictTests.framework */; };
Expand Down Expand Up @@ -719,6 +722,9 @@
6295DE332A84EF76006145F4 /* EZBingLookupModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZBingLookupModel.m; sourceTree = "<group>"; };
62A2D03D2A82967F007EEB01 /* EZBingRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZBingRequest.h; sourceTree = "<group>"; };
62A2D03E2A82967F007EEB01 /* EZBingRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZBingRequest.m; sourceTree = "<group>"; };
62E2BF472B4082BA00E42D38 /* AliService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliService.swift; sourceTree = "<group>"; };
62E2BF482B4082BA00E42D38 /* AliResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliResponse.swift; sourceTree = "<group>"; };
62E2BF492B4082BA00E42D38 /* AliTranslateType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliTranslateType.swift; sourceTree = "<group>"; };
62ED29A02B15F1F500901F51 /* EZWrapView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZWrapView.h; sourceTree = "<group>"; };
62ED29A12B15F1F500901F51 /* EZWrapView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZWrapView.m; sourceTree = "<group>"; };
6372B33DFF803C7096A82250 /* Pods_Easydict.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Easydict.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -1275,6 +1281,7 @@
03B0222B29231FA6001C7E63 /* Service */ = {
isa = PBXGroup;
children = (
62E2BF462B4082BA00E42D38 /* Ali */,
17BCAEF22B0DFF9000A7D372 /* Niutrans */,
2746AEBF2AF95040005FE0A1 /* Caiyun */,
C4DD01E72B12B3B00025EE8E /* Tencent */,
Expand Down Expand Up @@ -2049,6 +2056,16 @@
path = Bing;
sourceTree = "<group>";
};
62E2BF462B4082BA00E42D38 /* Ali */ = {
isa = PBXGroup;
children = (
62E2BF472B4082BA00E42D38 /* AliService.swift */,
62E2BF482B4082BA00E42D38 /* AliResponse.swift */,
62E2BF492B4082BA00E42D38 /* AliTranslateType.swift */,
);
path = Ali;
sourceTree = "<group>";
};
62ED299F2B15F1BE00901F51 /* EZWrapView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2496,6 +2513,7 @@
0309E1F0292B4A5E00AFB76A /* NSView+EZGetViewController.m in Sources */,
03B0232F29231FA6001C7E63 /* MMCrashFileTool.m in Sources */,
03B0233629231FA6001C7E63 /* MMEventMonitor.m in Sources */,
62E2BF4A2B4082BA00E42D38 /* AliService.swift in Sources */,
03B0233729231FA6001C7E63 /* MMMake.m in Sources */,
03B0232E29231FA6001C7E63 /* MMCrashSignalExceptionHandler.m in Sources */,
03BDA7C42A26DA280079D04F /* NSDictionary+RubyDescription.m in Sources */,
Expand Down Expand Up @@ -2583,6 +2601,7 @@
0333FDA32A035BEC00891515 /* NSArray+EZChineseText.m in Sources */,
03B0233229231FA6001C7E63 /* MMLog.swift in Sources */,
03DC7C5E2A3ABE28000BF7C9 /* EZConstKey.m in Sources */,
62E2BF4C2B4082BA00E42D38 /* AliTranslateType.swift in Sources */,
03E3E7C22ADE318800812C84 /* EZQueryMenuTextView.m in Sources */,
03B0231829231FA6001C7E63 /* SnipWindowController.m in Sources */,
03542A342936F70F00C34C33 /* EZLanguageManager.m in Sources */,
Expand Down Expand Up @@ -2644,6 +2663,7 @@
03DC7C662A3CA465000BF7C9 /* HWSegmentedControl.m in Sources */,
037E006D2B3DC098006491C6 /* EZOpenAIService+EZPromptMessages.m in Sources */,
03B022E929231FA6001C7E63 /* AppDelegate.m in Sources */,
62E2BF4B2B4082BA00E42D38 /* AliResponse.swift in Sources */,
03B0232729231FA6001C7E63 /* NSColor+MM.m in Sources */,
03B0233529231FA6001C7E63 /* MMFileLogFormatter.m in Sources */,
03DC38C1292CC97900922CB2 /* EZServiceInfo.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "ali translate.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Easydict/App/Easydict-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
#import "AppDelegate.h"
#import "EZConfiguration.h"
#import "EZEnumTypes.h"

#import "NSString+EZConvenience.h"
17 changes: 17 additions & 0 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
}
}
},
"ali_translate" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Ali Translate"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "阿里翻译"
}
}
}
},
"Alignment Error" : {
"comment" : "Error description",
"localizations" : {
Expand Down
142 changes: 142 additions & 0 deletions Easydict/Feature/Service/Ali/AliResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// AliResponse.swift
// Easydict
//
// Created by choykarl on 2023/12/20.
// Copyright © 2023 izual. All rights reserved.
//

import Foundation

/**
{
"requestId": "",
"success": true,
"httpStatusCode": 200,
"code": "",
"message": "",
"data": {
"translateText": "你好",
"detectLanguage": "en"
}
}

error:
{
"requestId": "",
"success": false,
"httpStatusCode": 500,
"code": "ParamError",
"message": "Query length limit exceeded",
"data": null
}
*/

struct AliWebResponse: Codable {
struct Data: Codable {
var translateText: String?
var detectLanguage: String?
}

var requestId: String?
var success: Bool
var httpStatusCode: Int?
var code: AnyCodable?
var message: String?
var data: Data?
}

/**
{
"Code" : "200",
"Data" : {
"Translated" : "你好",
"WordCount" : "5"
},
"RequestId" : ""
}

{
"Code" : "InvalidAccessKeyId.NotFound",
"HostId" : "mt.aliyuncs.com",
"Message" : "Specified access key is not found.",
"Recommend" : "",
"RequestId" : ""
}

*/
struct AliAPIResponse: Codable {
struct Data: Codable {
var translated: String?
var wordCount: String?

enum CodingKeys: String, CodingKey {
case translated = "Translated"
case wordCount = "WordCount"
}
}

var code: AnyCodable?
var data: Data?
var requestId: String?
var message: String?
var hostId: String?
var recommend: String?

enum CodingKeys: String, CodingKey {
case data = "Data"
case code = "Code"
case requestId = "RequestId"
case message = "Message"
case hostId = "HostId"
case recommend = "Recommend"
}
}

/**
{
"token": "",
"parameterName": "",
"headerName": ""
}
*/

struct AliTokenResponse: Codable {
var token: String?
var parameterName: String?
var headerName: String?
}

enum AnyCodable: Codable {
case string(String)
case int(Int)

init(from decoder: Decoder) throws {
if let intValue = try? decoder.singleValueContainer().decode(Int.self) {
self = .int(intValue)
} else if let stringValue = try? decoder.singleValueContainer().decode(String.self) {
self = .string(stringValue)
} else {
throw try DecodingError.dataCorruptedError(in: decoder.singleValueContainer(), debugDescription: "Code is neither Int nor String")
}
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .string(stringValue):
try container.encode(stringValue)
case let .int(intValue):
try container.encode(intValue)
}
}

var stringValue: String? {
switch self {
case let .int(i):
return String(i)
case let .string(s):
return s
}
}
}
Loading

0 comments on commit 788d6f4

Please sign in to comment.