@Test public void test1EN_ES_Translation() { String inputText = "Hello World!!!"; String expectedText = "Hola Mundo!!!"; try{ String transText = translator.translate(inputText, "en", "es"); assertNotNull("Text not translated", transText); assumeTrue(expectedText.equals(transText)); } catch(TikaException e){ e.printStackTrace(); fail(e.getMessage()); } catch(IOException e){ e.printStackTrace(); fail(e.getMessage()); } }
@Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable()) { return text; } WebClient client = WebClient.create(YANDEX_TRANSLATE_URL_BASE); String langCode; if (sourceLanguage == null) { langCode = targetLanguage; } else { langCode = sourceLanguage + '-' + targetLanguage; } Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", this.apiKey).query("lang", langCode) .query("text", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (!jsonResp.findValuesAsText("code").isEmpty()) { String code = jsonResp.findValuesAsText("code").get(0); if (code.equals("200")) { return jsonResp.findValue("text").get(0).asText(); } else { throw new TikaException(jsonResp.findValue("message").get(0).asText()); } } else { throw new TikaException("Return message not recognized: " + responseText.toString().substring(0, Math.min(responseText.length(), 100))); } } catch (JsonParseException e) { throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response from Lingo24 is not well formatted: " + responseText.toString()); } }
YandexTranslator implements Translator { @Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable()) { return text; } WebClient client = WebClient.create(YANDEX_TRANSLATE_URL_BASE); String langCode; if (sourceLanguage == null) { langCode = targetLanguage; } else { langCode = sourceLanguage + '-' + targetLanguage; } Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", this.apiKey).query("lang", langCode) .query("text", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (!jsonResp.findValuesAsText("code").isEmpty()) { String code = jsonResp.findValuesAsText("code").get(0); if (code.equals("200")) { return jsonResp.findValue("text").get(0).asText(); } else { throw new TikaException(jsonResp.findValue("message").get(0).asText()); } } else { throw new TikaException("Return message not recognized: " + responseText.toString().substring(0, Math.min(responseText.length(), 100))); } } catch (JsonParseException e) { throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response from Lingo24 is not well formatted: " + responseText.toString()); } } }
YandexTranslator implements Translator { @Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable()) { return text; } WebClient client = WebClient.create(YANDEX_TRANSLATE_URL_BASE); String langCode; if (sourceLanguage == null) { langCode = targetLanguage; } else { langCode = sourceLanguage + '-' + targetLanguage; } Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", this.apiKey).query("lang", langCode) .query("text", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (!jsonResp.findValuesAsText("code").isEmpty()) { String code = jsonResp.findValuesAsText("code").get(0); if (code.equals("200")) { return jsonResp.findValue("text").get(0).asText(); } else { throw new TikaException(jsonResp.findValue("message").get(0).asText()); } } else { throw new TikaException("Return message not recognized: " + responseText.toString().substring(0, Math.min(responseText.length(), 100))); } } catch (JsonParseException e) { throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response from Lingo24 is not well formatted: " + responseText.toString()); } } YandexTranslator(); }
YandexTranslator implements Translator { @Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable()) { return text; } WebClient client = WebClient.create(YANDEX_TRANSLATE_URL_BASE); String langCode; if (sourceLanguage == null) { langCode = targetLanguage; } else { langCode = sourceLanguage + '-' + targetLanguage; } Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", this.apiKey).query("lang", langCode) .query("text", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (!jsonResp.findValuesAsText("code").isEmpty()) { String code = jsonResp.findValuesAsText("code").get(0); if (code.equals("200")) { return jsonResp.findValue("text").get(0).asText(); } else { throw new TikaException(jsonResp.findValue("message").get(0).asText()); } } else { throw new TikaException("Return message not recognized: " + responseText.toString().substring(0, Math.min(responseText.length(), 100))); } } catch (JsonParseException e) { throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response from Lingo24 is not well formatted: " + responseText.toString()); } } YandexTranslator(); @Override String translate(String text, String sourceLanguage,
            String targetLanguage); String getApiKey(); void setApiKey(String apiKey); String getFormat(); void setFormat(String format); @Override String translate(String text, String targetLanguage); @Override boolean isAvailable(); }
YandexTranslator implements Translator { @Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable()) { return text; } WebClient client = WebClient.create(YANDEX_TRANSLATE_URL_BASE); String langCode; if (sourceLanguage == null) { langCode = targetLanguage; } else { langCode = sourceLanguage + '-' + targetLanguage; } Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", this.apiKey).query("lang", langCode) .query("text", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (!jsonResp.findValuesAsText("code").isEmpty()) { String code = jsonResp.findValuesAsText("code").get(0); if (code.equals("200")) { return jsonResp.findValue("text").get(0).asText(); } else { throw new TikaException(jsonResp.findValue("message").get(0).asText()); } } else { throw new TikaException("Return message not recognized: " + responseText.toString().substring(0, Math.min(responseText.length(), 100))); } } catch (JsonParseException e) { throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response from Lingo24 is not well formatted: " + responseText.toString()); } } YandexTranslator(); @Override String translate(String text, String sourceLanguage,
            String targetLanguage); String getApiKey(); void setApiKey(String apiKey); String getFormat(); void setFormat(String format); @Override String translate(String text, String targetLanguage); @Override boolean isAvailable();  }