feat: add baidu fanyi

master
ZGGSONG 2 years ago
parent 98605e2092
commit 18ff0a71b6

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

@ -0,0 +1 @@
<?xml version="1.0" ?><svg enable-background="new 0 0 24 24" id="Layer_1" version="1.1" viewBox="0 0 24 24" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><path d="M22,4h-9.7741699l-1.2526855-3.6616211C10.9038086,0.1357422,10.7138672,0,10.5,0H2 C0.8969727,0,0,0.8969727,0,2v16c0,1.1030273,0.8969727,2,2,2h9.6533203l1.378418,3.6757813 C13.1049805,23.8706055,13.2915039,24,13.5,24H22c1.1030273,0,2-0.8969727,2-2V6C24,4.8969727,23.1030273,4,22,4z" fill="#303C42"/><path d="M1,18V2c0-0.5512695,0.4487305-1,1-1h8.1425781l6.1577148,18H2C1.4487305,19,1,18.5512695,1,18z" fill="#00ACC1"/><path d="M19.4003296,11c-0.2365112,0.7894287-0.9160767,2.0952148-1.8649902,3.2930298 c-0.5110474-0.5839233-0.927124-1.1244507-1.0719604-1.4810181c-0.1040039-0.2563477-0.3964844-0.3789063-0.6513672-0.2753906 c-0.2558594,0.1040039-0.3793945,0.3955078-0.2753906,0.6513672c0.1993408,0.4907837,0.7245483,1.1732788,1.3353271,1.866333 c-0.2094116,0.2197266-0.4260864,0.4249878-0.6525879,0.6191406L14.6205444,11H19.4003296z" fill="#E6E6E6"/><polygon fill="#00ACC1" points="12.7216797,20 15.8980713,20 13.6746216,22.5411377 "/><polygon opacity="0.2" points="12.7216797,20 15.8980713,20 13.6746216,22.5411377 "/><path d="M23,22c0,0.5512695-0.4487305,1-1,1h-7.3980713l2.7745361-3.1708984l-0.0071411-0.0062256 c0.0110474-0.0125732,0.0275879-0.0185547,0.0374146-0.0323486c0.09375-0.1308594,0.1186523-0.2998047,0.0664063-0.4521484 l-0.9087524-2.6563721c0.3388062-0.2658691,0.6650391-0.5657959,0.9793701-0.8942261 c0.9603271,1.0123901,1.9286499,1.9071045,2.1188354,2.0808716C19.7583008,17.956543,19.8793945,18,20,18 c0.1352539,0,0.2700195-0.0546875,0.3686523-0.1621094c0.1865234-0.2041016,0.1728516-0.5200195-0.0307617-0.706543 c-0.4680786-0.4286499-1.3305054-1.2530518-2.1286011-2.0968018C19.3474121,13.6314087,20.1936035,12.0231323,20.4256592,11H21.5 c0.2763672,0,0.5-0.2236328,0.5-0.5S21.7763672,10,21.5,10H18V9.5C18,9.2236328,17.7763672,9,17.5,9S17,9.2236328,17,9.5V10h-2.5 c-0.0738525,0-0.1425781,0.0179443-0.2056274,0.0466309L12.5679321,5H22c0.5512695,0,1,0.4487305,1,1V22z" fill="#E6E6E6"/><linearGradient gradientUnits="userSpaceOnUse" id="SVGID_1_" x1="8.7845306" x2="22.7072906" y1="8.7834015" y2="22.7061615"><stop offset="0" style="stop-color:#000000;stop-opacity:0.1"/><stop offset="1" style="stop-color:#000000;stop-opacity:0"/></linearGradient><path d="M15.8120117,12.5366211c0.2548828-0.1035156,0.5473633,0.019043,0.6513672,0.2753906 c0.1448364,0.3565674,0.5609131,0.8970947,1.0719604,1.4810181C18.4842529,13.0952148,19.1638184,11.7894287,19.4003296,11 h-4.7797852l1.5988159,4.6734619c0.2265015-0.1941528,0.4431763-0.3994141,0.6525879-0.6191406 c-0.6107788-0.6930542-1.1359863-1.3755493-1.3353271-1.866333C15.4326172,12.9321289,15.5561523,12.640625,15.8120117,12.5366211z M17,9.5c0-0.0200806,0.0090942-0.0371094,0.0114136-0.0565796L12.5679321,5l1.7264404,5.0466309 C14.3574219,10.0179443,14.4261475,10,14.5,10H17V9.5z M19.9229736,12.3550415 c-0.4046021,0.8446045-1.0020142,1.802124-1.7136841,2.6795044c0.7980957,0.84375,1.6605225,1.6681519,2.1286011,2.0968018 c0.2036133,0.1865234,0.2172852,0.5024414,0.0307617,0.706543C20.2700195,17.9453125,20.1352539,18,20,18 c-0.1206055,0-0.2416992-0.043457-0.3374023-0.1313477c-0.1901855-0.1737671-1.1585083-1.0684814-2.1188354-2.0808716 c-0.3143311,0.3284302-0.640564,0.6283569-0.9793701,0.8942261l0.9087524,2.6563721 c0.0522461,0.1523438,0.0273438,0.3212891-0.0664063,0.4521484c-0.0098267,0.0137939-0.0263672,0.0197754-0.0374146,0.0323486 l0.0071411,0.0062256L14.6019287,23H22c0.5512695,0,1-0.4487305,1-1v-6.5679932L19.9229736,12.3550415z" fill="url(#SVGID_1_)"/><path d="M11.5,10h-4C7.2236328,10,7,10.2236328,7,10.5S7.2236328,11,7.5,11h3.4643555 C10.7211914,12.6938477,9.2602539,14,7.5,14C5.5703125,14,4,12.4296875,4,10.5S5.5703125,7,7.5,7 c0.8339844,0,1.6420898,0.2983398,2.2744141,0.8398438C9.984375,8.019043,10.2993164,7.996582,10.4794922,7.7851563 c0.1796875-0.2099609,0.1552734-0.5253906-0.0546875-0.7050781C9.6113281,6.3837891,8.5722656,6,7.5,6 C5.0185547,6,3,8.0185547,3,10.5S5.0185547,15,7.5,15s4.5-2.0185547,4.5-4.5C12,10.2236328,11.7763672,10,11.5,10z" fill="#303C42"/><linearGradient gradientUnits="userSpaceOnUse" id="SVGID_2_" x1="-1.8567469" x2="25.8567467" y1="5.5384927" y2="18.4615078"><stop offset="0" style="stop-color:#FFFFFF;stop-opacity:0.2"/><stop offset="1" style="stop-color:#FFFFFF;stop-opacity:0"/></linearGradient><path d="M22,4h-9.7741699l-1.2526855-3.6616211C10.9038086,0.1357422,10.7138672,0,10.5,0H2 C0.8969727,0,0,0.8969727,0,2v16c0,1.1030273,0.8969727,2,2,2h9.6533203l1.378418,3.6757813 C13.1049805,23.8706055,13.2915039,24,13.5,24H22c1.1030273,0,2-0.8969727,2-2V6C24,4.8969727,23.1030273,4,22,4z" fill="url(#SVGID_2_)"/></g><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/><g/></svg>

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

