Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Baidu Api Key And Switch Ali Service type #613

Merged
merged 31 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d6d04b1
docs: update README
tisfeng May 4, 2024
d6a2817
Merge branch 'dev'
tisfeng May 12, 2024
30dd06d
chore(deps): bump rexml in the bundler group across 1 directory (#558)
dependabot[bot] May 18, 2024
381033d
chore: update star-fork-notification trigger condition
tisfeng May 31, 2024
566ab7b
feat: baidu api translate
choykarl Jul 14, 2024
19b613a
feat: add file
choykarl Jul 14, 2024
346db3d
Merge branch 'dev' into baidu_key
choykarl Jul 14, 2024
01a793d
feat: baidu key config view
choykarl Jul 14, 2024
77a95b9
Merge branch 'dev' into baidu_key
tisfeng Jul 14, 2024
f347776
pref: rename param
choykarl Jul 14, 2024
ac831e6
Update Easydict/objc/Service/Baidu/BaiduApiTranslate.swift
choykarl Jul 15, 2024
037df73
Update Easydict/objc/Service/Baidu/BaiduApiTranslate.swift
choykarl Jul 15, 2024
684cf1c
Update Easydict/objc/Service/Baidu/BaiduApiTranslate.swift
choykarl Jul 15, 2024
6dd3654
Update Easydict/App/Localizable.xcstrings
choykarl Jul 15, 2024
29fc9c6
Update Easydict/App/Localizable.xcstrings
choykarl Jul 15, 2024
bb7283f
revert version value
choykarl Jul 15, 2024
af1d739
Merge branch 'dev' into baidu_key
choykarl Jul 15, 2024
ae2cb72
Merge branch 'dev' into baidu_key
tisfeng Jul 16, 2024
4af4edf
feat: baidu service api picker
choykarl Jul 18, 2024
a8e826b
feat: listen appid and secretkey
choykarl Jul 18, 2024
07a5e8e
Merge branch 'baidu_key' of github.com:choykarl/Easydict into baidu_key
choykarl Jul 18, 2024
ce6178a
feat: baidu api type picker
choykarl Jul 20, 2024
0b88f1b
Merge branch 'dev' into baidu_key
tisfeng Jul 20, 2024
ae1f0c7
refactor: refactor localization and API handling for consistency
tisfeng Jul 20, 2024
f46337f
Merge branch 'dev' into baidu_key
tisfeng Jul 22, 2024
3fe1bb2
feat: modify localized
choykarl Jul 25, 2024
32023ba
feat: ali service type picker
choykarl Jul 25, 2024
f0516bf
fix: missing file ServiceAPIType
tisfeng Jul 25, 2024
3ec60e9
feat: add ServiceAPIType file
choykarl Jul 25, 2024
1d3fea0
Revert "feat: add ServiceAPIType file"
tisfeng Jul 25, 2024
42257bd
fix: improve api key missing tips
tisfeng Jul 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
03542A5E2938F05B00C34C33 /* EZLanguageModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 03542A5D2938F05B00C34C33 /* EZLanguageModel.m */; };
0357B95A2C04387D00A48CB0 /* TextEditorCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0357B9592C04387D00A48CB0 /* TextEditorCell.swift */; };
035E37E72A0953120061DFAF /* EZToast.m in Sources */ = {isa = PBXBuildFile; fileRef = 035E37E62A0953120061DFAF /* EZToast.m */; };
035F9CCF2C529A04005D1C9A /* ServiceAPIType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035F9CCE2C529A04005D1C9A /* ServiceAPIType.swift */; };
0361965529FFECFC00806370 /* youdao-sign.js in Resources */ = {isa = PBXBuildFile; fileRef = 0361965429FFECFC00806370 /* youdao-sign.js */; };
036196752A000F5900806370 /* FWEncryptorAES.m in Sources */ = {isa = PBXBuildFile; fileRef = 036196702A000F5800806370 /* FWEncryptorAES.m */; };
036196762A000F5900806370 /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 036196722A000F5900806370 /* NSData+Base64.m */; };
Expand Down Expand Up @@ -301,6 +302,9 @@
C4DD01EB2B12BA250025EE8E /* TencentResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4DD01EA2B12BA250025EE8E /* TencentResponse.swift */; };
C4DD01ED2B12BE9B0025EE8E /* TencentTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4DD01EC2B12BE9B0025EE8E /* TencentTranslateType.swift */; };
C4DE3D6D2AC00EB500C2B85D /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = C4DE3D6C2AC00EB500C2B85D /* Localizable.xcstrings */; };
CB8C42FC2C441B5B004EC86F /* BaiduTranslate+ConfigurableService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8C42FB2C441B5A004EC86F /* BaiduTranslate+ConfigurableService.swift */; };
CB8C42FF2C441B74004EC86F /* BaiduApiTranslate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8C42FD2C441B74004EC86F /* BaiduApiTranslate.swift */; };
CB8C43002C441B74004EC86F /* BaiduApiResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8C42FE2C441B74004EC86F /* BaiduApiResponse.swift */; };
DC46DF802B4417B900DEAE3E /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC46DF7F2B4417B900DEAE3E /* Configuration.swift */; };
DC6D9C892B3969510055EFFC /* Appearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6D9C882B3969510055EFFC /* Appearance.swift */; };
DCF176F22B57CED700CA6026 /* Configuration+UserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCF176F12B57CED700CA6026 /* Configuration+UserData.swift */; };
Expand Down Expand Up @@ -432,6 +436,7 @@
0357B9592C04387D00A48CB0 /* TextEditorCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextEditorCell.swift; sourceTree = "<group>"; };
035E37E52A0953120061DFAF /* EZToast.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZToast.h; sourceTree = "<group>"; };
035E37E62A0953120061DFAF /* EZToast.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZToast.m; sourceTree = "<group>"; };
035F9CCE2C529A04005D1C9A /* ServiceAPIType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ServiceAPIType.swift; path = Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/ServiceAPIType.swift; sourceTree = SOURCE_ROOT; };
0361965429FFECFC00806370 /* youdao-sign.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "youdao-sign.js"; sourceTree = "<group>"; };
0361966F2A000F5800806370 /* NSData+Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+Base64.h"; sourceTree = "<group>"; };
036196702A000F5800806370 /* FWEncryptorAES.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FWEncryptorAES.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -803,6 +808,9 @@
C4DD01EC2B12BE9B0025EE8E /* TencentTranslateType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentTranslateType.swift; sourceTree = "<group>"; };
C4DE3D6C2AC00EB500C2B85D /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; name = Localizable.xcstrings; path = Easydict/App/Localizable.xcstrings; sourceTree = SOURCE_ROOT; };
C99EEB182385796700FEE666 /* Easydict-debug.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Easydict-debug.app"; sourceTree = BUILT_PRODUCTS_DIR; };
CB8C42FB2C441B5A004EC86F /* BaiduTranslate+ConfigurableService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BaiduTranslate+ConfigurableService.swift"; sourceTree = "<group>"; };
CB8C42FD2C441B74004EC86F /* BaiduApiTranslate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaiduApiTranslate.swift; sourceTree = "<group>"; };
CB8C42FE2C441B74004EC86F /* BaiduApiResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaiduApiResponse.swift; sourceTree = "<group>"; };
DC46DF7F2B4417B900DEAE3E /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
DC6D9C882B3969510055EFFC /* Appearance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appearance.swift; sourceTree = "<group>"; };
DCF176F12B57CED700CA6026 /* Configuration+UserData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Configuration+UserData.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1432,6 +1440,8 @@
03B0222C29231FA6001C7E63 /* Baidu */ = {
isa = PBXGroup;
children = (
CB8C42FE2C441B74004EC86F /* BaiduApiResponse.swift */,
CB8C42FD2C441B74004EC86F /* BaiduApiTranslate.swift */,
03542A412937B45E00C34C33 /* EZBaiduTranslate.h */,
03542A422937B45E00C34C33 /* EZBaiduTranslate.m */,
03542A442937B4C300C34C33 /* EZBaiduTranslateResponse.h */,
Expand Down Expand Up @@ -2414,6 +2424,7 @@
0AC8A8462B6A4E3F006DA5CC /* ServiceConfigurationSecretSectionView.swift */,
0AC8A8442B6A4D97006DA5CC /* ServiceConfigurationCells.swift */,
0357B9592C04387D00A48CB0 /* TextEditorCell.swift */,
035F9CCE2C529A04005D1C9A /* ServiceAPIType.swift */,
);
path = ServiceConfigurationView;
sourceTree = "<group>";
Expand All @@ -2428,6 +2439,7 @@
0AC8A8382B666F07006DA5CC /* CaiyunService+ConfigurableService.swift */,
0AC8A83A2B6682D4006DA5CC /* AliService+ConfigurableService.swift */,
0AC8A8422B6957B0006DA5CC /* BingService+ConfigurableService.swift */,
CB8C42FB2C441B5A004EC86F /* BaiduTranslate+ConfigurableService.swift */,
);
path = ConfigurationView;
sourceTree = "<group>";
Expand Down Expand Up @@ -2889,6 +2901,7 @@
03B3B8B52925DD3D00168E8D /* EZPopButtonViewController.m in Sources */,
03542A5B2938DA2B00C34C33 /* EZDetectLanguageButton.m in Sources */,
03B0232929231FA6001C7E63 /* NSDictionary+MM.m in Sources */,
035F9CCF2C529A04005D1C9A /* ServiceAPIType.swift in Sources */,
03280B812C23FE4A00E75A24 /* StreamConfigurationView.swift in Sources */,
0333FDA32A035BEC00891515 /* NSArray+EZChineseText.m in Sources */,
03B0233229231FA6001C7E63 /* MMLog.swift in Sources */,
Expand All @@ -2903,6 +2916,7 @@
0361967B2A0037F700806370 /* NSData+EZMD5.m in Sources */,
967712EE2B5B943400105E0F /* Shortcut.swift in Sources */,
03BFFC68295F4B87004E033E /* EZYoudaoDictModel.m in Sources */,
CB8C42FF2C441B74004EC86F /* BaiduApiTranslate.swift in Sources */,
03247E3A296AE8EC00AFCD67 /* EZLoadingAnimationView.m in Sources */,
0396D615292CC4C3006A11D9 /* EZLocalStorage.m in Sources */,
0329CD6F29EE924500963F78 /* EZRightClickDetector.m in Sources */,
Expand Down Expand Up @@ -2931,6 +2945,7 @@
9643D9462B71D103000FBEA6 /* KeyHolderRowView.swift in Sources */,
0399C6A829A74E0F00B4AFCC /* EZQueryResult+EZDeepLTranslateResponse.m in Sources */,
039B694F2A9D9F370063709D /* EZWebViewManager.m in Sources */,
CB8C42FC2C441B5B004EC86F /* BaiduTranslate+ConfigurableService.swift in Sources */,
03B0231629231FA6001C7E63 /* SnipFocusView.m in Sources */,
03FB3EDD2B1B405B004C3238 /* TencentSigning.swift in Sources */,
03B0230329231FA6001C7E63 /* EZResultView.m in Sources */,
Expand Down Expand Up @@ -2969,6 +2984,7 @@
EAE3D3502B62E9DE001EE3E3 /* GlobalContext.swift in Sources */,
EA9943F02B5354C400EE7B97 /* ShowWindowPositionExtensions.swift in Sources */,
96B2F1C52C07782400AD3126 /* RepoInfoHelper.swift in Sources */,
CB8C43002C441B74004EC86F /* BaiduApiResponse.swift in Sources */,
03B0233129231FA6001C7E63 /* MMCrash.m in Sources */,
03B0232629231FA6001C7E63 /* NSAttributedString+MM.m in Sources */,
03542A402937B3C900C34C33 /* EZOCRResult.m in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Easydict.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "7ec61cc8c0ffc99d533d209ed8dd5c63dc918d25ea4172d03761bf84e053e81b",
"originHash" : "c08faef207d4abe4942f46cbbff9587355018cfffbc88badfc29bcb2100a7649",
"pins" : [
{
"identity" : "abseil-cpp-binary",
Expand Down Expand Up @@ -87,8 +87,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/generative-ai-swift",
"state" : {
"revision" : "5d750b80651da9721c37c5eb1fc0b6750d1884d3",
"version" : "0.5.3"
"revision" : "54784005b57c2235a7669e0c12e8dafa68f0ca5f",
"version" : "0.5.4"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions Easydict/App/Easydict-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
#import "EZDeepLTranslate.h"
#import "EZBingService.h"
#import "NSString+EZUtils.h"
#import "EZBaiduTranslate.h"
#import "MMCrash.h"
88 changes: 87 additions & 1 deletion Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
}
},
"baidu_translate" : {
"comment" : "Baidu Translate",
"localizations" : {
"en" : {
"stringUnit" : {
Expand Down Expand Up @@ -908,6 +909,23 @@
}
}
},
"error_missing_api_key" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Missing API Key"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "缺少 API Key"
}
}
}
},
"error_network" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -2326,6 +2344,74 @@
}
}
},
"service.configuration.api_missing.tips %@" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Currently using the Secret Key API type, but a certain API Key is empty. Please go to Settings > Service > %@, fill in the required configuration information, or switch API types."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "当前正使用 Secret Key API 类型,但是某个 API Key 为空,请前往设置-服务-%@,填写相关配置信息,或切换 API 类型"
}
}
}
},
"service.configuration.api_picker.title" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "API Type"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "API 类型"
}
}
}
},
"service.configuration.baidu.app_id.title" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "App ID"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "App ID"
}
}
}
},
"service.configuration.baidu.secret_key.title" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Secret Key"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "Secret Key"
}
}
}
},
"service.configuration.bing.cookie.title" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -4225,4 +4311,4 @@
}
},
"version" : "1.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ extension Defaults.Keys {
// Ali
static let aliAccessKeyId = Key<String>(EZAliAccessKeyId, default: "")
static let aliAccessKeySecret = Key<String>(EZAliAccessKeySecret, default: "")
static let aliServiceApiTypeKey = Key<ServiceAPIType>(EZAliServiceApiTypeKey, default: .secretKey)

// baidu
static let baiduAppId = Key<String>(EZBaiduAppId, default: "")
static let baiduSecretKey = Key<String>(EZBaiduSecretKey, default: "")
static let baiduServiceApiTypeKey = Key<ServiceAPIType>(EZBaiduServiceApiTypeKey, default: .secretKey)
}

/// shortcut
Expand Down
26 changes: 15 additions & 11 deletions Easydict/Swift/Service/Ali/AliService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ class AliService: QueryService {
.ali
}

override func hasPrivateAPIKey() -> Bool {
!aliAccessKeyId.isEmpty && !aliAccessKeySecret.isEmpty
}

override func translate(
_ text: String,
from: Language,
Expand All @@ -78,13 +74,7 @@ class AliService: QueryService {
return
}

/**
use user's access key id and secret
easydict://writeKeyValue?EZAliAccessKeyId=
easydict://writeKeyValue?EZAliAccessKeySecret=
*/
if !aliAccessKeyId.isEmpty,
!aliAccessKeySecret.isEmpty {
if Defaults[.aliServiceApiTypeKey] == .secretKey {
requestByAPI(
id: aliAccessKeyId,
secret: aliAccessKeySecret,
Expand Down Expand Up @@ -152,6 +142,20 @@ class AliService: QueryService {
to: Language,
completion: @escaping (EZQueryResult, Error?) -> ()
) {
if id.isEmpty || secret.isEmpty {
completion(
result,
EZError(
type: EZErrorType.missingAPIKey,
description: String.localizedStringWithFormat(
NSLocalizedString("service.configuration.api_missing.tips %@", comment: "API key missing"),
name()
)
)
)
return
}

func hmacSha1(key: String, params: String) -> String? {
guard let secret = key.data(using: .utf8),
let what = params.data(using: .utf8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import SwiftUI
extension AliService {
override func configurationListItems() -> Any? {
ServiceConfigurationSecretSectionView(service: self, observeKeys: [.aliAccessKeyId, .aliAccessKeySecret]) {
ServiceConfigurationPickerCell(
titleKey: "service.configuration.api_picker.title",
key: .aliServiceApiTypeKey,
values: ServiceAPIType.allCases
)
ServiceConfigurationSecureInputCell(
textFieldTitleKey: "service.configuration.ali.access_key_id.title",
key: .aliAccessKeyId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// BaiduService+ConfigurableService.swift
// Easydict
//
// Created by karl on 2024/7/13.
// Copyright © 2024 izual. All rights reserved.
//

import Combine
import Foundation
import SwiftUI

extension EZBaiduTranslate {
open override func configurationListItems() -> Any {
ServiceConfigurationSecretSectionView(
service: self,
observeKeys: [.baiduAppId, .baiduSecretKey]
) {
ServiceConfigurationPickerCell(
titleKey: "service.configuration.api_picker.title",
key: .baiduServiceApiTypeKey,
values: ServiceAPIType.allCases
)

ServiceConfigurationSecureInputCell(
textFieldTitleKey: "service.configuration.baidu.app_id.title",
key: .baiduAppId
)

ServiceConfigurationSecureInputCell(
textFieldTitleKey: "service.configuration.baidu.secret_key.title",
key: .baiduSecretKey
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ServiceAPIType.swift
// Easydict
//
// Created by karl on 2024/7/25.
// Copyright © 2024 izual. All rights reserved.
//

import Defaults
import Foundation
import SwiftUI

enum ServiceAPIType: String, CaseIterable, Defaults.Serializable, EnumLocalizedStringConvertible {
case web = "Web API"
case secretKey = "Secret Key API"

// MARK: Internal

var title: LocalizedStringKey {
LocalizedStringKey(rawValue)
}
}
Loading