@Test public void testGermanToEnglish() { if (provider == null) { throw new SkipException("Provider was not correctly prepared."); } String translation = provider.getTranslation("Hallo!", TranslationDirection.GermanToEnglish); assertEquals(translation, "Hi!", "Translation service is not yielding the expected result."); }
@Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("q=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("langpair=").append(getLangPair(direction)); queryBuilder.append('&').append("of=json"); queryBuilder.append('&').append("de=").append(URLEncoder.encode("webmaster@illarion.org", "UTF-8")); URL queryUrl = new URL(queryBuilder.toString()); try (JsonReader rd = new JsonReader(new InputStreamReader(queryUrl.openStream(), 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) && (response.getResponseData() != null) && (response.getResponseData().getMatch() > 0.75)) { return response.getResponseData().getTranslatedText(); } } 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 MyMemory provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to MyMemory appears to have a invalid format.", e); } operational = false; return null; }
MyMemoryProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("q=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("langpair=").append(getLangPair(direction)); queryBuilder.append('&').append("of=json"); queryBuilder.append('&').append("de=").append(URLEncoder.encode("webmaster@illarion.org", "UTF-8")); URL queryUrl = new URL(queryBuilder.toString()); try (JsonReader rd = new JsonReader(new InputStreamReader(queryUrl.openStream(), 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) && (response.getResponseData() != null) && (response.getResponseData().getMatch() > 0.75)) { return response.getResponseData().getTranslatedText(); } } 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 MyMemory provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to MyMemory appears to have a invalid format.", e); } operational = false; return null; } }
MyMemoryProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("q=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("langpair=").append(getLangPair(direction)); queryBuilder.append('&').append("of=json"); queryBuilder.append('&').append("de=").append(URLEncoder.encode("webmaster@illarion.org", "UTF-8")); URL queryUrl = new URL(queryBuilder.toString()); try (JsonReader rd = new JsonReader(new InputStreamReader(queryUrl.openStream(), 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) && (response.getResponseData() != null) && (response.getResponseData().getMatch() > 0.75)) { return response.getResponseData().getTranslatedText(); } } 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 MyMemory provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to MyMemory appears to have a invalid format.", e); } operational = false; return null; } MyMemoryProvider(); }
MyMemoryProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("q=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("langpair=").append(getLangPair(direction)); queryBuilder.append('&').append("of=json"); queryBuilder.append('&').append("de=").append(URLEncoder.encode("webmaster@illarion.org", "UTF-8")); URL queryUrl = new URL(queryBuilder.toString()); try (JsonReader rd = new JsonReader(new InputStreamReader(queryUrl.openStream(), 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) && (response.getResponseData() != null) && (response.getResponseData().getMatch() > 0.75)) { return response.getResponseData().getTranslatedText(); } } 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 MyMemory provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to MyMemory appears to have a invalid format.", e); } operational = false; return null; } MyMemoryProvider(); @Nullable @Override String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction); @Override boolean isProviderWorking(); }
MyMemoryProvider implements TranslationProvider { @Nullable @Override public String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(serviceUrl).append('?'); try { queryBuilder.append("q=").append(URLEncoder.encode(original, "UTF-8")); queryBuilder.append('&').append("langpair=").append(getLangPair(direction)); queryBuilder.append('&').append("of=json"); queryBuilder.append('&').append("de=").append(URLEncoder.encode("webmaster@illarion.org", "UTF-8")); URL queryUrl = new URL(queryBuilder.toString()); try (JsonReader rd = new JsonReader(new InputStreamReader(queryUrl.openStream(), 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) && (response.getResponseData() != null) && (response.getResponseData().getMatch() > 0.75)) { return response.getResponseData().getTranslatedText(); } } 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 MyMemory provider.", e); } catch (MalformedURLException e) { log.error("Generated URL for the query to MyMemory appears to have a invalid format.", e); } operational = false; return null; } MyMemoryProvider(); @Nullable @Override String getTranslation(@Nonnull String original, @Nonnull TranslationDirection direction); @Override boolean isProviderWorking();  }