package org.springblade.common.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TencentTranslationForHTML {
public static void main(String[] args) {
String htmlContent = "<div style=\"text-align:center\"><img src=\"http://192.168.0.137:8999/machine/upload/image/20250208/32551087576762.jpg\" alt=\"图片 alt\" width=\"350\" height=\"auto\" data-align=\"center\"></div><p style=\"text-align: center\">下料工序后的加工件</p>";
try {
String translatedHtml = translateHTML(htmlContent);
System.out.println(translatedHtml);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String translateHTML(String html) throws com.tencentcloudapi.common.exception.TencentCloudSDKException {
Pattern tagPattern = Pattern.compile("<[^>]+>");
Matcher tagMatcher = tagPattern.matcher(html);
List<int[]> tagPositions = new ArrayList<>();
while (tagMatcher.find()) {
tagPositions.add(new int[]{tagMatcher.start(), tagMatcher.end()});
}
List<String> textParts = new ArrayList<>();
int lastEnd = 0;
for (int[] position : tagPositions) {
String textPart = html.substring(lastEnd, position[0]);
if (!textPart.isEmpty()) {
textParts.add(textPart);
}
lastEnd = position[1];
}
String remainingText = html.substring(lastEnd);
if (!remainingText.isEmpty()) {
textParts.add(remainingText);
}
List<String> translatedParts = new ArrayList<>();
for (String text : textParts) {
String translated = TencentTranslationUtil.getText(text,"zh","en");
translatedParts.add(translated);
}
StringBuilder result = new StringBuilder();
int textPartIndex = 0;
lastEnd = 0;
for (int[] position : tagPositions) {
int start = position[0];
int end = position[1];
if (start > lastEnd) {
result.append(translatedParts.get(textPartIndex++));
}
result.append(html, start, end);
lastEnd = end;
}
if (lastEnd < html.length()) {
result.append(translatedParts.get(textPartIndex));
}
return result.toString();
}
}