zj
2024-06-03 287ac389edd047696d956afafdb855a93830bc0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//package com.nq.common.interceptor;
////import com.example.demo.base.vo.Result;
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.JSONObject;
//import com.nq.common.ServerResponse;
//import com.nq.utils.translate.GoogleTranslateUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;
//import java.io.UnsupportedEncodingException;
//import java.net.URLDecoder;
//import java.util.ArrayList;
//
//import static cn.hutool.core.lang.Validator.isChinese;
//
///**
//     * 全局拦截返回值
//     * @author DaenMax
//     */
//    @ControllerAdvice
//    @Slf4j
//    public class MyResponseBodyAdvice implements ResponseBodyAdvice {
//        @Override
//        public boolean supports(MethodParameter returnType, Class converterType) {
//            return true;
//        }
//
//        @Override
//        public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//            //形如:/love/user/test2
//            String requestPath = request.getURI().getPath();
//            HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//            HttpSession httpSession = httpServletRequest.getSession(true);
//            String lang = request.getHeaders().getFirst("lang");
////            if (lang != null && !lang.equals("zh-CN") && !lang.equals("")) {
////                //不做处理
////                return body;
////            }
//            //可以在此处修改body,实现全局拦截返回结果修改后返回
//            ServerResponse result = (ServerResponse) body;
//            //result转换为json
//            String resultJson = JSONObject.toJSONString(result.getData());
//            log.info("翻以前"+resultJson);
//            //resultJson去除所有中文标点
////            resultJson = StringUtils.replaceChars(resultJson, ",。:;?!”“‘’《》〈〉「」『』〖〗【】()〔〕{}⦅⦆﹃﹄﹁﹂﹙﹚﹛﹜﹝﹞", "");
//            //resultJson去除影响翻译中文的标点
//            resultJson = StringUtils.replaceChars(resultJson, ",。:;?!”“‘’《》〈〉「」『』〖〗【】()〔〕{}⦅⦆﹃﹄﹁﹂﹙﹚﹛﹜﹝﹞", "");
//
////            resultJson = StringUtils.replaceChars(resultJson, ",。;:!”“‘’《》【】", "");
//            log.info("去除后"+resultJson);
//            GoogleTranslateUtil transan = new GoogleTranslateUtil();
//            StringBuilder translate = new StringBuilder();
//            //统计resultJson中的中文字符个数
//            int count = 0;
//            for (int i = 0; i < resultJson.length(); i++) {
//                String temp = resultJson.substring(i, i + 1);
//                if (isChinese(temp)) {
//                    count++;
//                }
//            }
//
//            log.info("resultJson中的中文字符个数为{}", count);
//            //如果resultJson中的中文字符个数大于2000分割中文翻译
//
//            if (lang != null && !lang.equals("zh-CN") && !lang.equals("")) {
//                if (count > 1000) {
//                    ArrayList<String> list = new ArrayList<>();
//                    int size = resultJson.length() / 1000;
//                    for (int i = 0; i < size; i++) {
//                        //以中文字符为单位分割
//
//                        String substring = resultJson.substring(i * 1000, (i + 1) * 1000);
//                        list.add(substring);
//                    }
//                    String substring = resultJson.substring(size * 1000);
//                    list.add(substring);
//                    for (String s : list) {
//                        try {
//                            translate.append(transan.translate("zh", lang, s));
//                        } catch (Exception e) {
//                            log.error("翻译异常", e);
//                            return body;
//                        }
//
//                    }
//                } else {
//                    try {
//                        translate.append(transan.translate("zh", lang, resultJson));
//                    } catch (Exception e) {
//                        log.error("翻译异常", e);
//                        return body;
//                    }
//                }
//
//                log.info("翻译结果为{}", translate);
//                //去除\"
//                String s = translate.toString().replaceAll("\\\\\"", "");
//                 JSONObject data = JSONObject.parseObject(s);
//
//                result.setData(data);
//                //放到缓存里,以便于可以在HandlerInterceptor拦截里取出并打印出返回结果
////            httpSession.setAttribute("body", body);
//                return body;
//            }
//
//            return  body;
//        }
//    }