@Test public void testTranslate() { String content = "content"; BackendLocaleCode srcLocale = new BackendLocaleCodeImpl(LocaleCode.EN); BackendLocaleCode transLocale = new BackendLocaleCodeImpl(LocaleCode.DE); MSTranslateArrayResp resp = new MSTranslateArrayResp(); List<MSTranslateArrayResponse> respList = new ArrayList<>(); respList.add(buildMSResponse("translation1")); resp.setResponse(respList); String responseString = dtoUtil.toXML(resp); MicrosoftTranslatorClient api = Mockito.mock(MicrosoftTranslatorClient.class); when(api.requestTranslations(any())).thenReturn(responseString); msBackend = new MicrosoftTranslatorBackend("subscriptionKey", dtoUtil); msBackend.setApi(api); List<AugmentedTranslation> translations = msBackend .translate(ImmutableList.of(content), srcLocale, transLocale, StringType.TEXT_PLAIN, Optional.of("tech")); assertThat(translations).hasSize(1); AugmentedTranslation translation = translations.get(0); assertThat(translation.getPlainTranslation()).isEqualTo("translation1"); assertThat(translation.getRawTranslation()) .isEqualTo(dtoUtil.toXML(resp.getResponse().get(0))); }
@Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode fromLocale, BackendLocaleCode toLocale, StringType stringType, Optional<String> category) throws MTException { try { MSTranslateArrayReq req = new MSTranslateArrayReq(); req.setSrcLanguage(fromLocale.getLocaleCode()); req.setTransLanguage(toLocale.getLocaleCode()); for (String content: contents) { req.getTexts().add(new MSString(content)); } MSTranslateArrayReqOptions options = new MSTranslateArrayReqOptions(); String format; switch (stringType) { case HTML: case XML: format = MediaType.TEXT_HTML; break; case TEXT_PLAIN: format = MediaType.TEXT_PLAIN; break; default: throw new IllegalArgumentException(stringType.name()); } options.setContentType(format); category.ifPresent(options::setCategory); req.setOptions(options); String rawResponse = api.requestTranslations(req); MSTranslateArrayResp resp = dtoUtil.toObject(rawResponse, MSTranslateArrayResp.class); return resp.getResponse().stream().map( res -> new AugmentedTranslation(res.getTranslatedText().getValue(), dtoUtil.toXML(res))) .collect(Collectors.toList()); } catch (JAXBException e) { throw new MTException("Unable to get translations from MS API", e); } }
MicrosoftTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode fromLocale, BackendLocaleCode toLocale, StringType stringType, Optional<String> category) throws MTException { try { MSTranslateArrayReq req = new MSTranslateArrayReq(); req.setSrcLanguage(fromLocale.getLocaleCode()); req.setTransLanguage(toLocale.getLocaleCode()); for (String content: contents) { req.getTexts().add(new MSString(content)); } MSTranslateArrayReqOptions options = new MSTranslateArrayReqOptions(); String format; switch (stringType) { case HTML: case XML: format = MediaType.TEXT_HTML; break; case TEXT_PLAIN: format = MediaType.TEXT_PLAIN; break; default: throw new IllegalArgumentException(stringType.name()); } options.setContentType(format); category.ifPresent(options::setCategory); req.setOptions(options); String rawResponse = api.requestTranslations(req); MSTranslateArrayResp resp = dtoUtil.toObject(rawResponse, MSTranslateArrayResp.class); return resp.getResponse().stream().map( res -> new AugmentedTranslation(res.getTranslatedText().getValue(), dtoUtil.toXML(res))) .collect(Collectors.toList()); } catch (JAXBException e) { throw new MTException("Unable to get translations from MS API", e); } } }
MicrosoftTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode fromLocale, BackendLocaleCode toLocale, StringType stringType, Optional<String> category) throws MTException { try { MSTranslateArrayReq req = new MSTranslateArrayReq(); req.setSrcLanguage(fromLocale.getLocaleCode()); req.setTransLanguage(toLocale.getLocaleCode()); for (String content: contents) { req.getTexts().add(new MSString(content)); } MSTranslateArrayReqOptions options = new MSTranslateArrayReqOptions(); String format; switch (stringType) { case HTML: case XML: format = MediaType.TEXT_HTML; break; case TEXT_PLAIN: format = MediaType.TEXT_PLAIN; break; default: throw new IllegalArgumentException(stringType.name()); } options.setContentType(format); category.ifPresent(options::setCategory); req.setOptions(options); String rawResponse = api.requestTranslations(req); MSTranslateArrayResp resp = dtoUtil.toObject(rawResponse, MSTranslateArrayResp.class); return resp.getResponse().stream().map( res -> new AugmentedTranslation(res.getTranslatedText().getValue(), dtoUtil.toXML(res))) .collect(Collectors.toList()); } catch (JAXBException e) { throw new MTException("Unable to get translations from MS API", e); } } @SuppressWarnings("unused")  MicrosoftTranslatorBackend(); @Inject  MicrosoftTranslatorBackend(@Credentials(BackendID.MS) String msAPIKey, DTOUtil dtoUtil); }
MicrosoftTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode fromLocale, BackendLocaleCode toLocale, StringType stringType, Optional<String> category) throws MTException { try { MSTranslateArrayReq req = new MSTranslateArrayReq(); req.setSrcLanguage(fromLocale.getLocaleCode()); req.setTransLanguage(toLocale.getLocaleCode()); for (String content: contents) { req.getTexts().add(new MSString(content)); } MSTranslateArrayReqOptions options = new MSTranslateArrayReqOptions(); String format; switch (stringType) { case HTML: case XML: format = MediaType.TEXT_HTML; break; case TEXT_PLAIN: format = MediaType.TEXT_PLAIN; break; default: throw new IllegalArgumentException(stringType.name()); } options.setContentType(format); category.ifPresent(options::setCategory); req.setOptions(options); String rawResponse = api.requestTranslations(req); MSTranslateArrayResp resp = dtoUtil.toObject(rawResponse, MSTranslateArrayResp.class); return resp.getResponse().stream().map( res -> new AugmentedTranslation(res.getTranslatedText().getValue(), dtoUtil.toXML(res))) .collect(Collectors.toList()); } catch (JAXBException e) { throw new MTException("Unable to get translations from MS API", e); } } @SuppressWarnings("unused")  MicrosoftTranslatorBackend(); @Inject  MicrosoftTranslatorBackend(@Credentials(BackendID.MS) String msAPIKey, DTOUtil dtoUtil); void onInit(
            @Observes @Initialized(ApplicationScoped.class) Object init); @Override List<AugmentedTranslation> translate(List<String> contents,
            BackendLocaleCode fromLocale,
            BackendLocaleCode toLocale, StringType stringType,
            Optional<String> category); @Override BackendLocaleCode getMappedLocale(@NotNull LocaleCode localeCode); @Override int getCharLimitPerRequest(); @Override BackendID getId(); }
MicrosoftTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode fromLocale, BackendLocaleCode toLocale, StringType stringType, Optional<String> category) throws MTException { try { MSTranslateArrayReq req = new MSTranslateArrayReq(); req.setSrcLanguage(fromLocale.getLocaleCode()); req.setTransLanguage(toLocale.getLocaleCode()); for (String content: contents) { req.getTexts().add(new MSString(content)); } MSTranslateArrayReqOptions options = new MSTranslateArrayReqOptions(); String format; switch (stringType) { case HTML: case XML: format = MediaType.TEXT_HTML; break; case TEXT_PLAIN: format = MediaType.TEXT_PLAIN; break; default: throw new IllegalArgumentException(stringType.name()); } options.setContentType(format); category.ifPresent(options::setCategory); req.setOptions(options); String rawResponse = api.requestTranslations(req); MSTranslateArrayResp resp = dtoUtil.toObject(rawResponse, MSTranslateArrayResp.class); return resp.getResponse().stream().map( res -> new AugmentedTranslation(res.getTranslatedText().getValue(), dtoUtil.toXML(res))) .collect(Collectors.toList()); } catch (JAXBException e) { throw new MTException("Unable to get translations from MS API", e); } } @SuppressWarnings("unused")  MicrosoftTranslatorBackend(); @Inject  MicrosoftTranslatorBackend(@Credentials(BackendID.MS) String msAPIKey, DTOUtil dtoUtil); void onInit(
            @Observes @Initialized(ApplicationScoped.class) Object init); @Override List<AugmentedTranslation> translate(List<String> contents,
            BackendLocaleCode fromLocale,
            BackendLocaleCode toLocale, StringType stringType,
            Optional<String> category); @Override BackendLocaleCode getMappedLocale(@NotNull LocaleCode localeCode); @Override int getCharLimitPerRequest(); @Override BackendID getId();  }