add 完成 国际化 功能

This commit is contained in:
疯狂的狮子li
2022-02-21 13:34:58 +08:00
parent 2ccf189139
commit dc6f7cd691
20 changed files with 640 additions and 430 deletions

View File

@@ -1,14 +1,18 @@
package com.ruoyi.common.core.exception;
/**
* 验证码错误异常类
*
* @author ruoyi
*/
public class CaptchaException extends RuntimeException {
private static final long serialVersionUID = 1L;
public CaptchaException(String msg) {
super(msg);
}
}
package com.ruoyi.common.core.exception;
/**
* 验证码错误异常类
*
* @author Lion Li
*/
public class CaptchaException extends RuntimeException {
private static final long serialVersionUID = 1L;
public CaptchaException() {
super("user.jcaptcha.error");
}
public CaptchaException(String msg) {
super(msg);
}
}

View File

@@ -42,6 +42,7 @@ public final class ServiceException extends RuntimeException {
return detailMessage;
}
@Override
public String getMessage() {
return message;
}
@@ -59,4 +60,4 @@ public final class ServiceException extends RuntimeException {
this.detailMessage = detailMessage;
return this;
}
}
}

View File

@@ -1,5 +1,7 @@
package com.ruoyi.common.core.exception.base;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -49,4 +51,16 @@ public class BaseException extends RuntimeException {
this(null, null, null, defaultMessage);
}
@Override
public String getMessage() {
String message = null;
if (!StringUtils.isEmpty(code)) {
message = MessageUtils.message(code, args);
}
if (message == null) {
message = defaultMessage;
}
return message;
}
}

View File

@@ -5,12 +5,12 @@ import com.ruoyi.common.core.exception.base.BaseException;
/**
* 用户信息异常类
*
* @author ruoyi
* @author Lion Li
*/
public class UserException extends BaseException {
private static final long serialVersionUID = 1L;
public UserException(String code, Object[] args) {
public UserException(String code, Object... args) {
super("user", code, args, null);
}
}

View File

@@ -0,0 +1,28 @@
package com.ruoyi.common.core.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* 获取i18n资源文件
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageUtils {
private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);
/**
* 根据消息键和参数 获取消息 委托给spring messageSource
*
* @param code 消息键
* @param args 参数
* @return 获取国际化翻译值
*/
public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
}
}