@Test(enabled = false) public void testGermanToEnglish() { if (provider == null) { throw new SkipException("Provider was not correctly prepared."); } String translation = provider.getTranslation("Hallo!", TranslationDirection.GermanToEnglish); assertEquals(translation, "Hello!", "Translation service is not yielding the expected result."); }
@Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { if (!isProviderWorking()) { return null; } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("text=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("lang=").append(getLang(direction)); queryBuilder.append("&srv=tr-text"); URL queryUrl = new URL(queryBuilder.toString()); URLConnection connection = queryUrl.openConnection(); connection.addRequestProperty("User-Agent", userAgent); try (JsonReader rd = new JsonReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) { Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); Response response = gson.fromJson(rd, Response.class); if (response != null) { List<String> result = response.getTexts(); if (result.size() == 1) { return result.get(0); } else if (result.size() > 1) { StringBuilder resultBuilder = new StringBuilder(); for (String text : result) { resultBuilder.append(text).append(' '); } return resultBuilder.toString().trim(); } } } catch (IOException e) { log.error("Error while reading from the service.", e); } catch (JsonParseException e) { log.error("Unexpected error while decoding json", e); } } catch (UnsupportedEncodingException e) { log.error("Error while encoding the text for transfer to the Yandex provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to Yandex appears to have a invalid format.", e); } catch (IOException e) { log.error("Failed to open connection for Yandex provider.", e); } operational = false; return null; }
YandexProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { if (!isProviderWorking()) { return null; } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("text=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("lang=").append(getLang(direction)); queryBuilder.append("&srv=tr-text"); URL queryUrl = new URL(queryBuilder.toString()); URLConnection connection = queryUrl.openConnection(); connection.addRequestProperty("User-Agent", userAgent); try (JsonReader rd = new JsonReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) { Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); Response response = gson.fromJson(rd, Response.class); if (response != null) { List<String> result = response.getTexts(); if (result.size() == 1) { return result.get(0); } else if (result.size() > 1) { StringBuilder resultBuilder = new StringBuilder(); for (String text : result) { resultBuilder.append(text).append(' '); } return resultBuilder.toString().trim(); } } } catch (IOException e) { log.error("Error while reading from the service.", e); } catch (JsonParseException e) { log.error("Unexpected error while decoding json", e); } } catch (UnsupportedEncodingException e) { log.error("Error while encoding the text for transfer to the Yandex provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to Yandex appears to have a invalid format.", e); } catch (IOException e) { log.error("Failed to open connection for Yandex provider.", e); } operational = false; return null; } }
YandexProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { if (!isProviderWorking()) { return null; } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("text=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("lang=").append(getLang(direction)); queryBuilder.append("&srv=tr-text"); URL queryUrl = new URL(queryBuilder.toString()); URLConnection connection = queryUrl.openConnection(); connection.addRequestProperty("User-Agent", userAgent); try (JsonReader rd = new JsonReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) { Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); Response response = gson.fromJson(rd, Response.class); if (response != null) { List<String> result = response.getTexts(); if (result.size() == 1) { return result.get(0); } else if (result.size() > 1) { StringBuilder resultBuilder = new StringBuilder(); for (String text : result) { resultBuilder.append(text).append(' '); } return resultBuilder.toString().trim(); } } } catch (IOException e) { log.error("Error while reading from the service.", e); } catch (JsonParseException e) { log.error("Unexpected error while decoding json", e); } } catch (UnsupportedEncodingException e) { log.error("Error while encoding the text for transfer to the Yandex provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to Yandex appears to have a invalid format.", e); } catch (IOException e) { log.error("Failed to open connection for Yandex provider.", e); } operational = false; return null; } YandexProvider(); }
YandexProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { if (!isProviderWorking()) { return null; } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("text=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("lang=").append(getLang(direction)); queryBuilder.append("&srv=tr-text"); URL queryUrl = new URL(queryBuilder.toString()); URLConnection connection = queryUrl.openConnection(); connection.addRequestProperty("User-Agent", userAgent); try (JsonReader rd = new JsonReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) { Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); Response response = gson.fromJson(rd, Response.class); if (response != null) { List<String> result = response.getTexts(); if (result.size() == 1) { return result.get(0); } else if (result.size() > 1) { StringBuilder resultBuilder = new StringBuilder(); for (String text : result) { resultBuilder.append(text).append(' '); } return resultBuilder.toString().trim(); } } } catch (IOException e) { log.error("Error while reading from the service.", e); } catch (JsonParseException e) { log.error("Unexpected error while decoding json", e); } } catch (UnsupportedEncodingException e) { log.error("Error while encoding the text for transfer to the Yandex provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to Yandex appears to have a invalid format.", e); } catch (IOException e) { log.error("Failed to open connection for Yandex provider.", e); } operational = false; return null; } YandexProvider(); @Nullable @Override String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction); @Override boolean isProviderWorking(); }
YandexProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { if (!isProviderWorking()) { return null; } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("text=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("lang=").append(getLang(direction)); queryBuilder.append("&srv=tr-text"); URL queryUrl = new URL(queryBuilder.toString()); URLConnection connection = queryUrl.openConnection(); connection.addRequestProperty("User-Agent", userAgent); try (JsonReader rd = new JsonReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) { Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); Response response = gson.fromJson(rd, Response.class); if (response != null) { List<String> result = response.getTexts(); if (result.size() == 1) { return result.get(0); } else if (result.size() > 1) { StringBuilder resultBuilder = new StringBuilder(); for (String text : result) { resultBuilder.append(text).append(' '); } return resultBuilder.toString().trim(); } } } catch (IOException e) { log.error("Error while reading from the service.", e); } catch (JsonParseException e) { log.error("Unexpected error while decoding json", e); } } catch (UnsupportedEncodingException e) { log.error("Error while encoding the text for transfer to the Yandex provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to Yandex appears to have a invalid format.", e); } catch (IOException e) { log.error("Failed to open connection for Yandex provider.", e); } operational = false; return null; } YandexProvider(); @Nullable @Override String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction); @Override boolean isProviderWorking();  }