Junjie
1 天以前 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
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
114
package com.zy.common.i18n;
 
import com.core.common.Cools;
 
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class I18nLocaleUtils {
 
    private static final Set<String> UPPERCASE_WORDS = new HashSet<>(
            Arrays.asList("ai", "api", "wcs", "wms", "llm", "rgv", "crn", "plc", "http", "mcp", "erp", "io")
    );
 
    private static final Pattern WORD_BOUNDARY = Pattern.compile("([a-z0-9])([A-Z])");
 
    private I18nLocaleUtils() {
    }
 
    public static Locale defaultLocale(I18nProperties properties) {
        return toLocale(properties.getDefaultLocale());
    }
 
    public static Locale toLocale(String value) {
        if (Cools.isEmpty(value)) {
            return Locale.SIMPLIFIED_CHINESE;
        }
        String normalized = value.replace('_', '-').trim();
        return Locale.forLanguageTag(normalized);
    }
 
    public static String toTag(Locale locale) {
        if (locale == null) {
            return "zh-CN";
        }
        String tag = locale.toLanguageTag();
        return Cools.isEmpty(tag) || "und".equalsIgnoreCase(tag) ? "zh-CN" : tag;
    }
 
    public static Locale resolveLocale(String requested, I18nProperties properties) {
        Locale fallback = defaultLocale(properties);
        if (Cools.isEmpty(requested)) {
            return fallback;
        }
        String requestedTag = requested.replace('_', '-').trim();
        if (requestedTag.contains(",")) {
            requestedTag = requestedTag.substring(0, requestedTag.indexOf(',')).trim();
        }
        if (requestedTag.contains(";")) {
            requestedTag = requestedTag.substring(0, requestedTag.indexOf(';')).trim();
        }
        for (String supported : properties.getSupportedLocales()) {
            String supportedTag = supported.replace('_', '-').trim();
            if (supportedTag.equalsIgnoreCase(requestedTag)) {
                return toLocale(supportedTag);
            }
            Locale supportedLocale = toLocale(supportedTag);
            if (supportedLocale.getLanguage().equalsIgnoreCase(toLocale(requestedTag).getLanguage())) {
                return supportedLocale;
            }
        }
        return fallback;
    }
 
    public static boolean isDefaultLocale(Locale locale, I18nProperties properties) {
        return toTag(defaultLocale(properties)).equalsIgnoreCase(toTag(locale));
    }
 
    public static String humanizeCode(String code) {
        if (Cools.isEmpty(code)) {
            return null;
        }
        String normalized = code;
        int hashIndex = normalized.indexOf('#');
        if (hashIndex > -1 && hashIndex < normalized.length() - 1) {
            normalized = normalized.substring(hashIndex + 1);
        } else {
            int slashIndex = normalized.lastIndexOf('/');
            if (slashIndex > -1 && slashIndex < normalized.length() - 1) {
                normalized = normalized.substring(slashIndex + 1);
            }
            if (normalized.endsWith(".html")) {
                normalized = normalized.substring(0, normalized.length() - 5);
            }
        }
        normalized = normalized.replaceAll("[^A-Za-z0-9_\\-]+", " ");
        Matcher matcher = WORD_BOUNDARY.matcher(normalized);
        normalized = matcher.replaceAll("$1 $2");
        normalized = normalized.replace('_', ' ').replace('-', ' ');
        String[] parts = normalized.trim().split("\\s+");
        if (parts.length == 0) {
            return code;
        }
        StringBuilder builder = new StringBuilder();
        for (String part : parts) {
            if (Cools.isEmpty(part)) {
                continue;
            }
            if (builder.length() > 0) {
                builder.append(' ');
            }
            String lower = part.toLowerCase(Locale.ENGLISH);
            if (UPPERCASE_WORDS.contains(lower)) {
                builder.append(lower.toUpperCase(Locale.ENGLISH));
            } else {
                builder.append(Character.toUpperCase(part.charAt(0)));
                if (part.length() > 1) {
                    builder.append(part.substring(1));
                }
            }
        }
        return builder.length() == 0 ? code : builder.toString();
    }
}