@Test public void testTranslateDocumentContent() { int maxLength = 10000; Locale srcLocale = new Locale(LocaleCode.EN, "English"); Locale transLocale = new Locale(LocaleCode.DE, "German"); String longText = StringUtils.repeat("5", maxLength); String maxString = StringUtils.repeat("t", maxLength + 1); List<String> htmls = ImmutableList.of("<div>Entry 1</div>", "<div>Entry 2</div>", "<div>Entry 6</div>", "<code>KCS code section</code>", "<div translate=\"no\">non translatable node</div>", "<div id=\"private-notes\"><span>private notes</span></div>"); List<String> processedHtmls = ImmutableList.of("<div>Entry 1</div>", "<div>Entry 2</div>", "<div>Entry 6</div>", "<var id=\"ZNTA-6-0\" translate=\"no\"></var>", "<var id=\"ZNTA-7-0\" translate=\"no\"></var>", "<var id=\"ZNTA-8-0\" translate=\"no\"></var>"); List<String> text = ImmutableList.of("Entry 3", "Entry 4", longText); List<String> translatedHtmls = ImmutableList.of("<div>MS: Entry 1</div>", "<div>MS: Entry 2</div>", "<div>MS: Entry 6</div>", "<var id=\"ZNTA-6-0\" translate=\"no\"></var>", "<var id=\"ZNTA-7-0\" translate=\"no\"></var>", "<var id=\"ZNTA-8-0\" translate=\"no\"></var>"); List<String> translatedText = ImmutableList.of("MS: Entry 3", "MS: Entry 4", "MS: Long text"); List<TypeString> contents = ImmutableList.of( new TypeString(htmls.get(0), MediaType.TEXT_HTML, "meta1"), new TypeString(htmls.get(1), MediaType.TEXT_HTML, "meta2"), new TypeString(text.get(0), MediaType.TEXT_PLAIN, "meta3"), new TypeString(text.get(1), MediaType.TEXT_PLAIN, "meta4"), new TypeString(text.get(2), MediaType.TEXT_PLAIN, "meta5"), new TypeString(htmls.get(2), MediaType.TEXT_HTML, "meta6"), new TypeString(htmls.get(3), MediaType.TEXT_HTML, "meta7"), new TypeString(htmls.get(4), MediaType.TEXT_HTML, "meta8"), new TypeString(htmls.get(5), MediaType.TEXT_HTML, "meta9"), new TypeString(maxString, MediaType.TEXT_PLAIN, "meta10")); List<TypeString> translatedContents = ImmutableList.of( new TypeString(translatedHtmls.get(0), MediaType.TEXT_HTML, "meta1"), new TypeString(translatedHtmls.get(1), MediaType.TEXT_HTML, "meta2"), new TypeString(translatedText.get(0), MediaType.TEXT_PLAIN, "meta3"), new TypeString(translatedText.get(1), MediaType.TEXT_PLAIN, "meta4"), new TypeString(translatedText.get(2), MediaType.TEXT_PLAIN, "meta5"), new TypeString(translatedHtmls.get(2), MediaType.TEXT_HTML, "meta6"), new TypeString(htmls.get(3), MediaType.TEXT_HTML, "meta7"), new TypeString(htmls.get(4), MediaType.TEXT_HTML, "meta8"), new TypeString(htmls.get(5), MediaType.TEXT_HTML, "meta9")); DocumentContent docContent = new DocumentContent(contents, "http: Document document = new Document("http: when(persistentTranslationService.getMaxLength(BackendID.MS)) .thenReturn(maxLength); when(persistentTranslationService.translate(document, processedHtmls, srcLocale, transLocale, BackendID.MS, StringType.HTML, Optional.of("tech"))).thenReturn(translatedHtmls); when(persistentTranslationService .translate(document, text.subList(0, 2), srcLocale, transLocale, BackendID.MS, StringType.TEXT_PLAIN, Optional.of("tech"))) .thenReturn(translatedText.subList(0, 2)); when(persistentTranslationService .translate(document, text.subList(2, 3), srcLocale, transLocale, BackendID.MS, StringType.TEXT_PLAIN, Optional.of("tech"))) .thenReturn(translatedText.subList(2, 3)); DocumentContent translatedDocContent = documentContentTranslatorService .translateDocument(document, docContent, BackendID.MS); assertThat(translatedDocContent.getLocaleCode()) .isEqualTo(transLocale.getLocaleCode().getId()); assertThat(translatedDocContent.getBackendId()).isEqualTo(BackendID.MS.getId()); assertThat(translatedDocContent.getUrl()).isEqualTo(docContent.getUrl()); assertThat(translatedDocContent.getWarnings()) .extracting(APIResponse::getDetails) .containsExactly(ShortString.shorten(maxString)); for (int i = 0; i < translatedDocContent.getContents().size() - 1; i++) { assertThat(translatedDocContent.getContents().get(i)) .isEqualTo(translatedContents.get(i)); } int requestsCount = text.size(); verify(persistentTranslationService, times(requestsCount)) .translate(any(), anyList(), any(Locale.class), any(Locale.class), any(BackendID.class), any(StringType.class), any()); }
public DocumentContent translateDocument(Document doc, DocumentContent documentContent, BackendID backendID) throws BadRequestException, MTException { Map<Integer, TypeString> indexTextMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexHTMLMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexXMLMap = new LinkedHashMap<>(); int maxLength = persistentTranslationService.getMaxLength(backendID); List<APIResponse> warnings = new ArrayList<>(); List<TypeString> typeStrings = new ArrayList<>(documentContent.getContents()); int index = 0; for (TypeString typeString: typeStrings) { MediaType mediaType = getMediaType(typeString.getType()); String source = typeString.getValue(); if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && !StringUtils.isBlank(source)) { if (source.length() <= maxLength) { indexTextMap.put(index, typeString); } else { StringTranslationResult result = translatePlainTextBySentences(doc, backendID, source, maxLength); typeString.setValue(result.getTranslation()); warnings.addAll(result.getWarnings()); } } else if (mediaType.equals(MediaType.TEXT_HTML_TYPE) || mediaType.equals(MediaType.TEXT_XML_TYPE)) { boolean xml = mediaType.equals(MediaType.TEXT_XML_TYPE); TranslatableNodeList translatableNodeList = xml ? ArticleUtil.replaceNonTranslatableNodeXML(index, source) : ArticleUtil.replaceNonTranslatableNodeHTML(index, source); String html = translatableNodeList.getHtml(); if (html.length() <= maxLength) { Map<Integer, TypeString> indexMap = xml ? indexXMLMap : indexHTMLMap; indexMap.put(index, typeString); } else { Function<String, Element> toElement = xml ? ArticleUtil::wrapXML : ArticleUtil::wrapHTML; StringTranslationResult result = translateLargeElement(doc, backendID, mediaType, maxLength, html, toElement); String translationWithPlaceholders = result.getTranslation(); String translatedString = removePlaceholders(translationWithPlaceholders, translatableNodeList, toElement); typeString.setValue(translatedString); warnings.addAll(result.getWarnings()); } } index++; } translateAndMergeStringsInBatch(doc, backendID, StringType.TEXT_PLAIN, maxLength, indexTextMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.HTML, maxLength, indexHTMLMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.XML, maxLength, indexXMLMap, typeStrings); return new DocumentContent(typeStrings, documentContent.getUrl(), doc.getToLocale().getLocaleCode().getId(), backendID.getId(), warnings); }
DocumentContentTranslatorService { public DocumentContent translateDocument(Document doc, DocumentContent documentContent, BackendID backendID) throws BadRequestException, MTException { Map<Integer, TypeString> indexTextMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexHTMLMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexXMLMap = new LinkedHashMap<>(); int maxLength = persistentTranslationService.getMaxLength(backendID); List<APIResponse> warnings = new ArrayList<>(); List<TypeString> typeStrings = new ArrayList<>(documentContent.getContents()); int index = 0; for (TypeString typeString: typeStrings) { MediaType mediaType = getMediaType(typeString.getType()); String source = typeString.getValue(); if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && !StringUtils.isBlank(source)) { if (source.length() <= maxLength) { indexTextMap.put(index, typeString); } else { StringTranslationResult result = translatePlainTextBySentences(doc, backendID, source, maxLength); typeString.setValue(result.getTranslation()); warnings.addAll(result.getWarnings()); } } else if (mediaType.equals(MediaType.TEXT_HTML_TYPE) || mediaType.equals(MediaType.TEXT_XML_TYPE)) { boolean xml = mediaType.equals(MediaType.TEXT_XML_TYPE); TranslatableNodeList translatableNodeList = xml ? ArticleUtil.replaceNonTranslatableNodeXML(index, source) : ArticleUtil.replaceNonTranslatableNodeHTML(index, source); String html = translatableNodeList.getHtml(); if (html.length() <= maxLength) { Map<Integer, TypeString> indexMap = xml ? indexXMLMap : indexHTMLMap; indexMap.put(index, typeString); } else { Function<String, Element> toElement = xml ? ArticleUtil::wrapXML : ArticleUtil::wrapHTML; StringTranslationResult result = translateLargeElement(doc, backendID, mediaType, maxLength, html, toElement); String translationWithPlaceholders = result.getTranslation(); String translatedString = removePlaceholders(translationWithPlaceholders, translatableNodeList, toElement); typeString.setValue(translatedString); warnings.addAll(result.getWarnings()); } } index++; } translateAndMergeStringsInBatch(doc, backendID, StringType.TEXT_PLAIN, maxLength, indexTextMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.HTML, maxLength, indexHTMLMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.XML, maxLength, indexXMLMap, typeStrings); return new DocumentContent(typeStrings, documentContent.getUrl(), doc.getToLocale().getLocaleCode().getId(), backendID.getId(), warnings); } }
DocumentContentTranslatorService { public DocumentContent translateDocument(Document doc, DocumentContent documentContent, BackendID backendID) throws BadRequestException, MTException { Map<Integer, TypeString> indexTextMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexHTMLMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexXMLMap = new LinkedHashMap<>(); int maxLength = persistentTranslationService.getMaxLength(backendID); List<APIResponse> warnings = new ArrayList<>(); List<TypeString> typeStrings = new ArrayList<>(documentContent.getContents()); int index = 0; for (TypeString typeString: typeStrings) { MediaType mediaType = getMediaType(typeString.getType()); String source = typeString.getValue(); if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && !StringUtils.isBlank(source)) { if (source.length() <= maxLength) { indexTextMap.put(index, typeString); } else { StringTranslationResult result = translatePlainTextBySentences(doc, backendID, source, maxLength); typeString.setValue(result.getTranslation()); warnings.addAll(result.getWarnings()); } } else if (mediaType.equals(MediaType.TEXT_HTML_TYPE) || mediaType.equals(MediaType.TEXT_XML_TYPE)) { boolean xml = mediaType.equals(MediaType.TEXT_XML_TYPE); TranslatableNodeList translatableNodeList = xml ? ArticleUtil.replaceNonTranslatableNodeXML(index, source) : ArticleUtil.replaceNonTranslatableNodeHTML(index, source); String html = translatableNodeList.getHtml(); if (html.length() <= maxLength) { Map<Integer, TypeString> indexMap = xml ? indexXMLMap : indexHTMLMap; indexMap.put(index, typeString); } else { Function<String, Element> toElement = xml ? ArticleUtil::wrapXML : ArticleUtil::wrapHTML; StringTranslationResult result = translateLargeElement(doc, backendID, mediaType, maxLength, html, toElement); String translationWithPlaceholders = result.getTranslation(); String translatedString = removePlaceholders(translationWithPlaceholders, translatableNodeList, toElement); typeString.setValue(translatedString); warnings.addAll(result.getWarnings()); } } index++; } translateAndMergeStringsInBatch(doc, backendID, StringType.TEXT_PLAIN, maxLength, indexTextMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.HTML, maxLength, indexHTMLMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.XML, maxLength, indexXMLMap, typeStrings); return new DocumentContent(typeStrings, documentContent.getUrl(), doc.getToLocale().getLocaleCode().getId(), backendID.getId(), warnings); } @SuppressWarnings({"unused", "uninitialized"})  DocumentContentTranslatorService(); @Inject  DocumentContentTranslatorService(
            PersistentTranslationService persistentTranslationService); }
