springboot好用的国际化封装方法

一、首先一定是国际化的properties,先来三个

文件等一下上properties代码,先上springboot的配置代码吧,需要把这个i18n的路径给告诉springboot。

spring:
  messages:
    basename: i18n/message #国际化配置文件地址
    encoding: utf-8

 现在上properties代码,这里放中文,英文两种:

#<!--参数校验失败400-->
username.not_blank=用户名不能为空!
username.over_max_len=用户名长度不能超过16位!
password.not_blank=密码不能为空!
password.valid_fail=密码校验失败!
captcha.not_blank=验证码不能为空!
nickname.over_max_len=昵称长度不能超过16位!
phone.not_blank=手机号码不能为空!
phone.valid_fail=手机号格式不正确!
email.not_blank=电子邮箱不能为空!
email.valid_fail=电子邮箱格式不正确!

#<!--请求返回代码-->
200=操作成功
400=参数校验失败
401=用户未登录
402=请求未授权
403=请求被拒绝
404=请求地址不正确或接口不存在
405=不支持当前请求方法
415=不支持当前媒体类型
500=服务器异常

1100=没有访问权限
2000=没有找到
2001=添加失败
2002=修改失败
2003=删除失败

#<!--参数校验失败400-->
username.not_blank=username is not blank!
username.over_max_len=The length of the username is less than 16 characters!
password.not_blank=password is not blank!
password.valid_fail=Password verification failed!
captcha.not_blank=captcha is not blank!
nickname.over_max_len=The lenggth of the nickname is less than 16 characters!
phone.not_blank=phone num is not blank!
phone.valid_fail=phone num is not formatted correctly!
email.not_blank=email is not blank!
email.valid_fail=email is not formatted correctly!

#<!--请求返回代码-->
200=The operation succeeded
400=Parameter validation failed
401=The user is not logged in
402=The request is not authorized
403=The request was denied
404=The request address is incorrect or the interface does not exist
405=The current request method is not supported
415=The current media type is not supported
500=Server exception

1100=No access
2000=Not found
2001=Add failed
2002=Modification failed
2003=Delete failed

二、springboot默认的区域信息解析是从请求头部信息中获取用哪种语言,这里我们改一下自定义区域信息解析器,要求首先地址栏如果有指定优先用地址栏指定的语言,如果没有就用请求头部信息中的内容,如果都没有,用服务器的语言。

package com.chhuang.component;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @ClassName UrlLocaleResolver
 * @Description 自己定义区域信息解析器,如果地址栏带l信息,如?l=en_US,就以地址栏为准,
 * 否则以头部信息中的accept-lang为准
 * @Author Darren Huang
 * @Date 2022/11/20 22:58
 * @Version 1.0
 */
@Slf4j
@Component("localeResolver")
public class UrlLocaleResolver implements LocaleResolver {

    private static final String[] ZH_CN = new String[]{"zh-cn","zh_cn","chinese","cn","zh","china"};
    private static final String[] EN_US = new String[]{"en-us","en_us","english","us","en","shit"};
    private static final Locale LOCALE_CN = new Locale("zh", "CN");
    private static final Locale LOCALE_US = new Locale("en", "US");

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter("l");
        Locale locale = null;
        if(StrUtil.isNotBlank(lang)){//地址栏存在语言信息
            if (ArrayUtil.contains(ZH_CN, lang.toLowerCase())){
                locale = LOCALE_CN;
            }else {
                locale = LOCALE_US;
            }
        }else {//从request获取语言信息
            Locale requestLocale = request.getLocale();
            if(requestLocale!=null) {
                lang = requestLocale.toLanguageTag();
                if (ArrayUtil.contains(ZH_CN, lang.toLowerCase())) {
                    locale = LOCALE_CN;
                } else if(ArrayUtil.contains(EN_US, lang.toLowerCase())){
                    locale = LOCALE_US;
                }
            }
        }

        if(locale==null) locale = Locale.getDefault();

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

}

三、为了使用方便,这里再封装一下MessageSource

package com.chhuang.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @ClassName I18nMessage
 * @Description 国际化信息转化组件
 * @Author Darren Huang
 * @Date 2022/11/20 20:13
 * @Version 1.0
 */
@Component
public class I18nMessage {

//    INSTANCE;

    @Autowired
    private MessageSource messageSource;

    public String get(Integer code){
        return get(String.valueOf(code));
    }

    public String get(String key) {
        try {
            return messageSource.getMessage(key, null, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            e.printStackTrace();
            return key;
        }
    }
}

四、好了,这里我们看一下,怎么使用吧

   /**
     * 获取国际化资源组件
     */
    @Autowired
    private I18nMessage i18nMessage;

 

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    @ApiOperation(value = "ChUser查询一条数据", notes = "根据ID查询一条数据的接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回一条ChUser信息")
    })
    public SaResult<ChUser> queryOne(@ApiParam(name = "id", value = "ChUserid编号", required = true) @PathVariable Serializable id) {
        ChUser user= this.chUserService.getById(id);
        if(ObjectUtil.isNotNull(user)) {
            return SaResult.data(i18nMessage.get(SaCode.SUCCESS.getCode()), user);
        }else {
            return SaResult.get(SaCode.SEACH_FAIL.getCode(), i18nMessage.get(SaCode.SEACH_FAIL.getCode()));
        }
    }

五、这里对properties的key做了封装SaCode,为了有空做一下统一异常处理,统一异常处理后比如这里如果判断用户不存在直接抛个异常,不用单独写这个SaResult,后期更新给大家看,先看一下SaCode代码吧。

package com.chhuang.core.enums;

/**
 * @ClassName SaCode
 * @Description 请求返回代码
 * @Author Darren Huang
 * @Date 2022/11/19 0:07
 * @Version 1.0
 */
public enum SaCode {
    /**
     * 请求成功
     */
    SUCCESS(200, "操作成功"),
    PARAMS_VALID_FAIL(400, "参数校验失败"),
    NOT_LOGIN(401, "用户未登录"),
    UN_AUTH(402, "请求未授权"),
    REQ_REJECT(403, "请求被拒绝"),
    NOT_FOUND(404, "请求地址不正确或接口不存在"),
    METHOD_NOT_SUPPORTED(405, "不支持当前请求方法"),
    MEDIA_TYPE_NOT_SUPPORTED(415, "不支持当前媒体类型"),
    INTERNAL_SERVER_ERROR(500, "服务器异常"),

    NOT_AUTH(1100, "没有访问权限"),

    SEACH_FAIL(2000, "没有找到"),
    SAVE_FAIL(2001, "添加失败"),
    MODIFY_FAIL(2002, "修改失败"),
    REMOVE_FAIL(2003, "删除失败");

    /**
     * code编码
     */
    Integer code;
    /**
     * 中文信息描述
     */
    String message;

    SaCode(Integer code, String message){
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getCodeStr(){return code.toString();}

    public String getMessage() {
        return message;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值