com.google.api.translate.Translate Java Examples

The following examples show how to use com.google.api.translate.Translate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MachineTranslationPreferencePage.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 访问goolge api以验证 Key是否可用。 ;
 */
private void googleValidator() {
	final String googleKey = googleKeyText.getText();
	BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {

		public void run() {
			// TODO Auto-generated method stub
			if (googleKey != null && !googleKey.trim().equals("")) {
				GoogleAPI.setHttpReferrer("http://www.heartsome.net");
				GoogleAPI.setKey(googleKey);
				try {
					String result = Translate.DEFAULT.execute("test", GoogleTransUtils.processLanguage("en-US"),
							GoogleTransUtils.processLanguage("zh-CN"));
					if (result.equals("测试")) {
						googleState = true;
					}
				} catch (GoogleAPIException e) {
					googleState = false;
				}
			} else {
				googleState = false;
			}
		}
	});
}
 
Example #2
Source File: SimpleMatcherGoogleImpl.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher#executeMatch(net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean)
 */
public String executeMatch(TransUnitInfo2TranslationBean tuInfo) {
	try {
		String srcText = tuInfo.getSrcPureText();
		String srcLang = tuInfo.getSrcLanguage();
		String tgtLang = tuInfo.getTgtLangugage();
		String result = Translate.DEFAULT.execute(srcText, GoogleTransUtils.processLanguage(srcLang),
				GoogleTransUtils.processLanguage(tgtLang));
		result=TextUtil.convertMachineTranslateResult(result);
		if (result != null) {
			return InnerTagClearUtil.clearTmx4Xliff(result);
		}
	} catch (GoogleAPIException e) {
		return "";
	}
	return "";
}
 
Example #3
Source File: GooglePreferencePage.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 访问api以验证 Key是否可用。 ;
 */
private void validator() {
	final String googleKey = googleKeyText.getText();
	BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {

		public void run() {
			// TODO Auto-generated method stub
			if (googleKey != null && !googleKey.trim().equals("")) {
				GoogleAPI.setHttpReferrer("http://www.heartsome.net");
				GoogleAPI.setKey(googleKey);
				try {
					String result = Translate.DEFAULT.execute("test", GoogleTransUtils.processLanguage("en-US"),
							GoogleTransUtils.processLanguage("zh-CN"));
					if (result.equals("测试")) {
						state = true;
					}
				} catch (GoogleAPIException e) {
					state = false;
				}
			} else {
				state = false;
			}
		}
	});
}
 
Example #4
Source File: SimpleMatcherGoogleImpl.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher#executeMatch(net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean)
 */
public String executeMatch(TransUnitInfo2TranslationBean tuInfo) {
	try {
		String srcText = tuInfo.getSrcPureText();
		String srcLang = tuInfo.getSrcLanguage();
		String tgtLang = tuInfo.getTgtLangugage();
		String result = Translate.DEFAULT.execute(srcText, GoogleTransUtils.processLanguage(srcLang),
				GoogleTransUtils.processLanguage(tgtLang));

		// 处理字符转义,有可能翻译的字符有一部分是转义了的,而另一部没有转义
		//bug:3317 fixed by yule 2013-7-26
		String resetSpecialString = TextUtil.resetSpecialString(result);
		result = TextUtil.cleanSpecialString(resetSpecialString);
		if (result != null) {
			return result;
		}
	} catch (GoogleAPIException e) {
		return "";
	}
	return "";
}
 
Example #5
Source File: GooglePreferencePage.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 访问api以验证 Key是否可用。 ;
 */
private void validator() {
	final String googleKey = googleKeyText.getText();
	BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {

		public void run() {
			// TODO Auto-generated method stub
			if (googleKey != null && !googleKey.trim().equals("")) {
				GoogleAPI.setHttpReferrer("http://www.heartsome.net");
				GoogleAPI.setKey(googleKey);
				try {
					String result = Translate.DEFAULT.execute("test", GoogleTransUtils.processLanguage("en-US"),
							GoogleTransUtils.processLanguage("zh-CN"));
					if (result.equals("测试")) {
						state = true;
					}
				} catch (GoogleAPIException e) {
					state = false;
				}
			} else {
				state = false;
			}
		}
	});
}
 
Example #6
Source File: SimpleMatcherGoogleImpl.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher#executeMatch(net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean)
 */
public String executeMatch(TransUnitInfo2TranslationBean tuInfo) {
	try {
		String srcText = tuInfo.getSrcPureText();
		String srcLang = tuInfo.getSrcLanguage();
		String tgtLang = tuInfo.getTgtLangugage();
		String result = Translate.DEFAULT.execute(srcText, GoogleTransUtils.processLanguage(srcLang),
				GoogleTransUtils.processLanguage(tgtLang));
		if (result != null) {
			return result;
		}
	} catch (GoogleAPIException e) {
		return "";
	}
	return "";
}
 
Example #7
Source File: MachineTranslationPreferencePage.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 访问bing api是否可用 ;
 */
private void bingValidator() {

	BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
		public void run() {
			String id = idText.getText();
			String bingKey = bingKeyText.getText();
			if (bingKey != null && !bingKey.trim().equals("") && id != null && !id.equals("")) {
				com.memetix.mst.translate.Translate.setClientId(id);
				com.memetix.mst.translate.Translate.setClientSecret(bingKey);
				try {
					String result = com.memetix.mst.translate.Translate.execute("test",
							BingTransUtils.processLanguage("en-US"), BingTransUtils.processLanguage("zh-CN"));
					if (result.equals("测试")) {
						bingState = true;
					} else {
						bingState = false;
					}
				} catch (Exception e) {
					bingState = false;
				}
			} else {
				bingState = false;
			}
		}
	});

}
 
Example #8
Source File: GoogleTranslateBox.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public static String translate(String text, Language sourceLang, Language targetLang, boolean markTranslation) {
    String translatedText = null;
    if(text != null && sourceLang != null && targetLang != null) {
        try {
            System.out.println("Google Translating '"+text+"' from '"+sourceLang+"' to '"+targetLang+"'");
            GoogleAPI.setHttpReferrer("http://wandora.org");
            if(apikey == null || apikey.length() == 0) {
                apikey = WandoraOptionPane.showInputDialog(Wandora.getWandora(), "Give your Google Translate API key?", "", "Give your Google Translate API key?");
                if(apikey != null) apikey = apikey.trim();
            }
            if(apikey != null) {
                GoogleAPI.setKey(apikey);
                translatedText = Translate.DEFAULT.execute(text, sourceLang, targetLang);
                if(translatedText != null && translatedText.length() == 0) {
                    translatedText = null;
                }
                if(translatedText != null && markTranslation) {
                    translatedText += " [GOOGLE TRANSLATION]";
                }
            }
        }
        catch(Exception e) {
            Wandora.getWandora().handleError(e);
            //e.printStackTrace();
        }
    }
    return translatedText;
}