-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"printWidth": 140 | ||
"printWidth": 180 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using STranslate.Model; | ||
using STranslate.Util; | ||
|
||
namespace STranslate.ViewModels | ||
{ | ||
/// <summary> | ||
/// 不同服务处理类 | ||
/// TODO: 新接口需要适配 | ||
/// </summary> | ||
public class ServiceHandler | ||
{ | ||
/// <summary> | ||
/// API | ||
/// </summary> | ||
/// <param name="service"></param> | ||
/// <param name="content"></param> | ||
/// <param name="source"></param> | ||
/// <param name="target"></param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public static async Task<string> ApiHandler(ITranslator service, string content, string source, string target, CancellationToken token) | ||
{ | ||
var response = | ||
(Task<object>) | ||
await service.TranslateAsync( | ||
new RequestApi() | ||
{ | ||
Text = content, | ||
SourceLang = source, | ||
TargetLang = target | ||
}, | ||
token | ||
); | ||
var ret = (response.Result as ResponseApi)!.Data?.ToString() ?? ""; | ||
|
||
return ret; | ||
} | ||
|
||
/// <summary> | ||
/// Baidu | ||
/// </summary> | ||
/// <param name="service"></param> | ||
/// <param name="content"></param> | ||
/// <param name="source"></param> | ||
/// <param name="target"></param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public static async Task<string> BaiduHandler(ITranslator service, string content, string source, string target, CancellationToken token) | ||
{ | ||
string salt = new Random().Next(100000).ToString(); | ||
string sign = StringUtil.EncryptString(service.AppID + content + salt + service.AppKey); | ||
var response = | ||
(Task<object>) | ||
await service.TranslateAsync( | ||
new RequestBaidu() | ||
{ | ||
Text = content, | ||
From = source, | ||
TO = target, | ||
AppId = service.AppID, | ||
Salt = salt, | ||
Sign = sign | ||
}, | ||
token | ||
); | ||
var transResults = (response.Result as ResponseBaidu)?.TransResult ?? []; | ||
var ret = | ||
transResults.Length == 0 | ||
? string.Empty | ||
: string.Join(Environment.NewLine, transResults.Where(trans => !string.IsNullOrEmpty(trans.Dst)).Select(trans => trans.Dst)); | ||
return ret; | ||
} | ||
|
||
/// <summary> | ||
/// Bing | ||
/// </summary> | ||
/// <param name="service"></param> | ||
/// <param name="content"></param> | ||
/// <param name="source"></param> | ||
/// <param name="target"></param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public static async Task<string> BingHandler(ITranslator service, string content, string source, string target, CancellationToken token) | ||
{ | ||
var req = new RequestBing | ||
{ | ||
From = source, | ||
To = target, | ||
Req = [new TextData { Text = content }], | ||
}; | ||
var response = (Task<object>)await service.TranslateAsync(req, token); | ||
var ret = (response.Result as ResponseBing[])!.FirstOrDefault()?.Translations?.FirstOrDefault()?.Text; | ||
|
||
return ret ?? ""; | ||
} | ||
} | ||
} |