Validator校验框架使用i18n国际化

目录

一.validator校验框架实现国际化提示

1.springboot项目pom引入Validator坐标

2.定义配置类

3.在实体类中应用Validator校验注解,message使用{}包裹消息码

4.在国际化properties中定义该消息码以及对应值

5.在Controller中开启注解校验

6.备注

二.i18n国际化带有参数的信息以及国际化参数

1.通过MessageSource对象的方法进行参数的匹配

2.国际化参数


一.validator校验框架实现国际化提示

1.springboot项目pom引入Validator坐标

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

2.定义配置类

package com.fkp.config;

import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 配置校验实现国际化
 */
@Configuration
public class ValidationConfiguration {

    @Resource
    ApplicationContext applicationContext;

    @Bean
    public Validator validator(MessageSource messageSource){
        LocalValidatorFactoryBean validator = ValidationAutoConfiguration.defaultValidator(applicationContext);
        validator.setValidationMessageSource(messageSource);
        return validator;
    }
}

3.在实体类中应用Validator校验注解,message使用{}包裹消息码

    static class User{
        @NotBlank(message = "{user.name.notBlank}")
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

4.在国际化properties中定义该消息码以及对应值

messages_en_US.properties

user.name.notBlank=name not be blank!

messages_zh_CN.properties

user.name.notBlank=姓名不能为空!

5.在Controller中开启注解校验

    @RequestMapping(value = "valid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse<User> valid(@Valid User user){
        return RestResponse.success(user);
    }

6.备注

调用该接口即可实现对name属性的校验,当不满足校验规则时,提示信息将实现国际化, 若message指定的国际化码没有找到是会将原值返回。

二.i18n国际化带有参数的信息以及国际化参数

1.通过MessageSource对象的方法进行参数的匹配

示例代码

package com.fkp.util;

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

import java.util.ArrayList;
import java.util.List;

@Component
public class I18nUtils {

    private static MessageSource messageSource;

    @Autowired
    private void setMessageSource(MessageSource messageSource){
        I18nUtils.messageSource = messageSource;
    }

    public static String getMessage(String key){
        String langMessage = key;
        try {
         langMessage = messageSource.getMessage(key, new Object[]{}, LocaleContextHolder.getLocale());
        }catch (NoSuchMessageException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
            langMessage = "i18n inner error!";
        }
        return langMessage;
    }

    public static String getMessageWithArgs(String key, String[] args){
        List<String> argMessageList = new ArrayList<>();
        for (String arg : args) {
            String argMessage = messageSource.getMessage(arg, null, "", LocaleContextHolder.getLocale());
            argMessageList.add(argMessage);
        }
        return messageSource.getMessage(key, argMessageList.toArray(), "unknow error", LocaleContextHolder.getLocale());
    }

}

message文件中使用{0},{1}...作为占位符,传入的参数类型为数组,与数组下标相匹配。

messages_en_US.properties

222={0} parameter missing

2.国际化参数

通过封装方法先将参数当作key国际化后作为国际化消息的参数传入即可。代码示例为上方I18nUtils#getMessageWithArgs方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值