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();
|
}
|
}
|