From df8f070ea4b6315164b79f913b3a8ce3b296c2dd Mon Sep 17 00:00:00 2001 From: macchiati Date: Mon, 19 Aug 2024 17:09:38 -0700 Subject: [PATCH] CLDR-17535 Further cleanups (that the unit tests didn't find) --- .../cldr/tool/GenerateLikelySubtagTests.java | 64 ++++++++++++++++++- .../cldr/tool/GenerateLikelySubtags.java | 31 +++++---- 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtagTests.java b/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtagTests.java index 73c40e3561b..6bc9f8b388d 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtagTests.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtagTests.java @@ -6,17 +6,21 @@ import java.util.Map; import java.util.Set; import org.unicode.cldr.draft.FileUtilities; -import org.unicode.cldr.tool.GenerateMaximalLocales.OutputStyle; +import org.unicode.cldr.tool.GenerateLikelySubtags.OutputStyle; +import org.unicode.cldr.util.CLDRConfig; import org.unicode.cldr.util.CLDRFile; import org.unicode.cldr.util.CLDRPaths; import org.unicode.cldr.util.CldrUtility; import org.unicode.cldr.util.LanguageTagParser; +import org.unicode.cldr.util.LocaleNames; import org.unicode.cldr.util.SupplementalDataInfo; public class GenerateLikelySubtagTests { private static final String SEPARATOR = CldrUtility.LINE_SEPARATOR; private static final OutputStyle OUTPUT_STYLE = OutputStyle.XML; private static PrintWriter out; + private static CLDRConfig CONFIG = CLDRConfig.getInstance(); + private static CLDRFile ENGLISH = CONFIG.getEnglish(); private static final String VERSION = CLDRFile.GEN_VERSION; @@ -133,7 +137,7 @@ private static void writeTestLine2( } private static String printNameOrError(final String maxFrom) { - String result = GenerateMaximalLocales.printingName(maxFrom, ""); + String result = printingName(maxFrom, ""); if (result == null) { return "ERROR"; } @@ -141,7 +145,7 @@ private static String printNameOrError(final String maxFrom) { } private static String getNameOrError(final String from) { - String result = GenerateMaximalLocales.toAlt(from, true); + String result = toAlt(from, true); if (result == null) { return "ERROR"; } @@ -155,4 +159,58 @@ private static String getItem(String from) { } return "\"" + toAlt + "\""; } + + private static final String[][] ALT_REVERSAL = { + // { "no", "nb" }, + // { "nb", "no" }, + {"he", "iw"}, + {"iw", "he"}, + }; + + public static String toAlt(String locale, boolean change) { + if (!change || locale == null) { + return locale; + } + String firstTag = getFirstTag(locale); + for (String[] pair : ALT_REVERSAL) { + if (firstTag.equals(pair[0])) { + locale = pair[1] + locale.substring(pair[1].length()); + break; + } + } + locale = locale.replace("_", "-"); + return locale; + } + + private static String getFirstTag(String locale) { + int pos = locale.indexOf('_'); + return pos < 0 ? locale : locale.substring(0, pos); + } + + public static String printingName(String locale, String spacing) { + if (locale == null) { + return null; + } + LanguageTagParser parser = new LanguageTagParser().set(locale); + String lang = parser.getLanguage(); + String script = parser.getScript(); + String region = parser.getRegion(); + return "{" + + spacing + + (lang.equals(LocaleNames.UND) + ? "?" + : ENGLISH.getName(CLDRFile.LANGUAGE_NAME, lang)) + + ";" + + spacing + + (script == null || script.equals("") + ? "?" + : ENGLISH.getName(CLDRFile.SCRIPT_NAME, script)) + + ";" + + spacing + + (region == null || region.equals("") + ? "?" + : ENGLISH.getName(CLDRFile.TERRITORY_NAME, region)) + + spacing + + "}"; + } } diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtags.java b/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtags.java index 73cb1248901..5619deb1886 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtags.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/tool/GenerateLikelySubtags.java @@ -31,8 +31,6 @@ import org.unicode.cldr.draft.FileUtilities; import org.unicode.cldr.draft.ScriptMetadata; import org.unicode.cldr.draft.ScriptMetadata.Info; -import org.unicode.cldr.tool.GenerateMaximalLocales.LocaleOverride; -import org.unicode.cldr.tool.GenerateMaximalLocales.LocaleStringComparator; import org.unicode.cldr.tool.LangTagsData.Errors; import org.unicode.cldr.tool.Option.Options; import org.unicode.cldr.tool.Option.Params; @@ -63,6 +61,18 @@ */ public class GenerateLikelySubtags { + public enum OutputStyle { + PLAINTEXT, + C, + C_ALT, + XML + } + + public enum LocaleOverride { + KEEP_EXISTING, + REPLACE_EXISTING + } + private static final Joiner JOIN_TAB = Joiner.on('\t').useForNull("∅"); private static final CLDRConfig CLDR_CONFIG = CLDRConfig.getInstance(); @@ -626,11 +636,8 @@ public static String getPart(CLDRLocale loc, LstrType type) { } } - /** - * Compare locales, first by count of components (handling und), then by language, script, and - * finally region - */ - static Comparator LOCALE_SOURCE = + /** Compare locales, putting und.* last. */ + public static Comparator LOCALE_SOURCE = new Comparator<>() { @Override @@ -640,7 +647,6 @@ public int compare(String locale1, String locale2) { // sort items with 0 components first, then 1, then 2 (there won't be 3) int result = ComparisonChain.start() - // .compare(getMask(l1), getMask(l2)) .compare(getLanguage(l1), getLanguage(l2)) .compare(getScript(l1), getScript(l2)) .compare(getRegion(l1), getRegion(l2)) @@ -686,13 +692,6 @@ public static String getNameSafe(String oldValue) { return "n/a"; } - enum OutputStyle { - PLAINTEXT, - C, - C_ALT, - XML - } - private static OutputStyle OUTPUT_STYLE = OutputStyle.valueOf(CldrUtility.getProperty("OutputStyle", "XML", "XML").toUpperCase()); @@ -1710,7 +1709,7 @@ public static void printLine( Map origins, boolean first, PrintWriter out) { - Set keys = new TreeSet<>(new LocaleStringComparator()); + Set keys = new TreeSet<>(LOCALE_SOURCE); keys.addAll(toPrint.keySet()); boolean noUndYet = true; for (String printingLocale : keys) {