我有一个响应,我从json解析并显示结果.是否有可能转换为其他语言,如法语,印地语,德语?
当我浏览时,我开始知道谷歌在2011年停止作为免费版本并开始定价.有没有免费版本将响应文本转换为其他语言?
一段代码如下:
TextView text; // created an id.
JSONObject jsono=new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
String name= object.getString("name");
text.setText(name);// how to convert this to other language.
比如说:回复我得到的是早上好.我需要翻译并在textview中显示为法语的Bonjour.
解决方法:
这是一个在Android应用程序上使用不同翻译服务的详细blog post.源代码于github,样本使用MyMemory service进行翻译.
/** Translate a given text between a source and a destination language */
public String translate(String text) {
String translated = null;
try {
String query = URLEncoder.encode(text, "UTF-8");
String langpair = URLEncoder.encode(srcLanguage.getLanguage()+"|"+dstLanguage.getLanguage(), "UTF-8");
String url = "http://mymemory.translated.net/api/get?q="+query+"&langpair="+langpair;
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(url);
HttpResponse hr = hc.execute(hg);
if(hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
JSONObject response = new JSONObject(EntityUtils.toString(hr.getEntity()));
translated = response.getJSONObject("responseData").getString("translatedText");
}
} catch (Exception e) {
e.printStackTrace();
}
return translated;
}
标签:android,google-translate,machine-translation
来源: https://codeday.me/bug/20190628/1319736.html