Junjie
昨天 66a9fc7a0065c4b1f0d488018659da98ee8594e7
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
package com.zy.common.i18n;
 
import com.core.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.*;
 
@RestController
@RequestMapping("/i18n")
public class I18nController {
 
    @Autowired
    private I18nMessageService i18nMessageService;
 
    @RequestMapping("/messages")
    public R messages(@RequestParam(required = false) String lang) {
        Locale locale = i18nMessageService.resolveLocale(lang);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locale", I18nLocaleUtils.toTag(locale));
        result.put("defaultLocale", i18nMessageService.getDefaultLocaleTag());
        result.put("supportedLocales", localeOptions(locale));
        result.put("messages", i18nMessageService.getMessages(locale));
        result.put("legacy", i18nMessageService.getLegacyMessages(locale));
        return R.ok(result);
    }
 
    private List<Map<String, String>> localeOptions(Locale locale) {
        List<Map<String, String>> options = new ArrayList<>();
        for (String supportedLocale : i18nMessageService.getSupportedLocaleTags()) {
            LinkedHashMap<String, String> option = new LinkedHashMap<>();
            option.put("tag", supportedLocale);
            option.put("label", i18nMessageService.getMessage("lang." + supportedLocale, locale));
            options.add(option);
        }
        return options;
    }
}