@Test public void testLongHTMLWithNoTags() { int maxLength = 25; Locale srcLocale = new Locale(LocaleCode.EN, "English"); Locale transLocale = new Locale(LocaleCode.DE, "German"); Document document = new Document("http: String html = "The quick brown fox jumps over the lazy dog."; when(persistentTranslationService.getMaxLength(BackendID.MS)) .thenReturn(maxLength); List<TypeString> contents = ImmutableList.of( new TypeString(html, MediaType.TEXT_HTML, "meta")); DocumentContent docContent = new DocumentContent(contents, "http: DocumentContent translatedDocContent = documentContentTranslatorService .translateDocument(document, docContent, BackendID.MS); assertThat(translatedDocContent.getContents().get(0).getValue()).isEqualTo(html); assertThat(translatedDocContent.getWarnings()) .extracting(this::responseToString) .containsExactly( "Warning: translation skipped: String length is over 25\n" + "The quick brown fox jumps over the lazy dog."); }
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);  }