DocumentContentTranslatorService { public DocumentContent translateDocument(Document doc, DocumentContent documentContent, BackendID backendID) throws BadRequestException, MTException { Map<Integer, TypeString> indexTextMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexHTMLMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexXMLMap = new LinkedHashMap<>(); int maxLength = persistentTranslationService.getMaxLength(backendID); List<APIResponse> warnings = new ArrayList<>(); List<TypeString> typeStrings = new ArrayList<>(documentContent.getContents()); int index = 0; for (TypeString typeString: typeStrings) { MediaType mediaType = getMediaType(typeString.getType()); String source = typeString.getValue(); if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && !StringUtils.isBlank(source)) { if (source.length() <= maxLength) { indexTextMap.put(index, typeString); } else { StringTranslationResult result = translatePlainTextBySentences(doc, backendID, source, maxLength); typeString.setValue(result.getTranslation()); warnings.addAll(result.getWarnings()); } } else if (mediaType.equals(MediaType.TEXT_HTML_TYPE) || mediaType.equals(MediaType.TEXT_XML_TYPE)) { boolean xml = mediaType.equals(MediaType.TEXT_XML_TYPE); TranslatableNodeList translatableNodeList = xml ? ArticleUtil.replaceNonTranslatableNodeXML(index, source) : ArticleUtil.replaceNonTranslatableNodeHTML(index, source); String html = translatableNodeList.getHtml(); if (html.length() <= maxLength) { Map<Integer, TypeString> indexMap = xml ? indexXMLMap : indexHTMLMap; indexMap.put(index, typeString); } else { Function<String, Element> toElement = xml ? ArticleUtil::wrapXML : ArticleUtil::wrapHTML; StringTranslationResult result = translateLargeElement(doc, backendID, mediaType, maxLength, html, toElement); String translationWithPlaceholders = result.getTranslation(); String translatedString = removePlaceholders(translationWithPlaceholders, translatableNodeList, toElement); typeString.setValue(translatedString); warnings.addAll(result.getWarnings()); } } index++; } translateAndMergeStringsInBatch(doc, backendID, StringType.TEXT_PLAIN, maxLength, indexTextMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.HTML, maxLength, indexHTMLMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.XML, maxLength, indexXMLMap, typeStrings); return new DocumentContent(typeStrings, documentContent.getUrl(), doc.getToLocale().getLocaleCode().getId(), backendID.getId(), warnings); } @SuppressWarnings({"unused", "uninitialized"})  DocumentContentTranslatorService(); @Inject  DocumentContentTranslatorService(
            PersistentTranslationService persistentTranslationService); DocumentContent translateDocument(Document doc,
            DocumentContent documentContent, BackendID backendID); MediaType getMediaType(String mediaType); boolean isMediaTypeSupported(String mediaType); }
