C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译薛定谔の喵

使用C++开发一个在线翻译工具,这个想法在我大脑中过了好几遍了,所以就搜了下资料,得知网络上有很多翻译API,这里我选择我平时使用较多的有道翻译API进行在线翻译工具开发的练习。翻译API返回的结果常见的有两种:xml和json格式,本文选择使用json数据来实现Berlin版本的在线翻译工具。

开发环境:Ubuntu12.04+GCC4.7

一、有道翻译API

有道翻译API的数据接口如下:

版本:1.1,请求方式:get,编码方式:utf-8

主要功能:中英互译,同时获得有道翻译结果和有道词典结果(可能没有)

参数说明:

type-返回结果的类型,固定为data

doctype-返回结果的数据格式,xml或json或jsonp

version-版本,当前最新版本为1.1

q-要翻译的文本,不能超过200个字符,需要使用utf-8编码

errorCode:

0-正常

20-要翻译的文本过长

30-无法进行有效的翻译

40-不支持的语言类型

50-无效的key

二、Curl和JsonCpp的安装

2.1Curl的安装

1cd$CURL_HOME2mkdirbuild3cdbuild4cmake..5make2.2JsonCpp的安装

1sudoapt-getinstallsconsScons安装好之后就可以编译JsonCpp了,使用如下命令:

1sconsplatform=linux-gcc好了,JsonCpp已经成功安装了,为了后面程序编译链接过程中方便,我在JsonCpp路径下的libs文件夹中设置了一个软连接,如下:

1ln-slibjson_linux-gcc-4.7_libmt.alibjson_linux-gcc.a三、在线翻译工具

直接贴代码:

CMake文件如下:

1project(test)2cmake_minimum_required(VERSION2.6)34include_directories($ENV{JSONCPP_HOME}/include$ENV{CURL_HOME}/include)5link_directories($ENV{JSONCPP_HOME}/libs/$ENV{CURL_HOME}/build/lib)6add_definitions(-std=c++0x)7set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS}-Wall")89set(source10translate.cc)11add_executable(translate${source})12target_link_libraries(translatejson_linux-gcc)13target_link_libraries(translatecurl)程序执行效果如下:

=====================================================================

补充:

1char*curl_easy_escape(CURL*curl,char*url,intlength);在我们检测到我们传入的参数有非ASCII编码的时候,我们调用该函数对其进行转换:

1if((*argv[1]&0x80)!=0)2{3type=0;4char*encode_word=curl_easy_escape(curl,argv[1],0);5translate_url+=encode_word;6}else{7type=1;8translate_url+=argv[1];9}由于后面显示格式会有所不同,所以我加入一个type用来记住翻译类型,好了,修改后的代码如下:

THE END
1.LibrariesDefinition&MeaningMerriamHow to Use Em Dashes (—), En Dashes (–) , and Hyphens (-) Plural and Possessive Names: A Guide The Difference Between 'i.e.' and 'e.g.' Why is '-ed' sometimes pronounced at the end of a word? What's the difference between 'fascism' and 'socialism'? https://www.merriam-webster.com/dictionary/libraries
2.library是什么意思library怎么读中文意思用法library library是什么意思、library怎么读 读音:英[?la?br?ri] 美['la?br?ri] 初中高中雅思 library 基本解释 n. 图书馆,藏书楼;藏书;书屋,书斋;图书出租处 library 词性变化 名词复数形式:libraries 中文词源 library 图书馆 来自拉丁语liber,树皮,纸张,书,来自PIE*leub,剥皮,撕开,词源同leaf,https://danci.gjcha.com/library.html
3.ThreeEnglish(dictionary)areinthebag.(dictionary) are in the bag.【考点】可数名词及其单复数. 【答案】dictionaries 【解答】 【点评】 声明:本试题解析著作权属菁优网所有,未经书面同意,不得复制发布。 当前模式为游客模式,立即登录查看试卷全部内容及下载 发布:2024/12/7 21:0:2组卷:47引用:5难度:0.7 https://www.jyeoo.com/shiti/7b10149c-1151-4154-975a-892546c16eb6
4.Library是什麼字體?眾識社區識字體網黑体-简 细体 94.7% 商用须授权 XW Zar 94.7% 免费可商用 Tassafout Noufouss UNICODE 94.3% 商用须授权 Teddus Standard UNICODE 94.2% 商用须授权 常规体 94% 商用须授权 Helvetica 94% 商用须授权 Tamalout Noufouss UNICODE 93.9% 商用须授权 Sohne-Buch 93.9% 商用须授权 AkrutiOfficeLekhahttps://hant.m.likefont.com/community/112551495/
5.课后辅导,当然可以!以下是一些基于课后辅导主题的标题建议:Online平台以及应用程序不仅丰富了资料来源,还增加了互动功能。这些工具允许教师为每个学生设计专属练习。此外,各类视频教程、在线测验等都能灵活运用,为枯燥无味的新知识注入生动元素,提高吸引力。在这种交互式体验中,自主探索成为一种乐趣,有效促进思维发展. 培养自主学习能力 A key aspect of after-school tutoring http://m.metapostoffice.net/synews/84015.html
6.探索“library”发音与图书馆的重要性在我们的日常生活中,图书馆(library)是一个非常重要的场所。无论是学生、研究者,还是普通的读者,图书馆都为我们提供了一个获取知识、放松心情的空间。然而,今天我们要讨论的并不是图书馆的功能,而是“library”这个词的读音以及它在学习和生活中的重要性。 https://news.huochengrm.cn/cydz/5889.html
7.libraries是什么意思libraries的中文翻译例句用法中文翻译 n. 图书馆( library的名词复数 );图书室 词态变化 原级:library 中文词源 ☆ 14世纪晚期进入英语,直接源自盎格鲁法语的librarie,意为对书的收藏;最初源自古典拉丁语的librarium,意为放书的抽屉,纸,羊皮卷。 词组短语 vulkan run time libraries online libraries在线图书馆:指通过互联网提供电子书籍、https://danci.gei6.com/libraries__fsbehkp5.html
8.LibrariesviaOnlineBulletinBoard在线翻译英语读音海词词典,最权威的学习词典,为您提供Investigate Reader Service in University Libraries via Online Bulletin Board的在线翻译,Investigate Reader Service in University Libraries via Online Bulletin Board是什么意思,Investigate Reader Service in University Librahttp://dict.cn/Investigate%20Reader%20Service%20in%20University%20Libraries%20via%20Online%20Bulletin%20Board
9.2020年9月英语四级真题(全三套)英语四级作文翻译听力范文真题及Directions: For this part, you are allowed 30 minutes to write an essay on Online libraries. You can start your essay with the sentence "Online libraries are becoming increasingly popular." You should write at least 120 words but no more than 180 words. https://www.chinaxiaokang.com/jiaoyupindao/2020/0924/1048925_3.html
10.08词汇运用(译单词变词形选词填空等)(202165.There are many online libraries where you can read books for ___, such as the national Online libraries Library of China and Shanghai Library.(免费的)根据句意用所给单词的适当形式填空66.If you buy a blind box, you’ll get a box with a(n)___ item inside.(known)67.Sandy listenedhttps://mip.21cnjy.com/P/18483416.html
11.onPublicLibraries剑桥雅思12听力原文翻译及答案解析出国留学S:And if everyone did that,libraries would be obsolete. T:Yes. S:But the digitalized books that are available online for free are mostly out of copyright,aren't they? And copyright in this country lasts for 70 years after the author dies. https://www.eol.cn/waiyu/news/20230624152551.html
12.buildingonline的翻译是:在线构建中文翻译英文意思,翻译英语a哦了,你猜猜 正在翻译,请等待[translate] adivided up 划分[translate] aIfyou close your eye syou can see them Ifyou关闭您的眼睛syou能看他们[translate] aFigure 10 shows the change in total public revenue—local, state and federal—for libraries from Fiscal 2008 through Fiscal 2010 图10http://eyu.zaixian-fanyi.com/fan_yi_10413140
13.DeepL翻译:全世界最准确的翻译翻译文本 源语言文本 键入翻译。 使用我们的文档翻译器拖放翻译PDF、Word(.docx)和PowerPoint(.pptx)文件。 请点击麦克风来翻译语音。 翻译结果 词典 词典功能暂不支持该语言组合。 解锁DeepL全部功能 –免费试用DeepL Pro 免费试用Pro30天 你正在使用DeepL免费版 https://www.deepl.com/translator
14.翻译Because Django is intended to be a full-stack Web framework that provides all the pieces necessary for Web developers to be productive, many times its *more convenient* to use Djangos template system than other Python template libraries, but its not a strict requirement in any sense. As youhttp://djangobook.py3k.cn/chapter04/translate/
15.Unity3d英文翻译?Editing Value Properties编辑值属性翻译:第七把剑 ?Preset Libraries预设库翻译:U_鹰 ?Assigning References指定引用翻译:第七把剑 ?Multi-Object Editing多重对象编辑翻译:colin3dmax ?Inspector Options检视面板选项翻译:第七把剑 ?Using the Scene View使用场景视图翻译:第七把剑 ?Scene View Navigation场景视图导航https://m.360docs.net/doc/f714578481.html
16.英语六级翻译原文及答案(通用61篇)英语六级翻译原文及答案(通用61篇) 英语六级翻译对于很多人来说都是难点,为了帮助大家备考英语六级翻译,小编整理了一些英语六级翻译原文及答案,希望能帮到大家! 历年英语六级翻译原文及答案(40篇): 2023年6月英语六级翻译 第一套翻译 原文: 近年来,中国城市加快发展,城市人居住环境得到显著改善。许多城市努力探索中国https://www.yjbys.com/edu/cet6/170259.html
17.译写他建立和改革了学校与图书馆,编写和翻译戏剧,组织戏剧表演,并在1893年出版了他的最后一本书A Short Autobiography(一本简短的自传)。 wdl.org wdl.org He founded and reformed schools and libraries,wrote and translated plays,organized theatrical performances, and published http://cn.linguee.com/%E4%B8%AD%E6%96%87-%E8%8B%B1%E8%AF%AD/%E7%BF%BB%E8%AD%AF/%E8%AF%91%E5%86%99.html
18.翻译'互联网络'–字典英文在上下文、翻译记忆库中将“互联网络"翻译成 英文 互联网络其实像一个收藏丰富的图书馆一样,满是悦目缤纷的文件。 In effect, the Internet is a vast collection of libraries with visually appealing documents. jw2019 事实上,互联网络上大部分的资讯交通主要是传递电子邮件,而很多人上网都只是为了使用这个https://glosbe.com/zh/en/%E4%BA%92%E8%81%94%E7%BD%91%E7%BB%9C
19.《苏菲诗歌&翻译》(汉英)纸质世界诗刊各栏目投稿细则(附简介)《苏菲诗歌&翻译》(英汉)纸质世界诗刊 简介 《苏菲诗歌&翻译》(英汉)纸质世界诗刊由中国诗人、翻译家苏菲(Sophy Chen)陈丽华2018年在苏菲国际翻译出版社旗下于中国香港发起创立,是一家全球发行, 以英汉互译为主导,推动中西方多语种诗歌的互译、互动、研究、传播与交流为一体的世界性诗歌刊物。 https://www.sophypoetry.net/2020/09/5620.html
20.goodterm什么意思英语goodterm的翻译音标读音On the other hand, I get a good term list in the end. 一方面可以通过阅读的过程学到别人好的翻译处理方式,另一方面则可以积累到不错的术语表。 www.67786.com All available sources like libraries, online databases, specialized journals or government and scholarly publications should be utilized in https://dict.youdao.com/w/good%20term/
21.论文翻译知识图谱论文中英对照翻译Conclusion: KnowLife is a large knowledge base for health and life sciences, automatically constructed from different Web sources. As a unique feature, KnowLife is harvested from different text genres such as scientific publications, health portals, and online communities. Thus, it has the potentialhttps://blog.csdn.net/qq_35340189/article/details/104481838
22.EBOOK在剑桥英语词典中的解释及翻译This act extends that royalty to cover some ebooks and audio books lent by libraries electronically. 来自 Wikipedia It is also available as anebookand as a subscription on the guide's website. 来自 Wikipedia Starting in 2013 the series will be available as ebooks. https://dictionary.cambridge.org/zhs/%E8%AF%8D%E5%85%B8/%E8%8B%B1%E8%AF%AD/ebook