@ -15,7 +15,8 @@
Topmost="True" Topmost="True"
WindowStyle="None" WindowStyle="None"
Height="450" Height="450"
Width="400"> Width="400"
Icon="/Images/translate.ico">
<Window.DataContext> <Window.DataContext>
<vm:MainVM/> <vm:MainVM/>
</Window.DataContext> </Window.DataContext>

@ -0,0 +1,37 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STranslate.Model
{
public class BaiduModel
{
public string Text { get; set; }
public string From { get; set; }
public string TO { get; set; }
public string AppId { get; set; }
public string Salt { get; set; }
public string Sign { get; set; }
}
public class BaiduResp
{
[JsonProperty("from")]
public string From { get; set; }
[JsonProperty("to")]
public string To { get; set; }
[JsonProperty("trans_result")]
public TransResult[] TransResult { get; set; }
}
public class TransResult
{
[JsonProperty("src")]
public string Src { get; set; }
[JsonProperty("dst")]
public string Dst { get; set; }
}
}

@ -8,6 +8,7 @@ namespace STranslate.Model
{ {
public enum LanguageEnum public enum LanguageEnum
{ {
auto, //自动
DE, //德语 DE, //德语
EN, //英语 EN, //英语
ES, //西班牙语 ES, //西班牙语

@ -0,0 +1,113 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace STranslate.Properties {
using System;
/// <summary>
/// 一个强类型的资源类,用于查找本地化的字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("STranslate.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] translate {
get {
object obj = ResourceManager.GetObject("translate", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] translate_x128 {
get {
object obj = ResourceManager.GetObject("translate_x128", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] translate_x256 {
get {
object obj = ResourceManager.GetObject("translate_x256", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] translate_x64 {
get {
object obj = ResourceManager.GetObject("translate_x64", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] translate1 {
get {
object obj = ResourceManager.GetObject("translate1", resourceCulture);
return ((byte[])(obj));
}
}
}
}

@ -0,0 +1,136 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="translate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\translate.svg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="translate1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\translate.ico;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="translate_x128" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\translate_x128.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="translate_x256" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\translate_x256.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="translate_x64" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\translate_x64.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

@ -4,10 +4,16 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework> <TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<ApplicationIcon>Images\translate.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove=".gitignore" /> <None Remove=".gitignore" />
<None Remove="Images\translate.ico" />
<None Remove="Images\translate.svg" />
<None Remove="Images\translate_x128.png" />
<None Remove="Images\translate_x256.png" />
<None Remove="Images\translate_x64.png" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -16,4 +22,37 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="Images\translate.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\translate.svg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\translate_x128.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\translate_x256.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\translate_x64.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project> </Project>

@ -31,5 +31,42 @@ namespace STranslate.Utils
var resp = JsonConvert.DeserializeObject<DeeplResp>(responseString); var resp = JsonConvert.DeserializeObject<DeeplResp>(responseString);
return resp; return resp;
} }
/// <summary>
/// 异步Post请求
/// </summary>
/// <param name="url"></param>
/// <param name="req"></param>
/// <returns></returns>
public static async Task<string> PostAsync(string url, string req)
{
using (var client = new HttpClient())
{
var content = new StringContent(req, Encoding.UTF8, "application/json");
var respContent = await client.PostAsync(url, content);
string respStr = await respContent.Content.ReadAsStringAsync();
;
return respStr;
}
}
/// <summary>
/// 异步Get请求
/// </summary>
/// <param name="urlpath"></param>
/// <returns></returns>
public static async Task<string> GetAsync(string urlpath)
{
using (var client = new HttpClient())
{
var respContent = await client.GetAsync(urlpath);
string respStr = await respContent.Content.ReadAsStringAsync();
return respStr;
}
}
} }
} }

@ -1,13 +1,18 @@
using STranslate.Model; using Newtonsoft.Json;
using STranslate.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
namespace STranslate.Utils namespace STranslate.Utils
{ {
public static class TranslateUtil public class TranslateUtil
{ {
private static readonly string _url = "http://172.17.209.47:8080/translate"; private static readonly string _url = "http://172.17.209.47:8080/translate";
public static string Translate(string input, LanguageEnum source, LanguageEnum target) public static string Translate(string input, LanguageEnum source, LanguageEnum target)
@ -25,5 +30,77 @@ namespace STranslate.Utils
} }
return string.Empty; return string.Empty;
} }
public static async Task<string> TranslateDeepLAsync(string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto)
{
var req = new DeeplReq()
{
Text = text,
SourceLang = source.ToString(),
TargetLang = target.ToString(),
};
var reqStr = JsonConvert.SerializeObject(req);
var respStr = await HttpUtil.PostAsync(_url, reqStr);
var resp = JsonConvert.DeserializeObject<DeeplResp>(respStr);
if (resp.Code == 200)
{
return resp.Data;
}
return string.Empty;
}
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.auto)
{
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string sign = EncryptString(appID + text + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(text);
url += "&from=" + source.ToString().ToLower();
url += "&to=" + target.ToString().ToLower();
url += "&appid=" + appID;
url += "&salt=" + salt;
url += "&sign=" + sign;
#if false
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
#endif
var retString = await HttpUtil.GetAsync(url);
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
if (resp.From != null)
{
return resp.TransResult[0]?.Dst;
}
return string.Empty;
}
// 计算MD5值
public static string EncryptString(string str)
{
MD5 md5 = MD5.Create();
// 将字符串转换成字节数组
byte[] byteOld = Encoding.UTF8.GetBytes(str);
// 调用加密方法
byte[] byteNew = md5.ComputeHash(byteOld);
// 将加密结果转换为字符串
StringBuilder sb = new StringBuilder();
foreach (byte b in byteNew)
{
// 将字节转换成16进制表示的字符串
sb.Append(b.ToString("x2"));
}
// 返回加密的字符串
return sb.ToString();
}
} }
} }

@ -11,17 +11,34 @@ namespace STranslate.ViewModel
{ {
public class MainVM : BaseVM public class MainVM : BaseVM
{ {
private string Text;
public MainVM() public MainVM()
{ {
TranslateCmd = new RelayCommand((_) => TranslateCmd = new RelayCommand((_) =>
{ {
return string.IsNullOrEmpty(InputTxt) ? false : true; return string.IsNullOrEmpty(InputTxt) ? false : true;
}, (_) => }, async (_) =>
{ {
OutputTxt = TranslateUtil.Translate(InputTxt, LanguageEnum.ZH, LanguageEnum.EN); Text = InputTxt;
//清空输入框 //清空输入框
InputTxt = ""; InputTxt = "";
OutputTxt = "翻译中...";
//获取结果
//var translateResp = await TranslateUtil.TranslateDeepLAsync(InputTxt, LanguageEnum.EN, LanguageEnum.auto);
var appId = "";
var secretKey = "";
var translateResp = await TranslateUtil.TranslateBaiduAsync(appId, secretKey, Text, LanguageEnum.EN, LanguageEnum.auto);
if (translateResp == string.Empty)
{
OutputTxt = "翻译出错,请稍候再试...";
return;
}
OutputTxt = translateResp;
}); });
} }