欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现谷歌翻译API

程序员文章站 2022-10-01 12:38:10
由于谷歌翻译官方API是付费版本,本着免费和开源的精神。分享一下用 Net Core 实现谷歌翻译API的代码。 项目引用的Nuget 包: ChakraCore.NET Newtonsoft.Json JavaScriptEngineSwitcher.ChakraCore.Native.win-x ......

  由于谷歌翻译官方api是付费版本,本着免费和开源的精神。分享一下用 net core 实现谷歌翻译api的代码。

 

项目引用的nuget 包:

chakracore.net

newtonsoft.json

javascriptengineswitcher.chakracore.native.win-x86(特别说明一下"win-x86",代表程序运行环境。)

翻译地址(国内可以访问)

参数说明 

text:待翻译文本

sl:需要翻译的语言(中文:zh-cn,英文:en)

tl: 翻译结果的语言(中文:zh-cn,英文:en)

C#实现谷歌翻译API

 

 

 

C#实现谷歌翻译API
 1 public class httphelper
 2     {
 3         /// <summary>
 4         /// 请求
 5         /// </summary>
 6         /// <param name="url">地址</param>
 7         /// <param name="args">参数</param>
 8         /// <returns></returns>
 9         public static string getrequest(string url, cookiecontainer cookiecontainer, dictionary<string, dynamic> args)
10         {
11             try
12             {
13                 if (args.count > 0)
14                 {
15                     var argstr = string.empty;
16 
17                     foreach (var item in args)
18                     {
19                         if (item.key == null || item.value == null)
20                         {
21                             continue;
22                         }
23                         argstr = $"{argstr}{item.key}={item.value}&";
24                     }
25 
26                     url = $"{url}?{argstr.trimend('&')}";
27                 }
28 
29                 httpwebrequest request = (httpwebrequest)webrequest.create(url);
30                 request.method = "get";
31                 request.timeout = 100000;
32                 request.cookiecontainer = cookiecontainer;
33                 request.useragent = getuseragent();
34                 request.headers.add("x-requested-with:xmlhttprequest");
35                 request.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
36                 using (var webresponse = (httpwebresponse)request.getresponse())
37                 {
38                     using (var reader = new streamreader(webresponse.getresponsestream(), encoding.utf8))
39                     {
40 
41                         return reader.readtoend();
42                     }
43                 }
44             }
45             catch (exception)
46             {
47                 return string.empty;
48             }
49         }
50         
51         /// <summary>
52         /// 用户代理
53         /// </summary>
54         /// <returns></returns>
55         private static string getuseragent()
56         {
57             var useragents = new list<string>
58             {
59                 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.1 (khtml, like gecko) chrome/22.0.1207.1 safari/537.1",
60                 "mozilla/5.0 (x11; cros i686 2268.111.0) applewebkit/536.11 (khtml, like gecko) chrome/20.0.1132.57 safari/536.11",
61                 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/536.6 (khtml, like gecko) chrome/20.0.1092.0 safari/536.6",
62                 "mozilla/5.0 (windows nt 6.2) applewebkit/536.6 (khtml, like gecko) chrome/20.0.1090.0 safari/536.6",
63                 "mozilla/5.0 (windows nt 6.2; wow64) applewebkit/537.1 (khtml, like gecko) chrome/19.77.34.5 safari/537.1",
64                 "mozilla/5.0 (x11; linux x86_64) applewebkit/536.5 (khtml, like gecko) chrome/19.0.1084.9 safari/536.5",
65                 "mozilla/5.0 (windows nt 6.0) applewebkit/536.5 (khtml, like gecko) chrome/19.0.1084.36 safari/536.5",
66                 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1063.0 safari/536.3",
67                 "mozilla/5.0 (windows nt 5.1) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1063.0 safari/536.3",
68                 "mozilla/5.0 (macintosh; intel mac os x 10_8_0) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1063.0 safari/536.3",
69                 "mozilla/5.0 (windows nt 6.2) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1062.0 safari/536.3",
70                 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1062.0 safari/536.3",
71                 "mozilla/5.0 (windows nt 6.2) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1061.1 safari/536.3",
72                 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1061.1 safari/536.3",
73                 "mozilla/5.0 (windows nt 6.1) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1061.1 safari/536.3",
74                 "mozilla/5.0 (windows nt 6.2) applewebkit/536.3 (khtml, like gecko) chrome/19.0.1061.0 safari/536.3",
75                 "mozilla/5.0 (x11; linux x86_64) applewebkit/535.24 (khtml, like gecko) chrome/19.0.1055.1 safari/535.24",
76                 "mozilla/5.0 (windows nt 6.2; wow64) applewebkit/535.24 (khtml, like gecko) chrome/19.0.1055.1 safari/535.24",
77                 "mozilla/5.0 (macintosh; u; mac os x mach-o; en-us; rv:2.0a) gecko/20040614 firefox/3.0.0 ",
78                 "mozilla/5.0 (macintosh; u; ppc mac os x 10.5; en-us; rv:1.9.0.3) gecko/2008092414 firefox/3.0.3",
79                 "mozilla/5.0 (macintosh; u; intel mac os x 10.5; en-us; rv:1.9.1) gecko/20090624 firefox/3.5",
80                 "mozilla/5.0 (macintosh; u; intel mac os x 10.6; en-us; rv:1.9.2.14) gecko/20110218 alexatoolbar/alxf-2.0 firefox/3.6.14",
81                 "mozilla/5.0 (macintosh; u; ppc mac os x 10.5; en-us; rv:1.9.2.15) gecko/20110303 firefox/3.6.15",
82                 "mozilla/5.0 (macintosh; intel mac os x 10.6; rv:2.0.1) gecko/20100101 firefox/4.0.1",
83                 "opera/9.80 (windows nt 6.1; u; en) presto/2.8.131 version/11.11",
84                 "opera/9.80 (android 2.3.4; linux; opera mobi/adr-1107051709; u; zh-cn) presto/2.8.149 version/11.10",
85                 "mozilla/5.0 (windows; u; windows nt 5.1; en-us) applewebkit/531.21.8 (khtml, like gecko) version/4.0.4 safari/531.21.10",
86                 "mozilla/5.0 (windows; u; windows nt 5.2; en-us) applewebkit/533.17.8 (khtml, like gecko) version/5.0.1 safari/533.17.8",
87                 "mozilla/5.0 (windows; u; windows nt 6.1; en-us) applewebkit/533.19.4 (khtml, like gecko) version/5.0.2 safari/533.18.5",
88                 "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0",
89                 "mozilla/4.0 (compatible; msie 8.0; windows nt 6.0; trident/4.0)",
90                 "mozilla/4.0 (compatible; msie 7.0; windows nt 6.0)",
91                 "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"
92             };
93 
94             return useragents.orderby(x => guid.newguid()).first();
95         }
96     }
httphelper

 

