java种语言包在线翻译_Java 实现在线翻译功能 调用微软Bing API

下面是利用java程序实现翻译功能,调用微软Bing API

注意:代码中的keyId 需要自己申请。。。

(2)去申请key,进入http://www.bing.com/developers/createapp.aspx,填写相关的你的应用信息就行了。就会有下面的图片中显示的key,中的Application ID就是key

package com.test;

import com.memetix.mst.language.Language;

import com.memetix.mst.translate.Translate;

public class Test {

public static void main(String[] args) {

/*

* 大概是针对C#和php的,没有仔细研究过(可以访问下方的http://msdn.microsoft.com/en-us/library/hh454950.aspx 来学习一下)

* Set your Windows Azure Marketplace client info - See http://msdn.microsoft.com/en-us/library/hh454950.aspx

Translate.setClientId( Enter your Windows Azure Client Id here );

Translate.setClientSecret( Enter your Windows Azure Client Secret here );

*/

//在Java程序内翻译

Translate.setKey( Enter your API Key here );

String translatedText;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大多源码来自互联网,本人只做部分正和 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import com.vince.*; /** * 将本地文件以哪编码输出 * @param inputfile 输入文件的路径 * @param outfile 输出文件的路径 * @param code 输出文件的编码 * @throws IOException */ public class Charchange{ public static void main(String[] args) throws IOException { String inputfile,outputfile,code; inputfile = "D:\\迅雷\\work\\fen\\2-temp-test.txt";//要转码的文件 outputfile = "D:\\迅雷\\work\\1.txt";//输出的文件 code = "utf-8"; System.out.println("转码开始"); convert(inputfile,outputfile,code); System.out.println("转码完成"); } public static void convert(String inputfile,String outfile,String code) throws IOException { StringBuffer sb = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); //得到当前文件的编码 String ch=getCharset(inputfile); InputStreamReader isr=null; OutputStreamWriter osw =null; //根据当前文件编码进行解码 if(ch.equals("UTF8")){ isr= new InputStreamReader(new FileInputStream(inputfile), "UTF-8"); }else if(ch.equals("Unicode")){ isr= new InputStreamReader(new FileInputStream(inputfile), "Unicode"); }else { isr= new InputStreamReader(new FileInputStream(inputfile), "GB2312"); } //将字符串存入StringBuffer BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); isr.close(); //以哪方式写入文件 if("UTF-8".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); }else if("GB2312".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "GB2312"); }else if("Unicode".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "Unicode"); }else{ osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); } BufferedWriter bw = new BufferedWriter(osw); String sb1 = sb.toString(); String a1 = deal(sb1); bw.write(a1); bw.close(); osw.close(); } /** * 根据文件路径判断编码 * @param str * @return * @throws IOException */ private static String getCharset(String str) throws IOException{ BytesEncodingDetect s = new BytesEncodingDetect(); String code = BytesEncodingDetect.javaname[s.detectEncoding(new File(str))]; return code; } //本方法完成单个无字符的转换 public static String Change(String temp){ String myString = temp.replace("&#", ""); String[] split = myString.split(";"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < split.length; i++) { sb.append((char)Integer.parseInt(split[i])); } return sb.toString(); } //接收String sb1并对字符串的联合处理 public static String deal(String sb1) { //模块化开始 String car="";//小车运输单个字符 while(sb1.length()!=0){ int markStar = sb1.indexOf("&"); //判断方法是以&开头的数据默认为要处理的无字符 if(markStar==0){ String temp = sb1.substring(markStar,8); car = car+Change(temp); sb1=sb1.substring(8); }else if(markStar==-1&sb1;.length()>0){ String temp = sb1.substring(0,sb1.length()); car = car+temp; sb1=sb1.substring(sb1.length()); }else{ String temp = sb1.substring(0,markStar); car = car+temp; sb1=sb1.substring(markStar); } } return car.toString() ; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值