This commit is contained in:
Glavo 2025-09-20 22:52:47 +08:00
parent 454bb8bb7d
commit 51945d837e
3 changed files with 19 additions and 30 deletions

View File

@ -142,9 +142,9 @@ public final class Locales {
}
Locale inJavaLocale = inLocale.getLocale();
if (LocaleUtils.isISO3Language(inJavaLocale.getLanguage())) {
if (inJavaLocale.getLanguage().length() > 2) {
String iso1 = LocaleUtils.getISO1Language(inJavaLocale);
if (LocaleUtils.isISO1Language(iso1)) {
if (iso1.length() <= 2) {
Locale.Builder builder = new Locale.Builder()
.setLocale(inJavaLocale)
.setLanguage(iso1);

View File

@ -30,7 +30,7 @@ import java.util.ResourceBundle;
/// - For all Chinese locales, `zh-CN` is always added to the candidate list. If `zh-Hans` already exists in the candidate list,
/// `zh-CN` is inserted before `zh`; otherwise, it is inserted after `zh`.
/// - For all Traditional Chinese locales, `zh-TW` is always added to the candidate list (before `zh`).
/// - For all [supported][LocaleUtils#toISO1Language(String)] ISO 639-3 language code (such as `eng`, `zho`, `lzh`, etc.),
/// - For all [supported][LocaleUtils#mapToISO1Language(String)] ISO 639-3 language code (such as `eng`, `zho`, `lzh`, etc.),
/// a candidate list with the language code replaced by the ISO 639-1 (Macro)language code is added to the end of the candidate list.
///
/// @author Glavo
@ -41,7 +41,6 @@ public class DefaultResourceBundleControl extends ResourceBundle.Control {
public DefaultResourceBundleControl() {
}
@Override
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
return LocaleUtils.getCandidateLocales(locale);

View File

@ -64,18 +64,24 @@ public final class LocaleUtils {
: locale.stripExtensions().toLanguageTag();
}
public static boolean isISO1Language(String language) {
return language.length() == 2;
}
public static boolean isISO3Language(String language) {
return language.length() == 3;
}
public static @NotNull String getISO1Language(Locale locale) {
String language = locale.getLanguage();
if (language.isEmpty()) return "en";
return isISO3Language(language) ? toISO1Language(language) : language;
if (language.length() <= 2)
return language;
String lang = language;
while (lang != null) {
if (lang.length() <= 2)
return lang;
else {
String iso1 = mapToISO1Language(lang);
if (iso1 != null)
return iso1;
}
lang = getParentLanguage(lang);
}
return language;
}
/// Get the script of the locale. If the script is empty and the language is Chinese,
@ -288,6 +294,7 @@ public final class LocaleUtils {
// ---
/// Map ISO 639-3 language codes to ISO 639-1 language codes.
private static @Nullable String mapToISO1Language(String iso3Language) {
return switch (iso3Language) {
case "eng" -> "en";
@ -309,23 +316,6 @@ public final class LocaleUtils {
};
}
/// Try to convert ISO 639-3 language codes to ISO 639-1 language codes.
public static String toISO1Language(String languageTag) {
String lang = languageTag;
while (lang != null) {
if (lang.length() <= 2)
return lang;
else {
String iso1 = mapToISO1Language(lang);
if (iso1 != null)
return iso1;
}
lang = getParentLanguage(lang);
}
return languageTag;
}
public static boolean isEnglish(Locale locale) {
return "en".equals(getISO1Language(locale));
}