目录

介绍

要求

示例翻译函数

兴趣点


介绍

我们想轻松地将文本字符串翻译成另一种语言。从Google 翻译API返回的结果非常晦涩。它们采用JSON锯齿状数组的形式。当您必须翻译多个句子时,甚至更加困难。本技巧介绍了如何使用C#正确使用免费API。

要求

您必须添加对的System.Web.Extensions引用。然后添加以下using指令:

using System.Net.Http;
using System.Collections;
using System.Web.Script.Serialization;

示例翻译函数

在代码中添加以下函数:

public string TranslateText(string input){// Set the language from/to in the url (or pass it into this function)string url = String.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}","en", "es", Uri.EscapeUriString(input));HttpClient httpClient = new HttpClient();string result = httpClient.GetStringAsync(url).Result;// Get all json datavar jsonData = new JavaScriptSerializer().Deserialize<List<dynamic>>(result);// Extract just the first array element (This is the only data we are interested in)var translationItems = jsonData[0];// Translation Datastring translation = "";// Loop through the collection extracting the translated objectsforeach (object item in translationItems){// Convert the item array to IEnumerableIEnumerable translationLineObject = item as IEnumerable;// Convert the IEnumerable translationLineObject to a IEnumeratorIEnumerator translationLineString = translationLineObject.GetEnumerator();// Get first object in IEnumeratortranslationLineString.MoveNext();// Save its value (translated text)translation += string.Format(" {0}", Convert.ToString(translationLineString.Current));}// Remove first blank characterif (translation.Length > 1) { translation = translation.Substring(1); };// Return translationreturn translation;}

在以下行中设置from/to语言。在这种情况下,en(从语言)和es(到语言):

string url = String.Format
("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
"en", "es", Uri.EscapeUriString(input));

然后调用您的代码:

string translatedText = TranslateText(text);

兴趣点

使用免费的API,您每小时只能翻译大约100个单词。如果您滥用此权限,则Google API将返回429(请求过多)错误。

在C#中使用Google翻译API相关推荐

  1. Google 翻译API Demo

    接上篇 Google翻译API(B/S调用和C/S调用) 上篇里提到的接口调用方法是get方式,这样有个问题,每次请求翻译的内容不能超过url允许的长度.需要改成post方式才行,但是google没有 ...

  2. Google翻译API(B/S调用和C/S调用)

    浏览器调用: <html><head><script type="text/javascript" src="http://www.goog ...

  3. Google翻译API的使用

    今天学习了下Google翻译的API,有所收获,也有些问题.      总结了下,主要有两种调用方式:B/S方式(在网页中),C/S方式(在Java程序中). 第一种:B/S方式. Google Aj ...

  4. Google翻译API使用案例

    简单整理了下Google翻译的API使用方法.简单的说就是一个Ajax请求: 效果图: 源代码:都附上了注释还是比较好看懂的.将该代码复制成html即可运行. <html> <hea ...

  5. python google地图_请问该如何在python中使用google maps api?

    开通API功能及相应的权限 这个这部分主要是因为要和google的api需要自动去开通 ,首先默认情况下google是没有开放这个服务的,需要首先去告诉google 我是开发者,我要做一个项目,这个项 ...

  6. 微信小程序中调用百度翻译API(出现的错误54001 52003)

    我自己的54001: appid 和key出现错误, 先用的是百度提供的模板appid 及key 然后一直报错  之后用其它的appid 和key后能够正常运行 52003:这个需要自己申请一个app ...

  7. python 调用Google Translate API进行翻译

    调用Google 翻译API进行翻译 #!/usr/bin/python #coding: UTF-8 import sys reload(sys) sys.setdefaultencoding('u ...

  8. 微软翻译api的使用介绍和注意事项

    google翻译api已经收费了,而微软翻译api目前是免费的,支持几种不同的方式访问,如果感兴趣可以自己封装下协议处理.官方介绍: http://msdn.microsoft.com/en-us/l ...

  9. 【工具】Vscode翻译插件推荐(不用谷歌翻译api、支持短句英汉互译、支持查词、支持自动补全、不需要浏览器)

    需求: 1)偶尔需要查英文生词: 2)有时候想不起来中文对应的英文: 3)不想回到浏览器打开一堆网页: 4)谷歌翻译挂了. 偶尔需要的需求: 1)短句翻译. 因为谷歌翻译挂了,首先,排除最热门的翻译插 ...

最新文章

  1. java基础----Runtime类的使用(一)
  2. python3精要(64)-Python命名风格规范-google版
  3. Jenkins添加从节点相关配置
  4. LNMP一键安装脚本(第二版)
  5. jquery动态改变onclick属性导致失效的问题解决方法
  6. 如果__name__ =='__main__':在Python中怎么办?
  7. VUE:父子组件间传参、子组件传值给父组件、父组件传值给子组件
  8. 第 3-4 课:数据结构——队列详解 + 面试题
  9. 5-7激活层-BN层-FC层-损失层
  10. 外媒:柔宇科技搁置赴美上市计划 考虑在中国进行IPO
  11. 拿来就能用!用爬虫秒抢到孩子心仪的幼儿园 | CSDN 博文精选
  12. JavaScript函数—JavaScript闭包
  13. H.264视频质量评价算法(基于偏最小二乘法回归)
  14. fatal: 无法访问 ‘x.git/‘gnutls_handshake() failed: The TLS connection was non-properly terminated.解决办法
  15. Google 投资 Lyft 背后、AlphaGo Zero 人工智能威胁论?
  16. webService CXF框架
  17. centos改变文件拥有者_linux修改文件所有者和文件所在组
  18. 线程的终止的4种方式
  19. 513、Java Spring Cloud Alibaba -【Spring Cloud Alibaba Nacos】 2021.08.30
  20. android 测光模式,安卓手机里的专业模式究竟该怎么拍?

热门文章

  1. 游戏迷英雄联盟游戏高清壁纸!扮演你的英雄
  2. 设计资源类的网站有哪些?
  3. 小雪节气艺术字体设计PNG素材 | 希望可以温暖到你了
  4. uniapp对象怎么获取值_原理 | 怎么获取PMSM电机转子位置Offset值
  5. LeetCode-----替换空格
  6. C++管道通信类封装
  7. VisualStudio2019 DLL生成并使用教程(C++)最详细Demo教程
  8. Stack Overflow RToax
  9. Linux虚拟网络设备之tun/tap
  10. web前端基础(11html5和css)