C#实现谷歌翻译API
  1 /// <summary>
  2     /// 翻译 助手
  3     /// </summary>
  4     public static class translationhelper
  5     {
  6         /// <summary>
  7         /// chakra 上下文
  8         /// </summary>
  9         private static readonly chakracontext _chakracontext;
 10 
 11         /// <summary>
 12         /// cookie
 13         /// </summary>
 14         private static readonly cookiecontainer _cookiecontainer;
 15 
 16         /// <summary>
 17         /// 请求地址
 18         /// </summary>
 19         private static readonly string _baseurl;
 20 
 21         /// <summary>
 22         /// 静态
 23         /// </summary>
 24         static translationhelper()
 25         {
 26             var runtime = chakraruntime.create();
 27 
 28             _baseurl = "http://translate.google.cn/translate_a/single";
 29             _cookiecontainer = new cookiecontainer();
 30             _chakracontext = runtime.createcontext(true);
 31 
 32             var basepath = platformservices.default.application.applicationbasepath;
 33             var jsfiletext = file.readalltext($@"{basepath}\gettk.js");
 34 
 35             _chakracontext.runscript(jsfiletext); //运行脚本
 36         }
 37 
 38         /// <summary>
 39         /// 获取翻译结果(需要翻译的文字默认使用中文)
 40         /// </summary>
 41         /// <param name="tolang">语言</param>
 42         /// <param name="originaltext">待翻译的文本</param>
 43         /// <returns></returns>
 44         public static string gettranslation(this string tolang, string originaltext)
 45         {
 46             if (string.isnullorempty(tolang))
 47             {
 48                 return tolang;
 49             }
 50             if (string.isnullorempty(originaltext))
 51             {
 52                 return originaltext;
 53             }
 54 
 55             return gettranslation("zh-cn", tolang, originaltext);
 56 
 57         }
 58 
 59         /// <summary>
 60         /// 获取翻译结果
 61         /// </summary>
 62         /// <param name="fromlang">需要翻译的语言</param>
 63         /// <param name="tolang">翻译结果的语言</param>
 64         /// <param name="originaltext">待翻译文本</param>
 65         /// <returns></returns>
 66         public static string gettranslation(this string fromlang, string tolang, string originaltext)
 67         {
 68             var args = new dictionary<string, dynamic>
 69             {
 70                 { "client", "t" },
 71                 { "sl", fromlang },
 72                 { "tl", tolang },
 73                 { "dt", "t" },
 74                 { "tk", gettk(originaltext) },
 75                 { "text", httputility.urlencode(originaltext) }
 76             };
 77 
 78             var result = httphelper.getrequest(_baseurl, _cookiecontainer, args);
 79 
 80             return result.formattedjson();
 81         }
 82 
 83         /// <summary>
 84         /// 获取tk
 85         /// </summary>
 86         /// <param name="originaltext"></param>
 87         /// <returns></returns>
 88         private static string gettk(string originaltext)
 89         {
 90             _chakracontext.globalobject.writeproperty("originaltext", originaltext);
 91             return _chakracontext.runscript("gettk(originaltext)");
 92         }
 93 
 94         /// <summary>
 95         /// 格式化json
 96         /// </summary>
 97         /// <param name="jsonstr">json</param>
 98         /// <returns></returns>
 99         private static string formattedjson(this string jsonstr)
100         {
101             if (string.isnullorempty(jsonstr))
102             {
103                 return string.empty;
104             }
105 
106             var array = jsonconvert.deserializeobject<jarray>(jsonstr);
107 
108             var result = array[0][0][0].tostring();
109 
110             return result;
111         }
112 
113     }
translationhelper

 

 

 

 参考:c#实现谷歌翻译api