spring boot国际化——MessageSource的使用

修改properties文件的目录:在application.yml或者application.properties中配置 spring.message.basename

spring:
    application:
        name: test-worklog
    messages:
        basename: i18n/messages
        encoding: UTF-8

这里写图片描述
注:使用MessageSource获取配置文件不可删除message.properties文件。

MessageSource的使用:

  1. 无参数
operation.success = 操作成功。
String msg1 = this.messageSource.getMessage("operation.success", null, Locale.CHINA);  
  1. 动态参数
start.ge.end = 开始日期{0}必须小于结束日期{1}!

String [] param = {startDate, endDate};

String msg =getMessage("start.ge.end", param);

@Autowired
private MessageSource messageSource;
/**
 * 国际化
 *
 * @param result
 * @return
 */
public String getMessage(String result, Object[] params) {
    String message = "";
    try {
        Locale locale = LocaleContextHolder.getLocale();
        message = messageSource.getMessage(result, params, locale);
    } catch (Exception e) {
        LOGGER.error("parse message error! ", e);
    }
    return message;
}

转载自:https://blog.csdn.net/weixin_36833780/article/details/80311486

猜你喜欢

转载自blog.csdn.net/sinat_24044957/article/details/80574947