zhitong.yu
7 天以前 7fc9c42987a13c1d8d2159591a5803e70518827f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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());
        }
 
 
    }
}