java过滤器实现全局的简繁体转化

背景 : 因为是国外项目,有些国家和地区希望默认给繁体,也希望谷歌抓包抓源代码中是繁体推广

首先使用到的是opencc库,值得一提的是前端也有,这样就不会有库对库的冲突了

maven仓库

        <dependency>
            <groupId>com.github.houbb</groupId>
            <artifactId>opencc4j</artifactId>
            <version>1.1.0</version>
        </dependency>

前端的看源码仓库

直接上工具类

public class ChineseConverterUtil {

    // 将繁体中文转换为简体中文
    public static String fToJ(String input) {
        if (StringUtils.isEmpty(input) || !isTraditionalChinese(input)) {
            return input;
        }
        return ZhConverterUtil.convertToSimple(input);
    }

    // 将字符串中的简体中文替换为繁体中文
    public static String jToF(String input) {
        if (StringUtils.isEmpty(input)) {
            return input;
        }
        // 正则表达式匹配简体中文字符
        Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+");
        Matcher matcher = pattern.matcher(input);
        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String simplified = matcher.group();
            String traditional = ZhConverterUtil.convertToTraditional(simplified);
            matcher.appendReplacement(result, traditional);
        }
        matcher.appendTail(result);
        return result.toString();
    }


    // 检查输入文本是否为繁体中文
    public static boolean isTraditionalChinese(String text) {
        for (char c : text.toCharArray()) {
            if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                // 进一步检查字符是否为繁体字,可以根据实际需求进行扩展
                return true;
            }
        }
        return false;
    }
}

然后这样会把简体都替换成繁体字

当然,需要一个过滤器去调用它

package com.ka.service.filter;


import com.ka.common.core.utils.ChineseConverterUtil;
import com.ka.service.wrapper.ResponseWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class AnonymityFilter implements Filter {

    private static Logger logger = LoggerFactory.getLogger(AnonymityFilter.class);


    @Override
    public void init(FilterConfig filterConfig) {
		//初始化操作
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		//转换为代理类
        ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) servletResponse);
        filterChain.doFilter(servletRequest, responseWrapper);
        byte[] content = responseWrapper.getContent();

        if (content.length > 0) {
            String str = new String(content, "UTF-8");
            String ciphertext = null;
            try {
                //在此可以根据需要处理接口返回的信息
                ciphertext = ChineseConverterUtil.jToF(str);
            }
            catch (Exception e)
            {
                //异常最好不要使用e.printStackTrace() 被吃掉
                e.printStackTrace();
            }
            //把返回值输出到客户端
            ServletOutputStream out = servletResponse.getOutputStream();
            out.write(ciphertext.getBytes());
            out.flush();
        }
		//由于我是需要对返回的信息进行处理,所以先进行放行操作。
		//若是需要在请求前进行相关操作,则需要在此请求前处理
		//获取返回值信息
    }
    
    @Override
    public void destroy() {
    //销毁时使用
    }
}

以上都是干货

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值