DocumentContentTranslatorService { public DocumentContent translateDocument(Document doc, DocumentContent documentContent, BackendID backendID) throws BadRequestException, MTException { Map<Integer, TypeString> indexTextMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexHTMLMap = new LinkedHashMap<>(); Map<Integer, TypeString> indexXMLMap = new LinkedHashMap<>(); int maxLength = persistentTranslationService.getMaxLength(backendID); List<APIResponse> warnings = new ArrayList<>(); List<TypeString> typeStrings = new ArrayList<>(documentContent.getContents()); int index = 0; for (TypeString typeString: typeStrings) { MediaType mediaType = getMediaType(typeString.getType()); String source = typeString.getValue(); if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && !StringUtils.isBlank(source)) { if (source.length() <= maxLength) { indexTextMap.put(index, typeString); } else { StringTranslationResult result = translatePlainTextBySentences(doc, backendID, source, maxLength); typeString.setValue(result.getTranslation()); warnings.addAll(result.getWarnings()); } } else if (mediaType.equals(MediaType.TEXT_HTML_TYPE) || mediaType.equals(MediaType.TEXT_XML_TYPE)) { boolean xml = mediaType.equals(MediaType.TEXT_XML_TYPE); TranslatableNodeList translatableNodeList = xml ? ArticleUtil.replaceNonTranslatableNodeXML(index, source) : ArticleUtil.replaceNonTranslatableNodeHTML(index, source); String html = translatableNodeList.getHtml(); if (html.length() <= maxLength) { Map<Integer, TypeString> indexMap = xml ? indexXMLMap : indexHTMLMap; indexMap.put(index, typeString); } else { Function<String, Element> toElement = xml ? ArticleUtil::wrapXML : ArticleUtil::wrapHTML; StringTranslationResult result = translateLargeElement(doc, backendID, mediaType, maxLength, html, toElement); String translationWithPlaceholders = result.getTranslation(); String translatedString = removePlaceholders(translationWithPlaceholders, translatableNodeList, toElement); typeString.setValue(translatedString); warnings.addAll(result.getWarnings()); } } index++; } translateAndMergeStringsInBatch(doc, backendID, StringType.TEXT_PLAIN, maxLength, indexTextMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.HTML, maxLength, indexHTMLMap, typeStrings); translateAndMergeStringsInBatch(doc, backendID, StringType.XML, maxLength, indexXMLMap, typeStrings); return new DocumentContent(typeStrings, documentContent.getUrl(), doc.getToLocale().getLocaleCode().getId(), backendID.getId(), warnings); } @SuppressWarnings({"unused", "uninitialized"})  DocumentContentTranslatorService(); @Inject  DocumentContentTranslatorService(
            PersistentTranslationService persistentTranslationService); DocumentContent translateDocument(Document doc,
            DocumentContent documentContent, BackendID backendID); MediaType getMediaType(String mediaType); boolean isMediaTypeSupported(String mediaType);  }