refactor: 增加指定语言方法用于定时任务或后端发送消息时使用

This commit is contained in:
fit2-zhao 2024-09-23 18:29:12 +08:00 committed by Craftsman
parent d1122b8958
commit bc1455314c

View File

@ -4,6 +4,8 @@ import jakarta.annotation.Resource;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import java.util.Locale;
public class Translator {
private static MessageSource messageSource;
@ -26,10 +28,19 @@ public class Translator {
return messageSource.getMessage(key, null, defaultMessage, LocaleContextHolder.getLocale());
}
/**
* 单Key翻译并指定默认语言
*/
public static String get(String key, Locale locale) {
return messageSource.getMessage(key, null, locale);
}
/**
* 带参数
*/
public static String getWithArgs(String key, Object... args) {
return messageSource.getMessage(key, args, "Not Support Key: " + key, LocaleContextHolder.getLocale());
}
}