whyclxw
7 天以前 669906b87674d4b60a6fd5ea3a938c636952dbc2
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.whyc.util;
 
import com.whyc.config.I18nLocaleResolver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
 
@Component
public class MessageUtils {
    /*private static MessageSource messageSource;
 
    public MessageUtils(MessageSource messageSource) {
        MessageUtils.messageSource = messageSource;
    }
 
    public static String getMessage(String key) {
        try {
            return messageSource.getMessage(
                    key,
                    null,
                    LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return key;
        }
    }*/
    @Value("${spring.messages.basename}")
    private String basename;
 
    private final  I18nLocaleResolver resolver;
 
    private static I18nLocaleResolver customLocaleResolver;
 
    private static String path;
 
    static{
        if(customLocaleResolver == null) {
            customLocaleResolver = new I18nLocaleResolver();
            path = "i18n/message";
        }
    }
    public MessageUtils(I18nLocaleResolver resolver) {
        this.resolver = resolver;
    }
 
    @PostConstruct
    public void init() {
        setBasename(basename);
        setCustomLocaleResolver(resolver);
    }
 
    /**
     * 获取 国际化后内容信息
     *
     * @param code 国际化key
     * @return 国际化后内容信息
     */
    public static String getMessage(String code) {
        Locale locale = customLocaleResolver.getLocal();
        return getMessage(code, null, code, locale);
    }
 
    /**
     * 获取 国际化后内容信息
     *
     * @param code 国际化key
     * @return 国际化后内容信息
     */
    public static String getMessageSocket(String code, String lang) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        if (lang != null) {
            if ((lang.contains("zh"))) {
                locale = Locale.SIMPLIFIED_CHINESE;
            } else { //不是中文,则采用en_US
                locale = Locale.US;
            }
        }
        return getMessage(code, null, code, locale);
    }
 
    public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
        //判断locale,如果不是zh或en,则使用英文
        String language = locale.getLanguage();
        if((language.equals("zh"))){
            locale = Locale.SIMPLIFIED_CHINESE;
        }else{ //不是中文,则采用en_US
            locale = Locale.US;
        }
        messageSource.setBasename(path);
        String content;
        try {
            content = messageSource.getMessage(code, args, locale);
        } catch (Exception e) {
            content = defaultMessage;
        }
        return content;
 
    }
 
    public static void setBasename(String basename) {
        MessageUtils.path = basename;
    }
 
    public static void setCustomLocaleResolver(I18nLocaleResolver resolver) {
        MessageUtils.customLocaleResolver = resolver;
    }
 
}