package com.hxzk.gps.util.MessageUtils;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
import org.springframework.context.MessageSource;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Locale;
|
|
@Component
|
public class MessageUtils {
|
|
private static MessageSource messageSource;
|
|
// 通过构造函数注入 MessageSource
|
public MessageUtils(MessageSource messageSource) {
|
MessageUtils.messageSource = messageSource;
|
}
|
|
/**
|
* 获取国际化消息
|
* @param code 消息代码
|
* @param args 消息参数
|
* @return 国际化消息
|
*/
|
public static String getMessage(String code) {
|
try {
|
String saTokenLanguage = (String) StpUtil.getSession().get("user_language");
|
Locale locale;
|
if (saTokenLanguage.equals("zh")){
|
locale = Locale.CHINA;
|
}else{
|
locale = Locale.US;
|
}
|
|
return messageSource.getMessage(code, null, locale);
|
}catch (Exception e){
|
return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
|
}
|
|
|
}
|
}
|