Skip to content

Commit

Permalink
CLDR-17535 Further cleanups (that the unit tests didn't find)
Browse files Browse the repository at this point in the history
  • Loading branch information
macchiati committed Aug 20, 2024
1 parent 046e25f commit df8f070
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -133,15 +137,15 @@ 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";
}
return result;
}

private static String getNameOrError(final String from) {
String result = GenerateMaximalLocales.toAlt(from, true);
String result = toAlt(from, true);
if (result == null) {
return "ERROR";
}
Expand All @@ -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
+ "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String> LOCALE_SOURCE =
/** Compare locales, putting und.* last. */
public static Comparator<String> LOCALE_SOURCE =
new Comparator<>() {

@Override
Expand All @@ -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))
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -1710,7 +1709,7 @@ public static void printLine(
Map<String, String> origins,
boolean first,
PrintWriter out) {
Set<String> keys = new TreeSet<>(new LocaleStringComparator());
Set<String> keys = new TreeSet<>(LOCALE_SOURCE);
keys.addAll(toPrint.keySet());
boolean noUndYet = true;
for (String printingLocale : keys) {
Expand Down

0 comments on commit df8f070

Please sign in to comment.