一、翻译标签文本工具
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();}}
二、腾讯翻译api调用
package org.springblade.common.utils;import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateRequest;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateResponse;
import org.springblade.core.tool.utils.Func;import java.lang.reflect.Field;
public class TencentTranslationUtil {private final static String secretId = "自己的";private final static String secretKey = "自己的";private final static String params = "isMenu,type,icon";private final TmtClient client;public static String getText(String text, String sourceLang, String targetLang) {TencentTranslationUtil translationClient = new TencentTranslationUtil(secretId, secretKey);if (Func.isEmpty(text.trim())) {return text;}try {return translationClient.translateText(text, sourceLang, targetLang);} catch (TencentCloudSDKException e) {System.err.println("腾讯翻译错误 = " + e.getMessage());}return "";}public TencentTranslationUtil(String secretId, String secretKey) {Credential cred = new Credential(secretId, secretKey);client = new TmtClient(cred, "ap-beijing");}public TencentTranslationUtil(String secretId, String secretKey, String region) {Credential cred = new Credential(secretId, secretKey);client = new TmtClient(cred, region);}public String translateText(String text, String sourceLang, String targetLang) throws TencentCloudSDKException {TextTranslateRequest req = new TextTranslateRequest();req.setSourceText(text);req.setSource(sourceLang);req.setTarget(targetLang);req.setProjectId(0L);TextTranslateResponse resp = client.TextTranslate(req);return resp.getTargetText();}public static void translateFields(Object obj, String sourceLang, String targetLang) {if (!sourceLang.equals(targetLang)) {Class<?> clazz = obj.getClass();Field[] fields = clazz.getDeclaredFields();try {for (Field field : fields) {field.setAccessible(true); Object value = field.get(obj);if (Func.isNotEmpty(value) && value instanceof String&& !((String) value).contains("div") && !((String) value).contains("opacity") && !isNumeric(((String) value))) {if (!params.contains(field.getName())) {Thread.sleep(150);String text = TencentTranslationUtil.getText((String) value, sourceLang, targetLang);if (Func.isNotEmpty(text)) {field.set(obj, text); } else {field.set(obj, Func.toStr(value)+"(翻译失败)"); }}}}} catch (Exception e) {}}}public static void main(String[] args) {
if(!isNumeric("1")){System.out.println("翻译");}}public static boolean isNumeric(String str) {return str.matches("-?\\d+(\\.\\d+)?");}
}