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

Add Ali translate service #271

Closed
wants to merge 18 commits into from
Closed

Add Ali translate service #271

wants to merge 18 commits into from

Conversation

choykarl
Copy link
Collaborator

@choykarl choykarl commented Dec 21, 2023

close #65

@tisfeng
Copy link
Owner

tisfeng commented Dec 21, 2023

啊这,这接口你是抓包用的网页版?这个接口有什么限制没有,比如类似 DeepL 频率限制?

image

@tisfeng
Copy link
Owner

tisfeng commented Dec 21, 2023

野生的网页 API 可能不稳定,有时候接口会变动,或者有频次限制等,DeepL 是没办法,获取官方 API key 太难了。

对于这种有官方 API,且提供一定免费额度的接口,建议还是优先用官方的 API,这样更稳定可靠。

主账号每月免费额度一百万字符(子账号共享主账号的每月免费额度)

if let data = response.data {
do {
let errorResponse = try JSONDecoder().decode(TencentErrorResponse.self, from: data)
ezError?.errorDataMessage = errorResponse.response.error.message
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

阿里翻译,怎么会用到腾讯翻译的模型 TencentErrorResponse

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个报错类型如果未知,干脆包一层抛给 EZError,直接显示原始的报错信息。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦,我忘记了,Alamofire 和 AFNetworking 的错误类型不同,AFError 没有带 userInfo 信息,因此 Swift 这边如果不自己解析错误信息,就需要把 response.data 传过去。

let ezError = EZError(nsError: error, errorResponseData: response.data)

@choykarl
Copy link
Collaborator Author

sorry,这次弄的有点仓促了,我明天改下。
接口是阿里网页的接口,我是看了bob的阿里插件用的这个。
那个code500我看返回的错误提示就是不支持的语言,所以我用了unsupportedLanguage。

@tisfeng
Copy link
Owner

tisfeng commented Dec 21, 2023

没事,这个网页接口也不用删,可以放着。

@tisfeng
Copy link
Owner

tisfeng commented Dec 21, 2023

优化了一下,让阿里翻译各种语言都支持了繁体中文。

对于一些不支持繁体中文的服务,例如 DeepL,此时只要将繁体中文视为简体中文 <traditionalChinese: "zh">,然后让 autoConvertTraditionalChinese 返回 YES,就能自动支持繁体中文了。

至于原理,简单来说,就是内部我们自己手动将简体中文转为繁体中文。

具体可以看这个提交 94cf2e0

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

Copy link

gitguardian bot commented Dec 22, 2023

⚠️ GitGuardian has uncovered 3 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id Secret Commit Filename
- Generic High Entropy Secret bcd5bd2 Easydict/Feature/Service/Ali/AliResponse.swift View secret
- Generic High Entropy Secret 29e2763 Easydict/Feature/Service/Ali/AliResponse.swift View secret
- Alibaba Cloud Keys 50c01b9 Easydict/Feature/Service/Ali/AliService.swift View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

@choykarl
Copy link
Collaborator Author

发现一个很奇怪的事情,有些中文翻译会报错,例如“生死难料”这个词在阿里翻译里会直接报错,网页上翻页也会报错。
报错信息是“Query csi check not pass”。没太懂这是啥意思。。。

@choykarl
Copy link
Collaborator Author

这个GitGuardian错误应该是我注释里面有个token,但是我现在删掉也不行,也检查不过去。。。

@tisfeng
Copy link
Owner

tisfeng commented Dec 22, 2023

GitGuardian 这个主要是提醒作用,如果是一些隐私的 key 之类的,revoke 掉就好。

如果要去掉这个警告,可能需要你本地重写一下提交历史,把 token 相关的去掉,然后强推。

@tisfeng
Copy link
Owner

tisfeng commented Dec 22, 2023

我没看懂,你这个修改,好像只添加了一个 _csrf 参数,之前的 get 接口有什么问题?

@tisfeng
Copy link
Owner

tisfeng commented Dec 22, 2023

野生的网页 API 可能不稳定,有时候接口会变动,或者有频次限制等,DeepL 是没办法,获取官方 API key 太难了。

对于这种有官方 API,且提供一定免费额度的接口,建议还是优先用官方的 API,这样更稳定可靠。

主账号每月免费额度一百万字符(子账号共享主账号的每月免费额度)

另外,其实我是希望你能添加阿里翻译的官方 API

@choykarl
Copy link
Collaborator Author

这个post是我抓的网页翻译的接口,之前get是我看bob的插件写的,但是我没看到那个get在哪里有用,可能是网页之前用的是get现在换成了post了吧。所以我两个都保留了,算是做一个兜底。

@choykarl
Copy link
Collaborator Author

野生的网页 API 可能不稳定,有时候接口会变动,或者有频次限制等,DeepL 是没办法,获取官方 API key 太难了。

对于这种有官方 API,且提供一定免费额度的接口,建议还是优先用官方的 API,这样更稳定可靠。

主账号每月免费额度一百万字符(子账号共享主账号的每月免费额度)

另外,其实我是希望你能添加阿里翻译的官方 API

额,我看那个api是需要收费的啊😳

@tisfeng
Copy link
Owner

tisfeng commented Dec 23, 2023

@choykarl
Copy link
Collaborator Author

看官方文档 https://help.aliyun.com/zh/machine-translation/product-overview/pricing-of-machine-translation

image

我们这是开源的软件,用个人账号的api key授权不会有问题么,而且我看api调用好像QPS还限制了50。

@tisfeng
Copy link
Owner

tisfeng commented Dec 23, 2023

额,有什么问题?

参考目前已添加的腾讯翻译,大体上我们只负责接入官方 API,至于需要 API key,这个让用户自己去申请就好了。

但为了新用户方便使用,我们可以内置一个有免费额度限制的 API key,这个通常用的是我自己额外注册申请的一个账号下的 key。比如腾讯翻译每月有 500 万免费额度,我在程序内简单限制了一下每个新用户的使用额度。另外如果这个月 500 万免费额度用完了,也同样不能使用了,会报错提示。

具体你可以看一下腾讯翻译的代码。

@choykarl
Copy link
Collaborator Author

额,有什么问题?

参考目前已添加的腾讯翻译,大体上我们只负责接入官方 API,至于需要 API key,这个让用户自己去申请就好了。

但为了新用户方便使用,我们可以内置一个有免费额度限制的 API key,这个通常用的是我自己额外注册申请的一个账号下的 key。比如腾讯翻译每月有 500 万免费额度,我在程序内简单限制了一下每个新用户的使用额度。另外如果这个月 500 万免费额度用完了,也同样不能使用了,会报错提示。

具体你可以看一下腾讯翻译的代码。

啊,我理解错了,我以为是要内置一个apikey给所有人用呢。
那我稍后看一下,不过我这几天有事要做,这周应该上不了了。

@tisfeng
Copy link
Owner

tisfeng commented Dec 23, 2023

可以,那这个下周再上也行。

@tisfeng
Copy link
Owner

tisfeng commented Dec 30, 2023

周末准备发布一个新版本,这个如果还没做完,那就放到下一个版本吧。

@choykarl
Copy link
Collaborator Author

昨晚写了,代码还没提呢,中午提一下

# Conflicts:
#	Easydict/App/Easydict-Bridging-Header.h
@tisfeng tisfeng requested a review from Kyle-Ye December 30, 2023 08:45
@tisfeng
Copy link
Owner

tisfeng commented Dec 30, 2023

@choykarl 还有什么问题吗,如果没问题了,需要解决所有的 Resolve conversation,不然 PR 流程会卡住。

另外有新的 commit 后,需要重新 Re-request review

image image

@choykarl choykarl closed this Dec 30, 2023
@choykarl choykarl deleted the ali_translate branch January 28, 2024 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants