今天遇到一个小需求,就是将数据库中的某个中文字段翻译成英文,总共有六百多条,直接只用数据库update语句和手动翻译效率很慢。我想这如果可以调用有道翻译API接口将翻译的语句结合原中文字段拼接成update语句,最后将update语句做成update脚本即可一次性全部修改数据库中某个字段的翻译工作。可能不明白什么意思。原来同事让我用以下语句对数据库中几百条记录进行逐条翻译,那么我将原语句放入有道翻译翻译一遍然后结果放到下面语句。这样真的很抓狂。
update table_s set name = "hello" where name = "你好";
于是,想着用程序调用有道翻译API接口对数据库中待翻译字段全部翻译,将翻译结果拼接成以上语句,最终程序执行一遍的结果如下,那么我将一下一句在数据库中运行一遍即可完成同事交代的任务。
update table_s set name = "hello" where name = "你好";
update table_s set name = "hello1" where name = "你好1";
update table_s set name = "hello2" where name = "你好2";
update table_s set name = "hello3" where name = "你好3";
update table_s set name = "hello" where name = "你好";
上面是需求,现在说说怎么完成吧,调研有道翻译API接口,地址如下:http://fanyi.youdao.com/openapi?path=data-mode。
你需要注册,按照其指南完成相关操作,并在以下菜单处获得demo代码。
本文的代码已经上传至github,有兴趣的可以去查看,代码中有相关注释,相信你一定能够看的懂!
github地址:https://github.com/fishingfly/youdaoAPI。说明一下,我使用eclipse见的maven工程。