Skip to content

Commit

Permalink
CLDR-15830 Move NameTable from CLDRFile to NameType; remove getNameFr…
Browse files Browse the repository at this point in the history
…omTypenumCode

-Also move the NameTable-associated methods, and consolidate

-Change more int to NameType

-Make toCldrInt private

-Remove dead code
  • Loading branch information
btangmu committed Dec 15, 2024
1 parent 2e16740 commit e79ee94
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ private static List<String> compare(
BasicLanguageData.Type oldValue = oldDataToType.get(s);
BasicLanguageData.Type newValue = newDataToType.get(s);
if (!CldrUtility.equals(oldValue, newValue)) {
int code = s.length() == 4 ? CLDRFile.SCRIPT_NAME : CLDRFile.TERRITORY_NAME;
String name = englishNameGetter.getNameFromTypenumCode(code, s);
NameType nameType = s.length() == 4 ? NameType.SCRIPT : NameType.TERRITORY;
String name = englishNameGetter.getNameFromTypeEnumCode(nameType, s);
temp.setLength(0);
temp.append("[").append(s).append(":").append(name).append("] ");
if (oldValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.unicode.cldr.util.CLDRTransforms.ParsedTransformID;
import org.unicode.cldr.util.CLDRTransforms.Visibility;
import org.unicode.cldr.util.LanguageTagParser;
import org.unicode.cldr.util.NameType;
import org.unicode.cldr.util.StandardCodes;
import org.unicode.cldr.util.StandardCodes.CodeType;
import org.unicode.cldr.util.With;
Expand Down Expand Up @@ -244,25 +245,25 @@ private String getName(String target) {
ltp.set(target);
if (ltp.getLanguage().equals("und")) {
String result = "";
result = add(result, CLDRFile.SCRIPT_NAME, ltp.getScript());
result = add(result, CLDRFile.TERRITORY_NAME, ltp.getRegion());
result = add(result, NameType.SCRIPT, ltp.getScript());
result = add(result, NameType.TERRITORY, ltp.getRegion());
for (String v : ltp.getVariants()) {
result = add(result, CLDRFile.VARIANT_NAME, v);
result = add(result, NameType.VARIANT, v);
}
return result;
}
return english.nameGetter().getNameFromBCP47(target.replace('-', '_'));
}

private String add(String result, int type, String code) {
private String add(String result, NameType type, String code) {
if (code.isEmpty()) {
return result;
}
if (result.length() != 0) {
result += ", ";
}
String temp = english.nameGetter().getNameFromTypenumCode(type, code);
if (type == CLDRFile.SCRIPT_NAME && fieldToCode.containsKey(temp)) {
String temp = english.nameGetter().getNameFromTypeEnumCode(type, code);
if (type == NameType.SCRIPT && fieldToCode.containsKey(temp)) {
temp += "*";
}
return result + (temp == null ? code : temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public static void main(String[] args) throws Exception {
String code = it.next();
String priority = priorityMap.get(code);
if (priority == null) continue;
int type = getType(code);
// if (type != CLDRFile.TERRITORY_NAME) continue;
NameType type = getType(code);
priority_set.add(new String[] {priority, type + "", code});
}
String lastPriority = "";
Expand Down Expand Up @@ -167,15 +166,15 @@ public static void main(String[] args) throws Exception {
CLDRFile sourceData = cldrFactory.make(sourceLocale, true);
pw.println();
String title = sourceLocale;
checkItems(pw, title, sourceData, CLDRFile.LANGUAGE_NAME, targetLanguageSet);
checkItems(pw, title, sourceData, CLDRFile.SCRIPT_NAME, targetScriptSet);
checkItems(pw, title, sourceData, CLDRFile.TERRITORY_NAME, targetRegionSet);
checkItems(pw, title, sourceData, CLDRFile.CURRENCY_NAME, targetCurrencySet);
checkItems(pw, title, sourceData, NameType.LANGUAGE, targetLanguageSet);
checkItems(pw, title, sourceData, NameType.SCRIPT, targetScriptSet);
checkItems(pw, title, sourceData, NameType.TERRITORY, targetRegionSet);
checkItems(pw, title, sourceData, NameType.CURRENCY, targetCurrencySet);
// only check timezones if exemplar characters don't include a-z
String v = sourceData.getStringValue("//ldml/characters/exemplarCharacters");
UnicodeSet exemplars = new UnicodeSet(v);
if (exemplars.contains('a', 'z')) continue;
checkItems(pw, title, sourceData, CLDRFile.TZ_EXEMPLAR, targetTZSet);
checkItems(pw, title, sourceData, NameType.TZ_EXEMPLAR, targetTZSet);
}
pw.println();
pw.println("Sizes - incremental");
Expand Down Expand Up @@ -322,7 +321,7 @@ static void checkItems(
PrintWriter pw,
String sourceLocale,
CLDRFile sourceData,
int type,
NameType type,
Set<String> targetItemSet) {
for (Iterator<String> it2 = targetItemSet.iterator(); it2.hasNext(); ) {
String item = it2.next();
Expand Down Expand Up @@ -359,40 +358,40 @@ private static String getItemName(CLDRFile data, String item) {
return getItemName(data, getType(item), item);
}

private static int getType(String item) {
int type = CLDRFile.LANGUAGE_NAME;
if (item.indexOf('/') >= 0) type = CLDRFile.TZ_EXEMPLAR; // America/Los_Angeles
else if (item.length() == 4) type = CLDRFile.SCRIPT_NAME; // Hant
else if (item.charAt(0) <= '9') type = CLDRFile.TERRITORY_NAME; // 001
private static NameType getType(String item) {
NameType type = NameType.LANGUAGE;
if (item.indexOf('/') >= 0) type = NameType.TZ_EXEMPLAR; // America/Los_Angeles
else if (item.length() == 4) type = NameType.SCRIPT; // Hant
else if (item.charAt(0) <= '9') type = NameType.TERRITORY; // 001
else if (item.charAt(0) < 'a') {
if (item.length() == 3) type = CLDRFile.CURRENCY_NAME;
else type = CLDRFile.TERRITORY_NAME; // US or USD
if (item.length() == 3) type = NameType.CURRENCY;
else type = NameType.TERRITORY; // US or USD
}
return type;
}

private static String getTypeName(String item) {
switch (getType(item)) {
case CLDRFile.LANGUAGE_NAME:
case LANGUAGE:
return "Lang";
case CLDRFile.TZ_EXEMPLAR:
case TZ_EXEMPLAR:
return "Zone";
case CLDRFile.SCRIPT_NAME:
case SCRIPT:
return "Script";
case CLDRFile.TERRITORY_NAME:
case TERRITORY:
return "Region";
case CLDRFile.CURRENCY_NAME:
case CURRENCY:
return "Curr.";
}
return "?";
}

private static String getItemName(CLDRFile data, int type, String item) {
private static String getItemName(CLDRFile data, NameType type, String item) {
String result;
if (type == CLDRFile.LANGUAGE_NAME) {
if (type == NameType.LANGUAGE) {
result = data.nameGetter().getNameFromBCP47(item);
} else if (type != CLDRFile.TZ_EXEMPLAR) {
result = data.nameGetter().getNameFromTypenumCode(type, item);
} else if (type != NameType.TZ_EXEMPLAR) {
result = data.nameGetter().getNameFromTypeEnumCode(type, item);
} else {
String prefix = "//ldml/dates/timeZoneNames/zone[@type=\"" + item + "\"]/exemplarCity";
result = data.getStringValue(prefix);
Expand Down
6 changes: 3 additions & 3 deletions tools/cldr-code/src/main/java/org/unicode/cldr/tool/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ private static void listObsoletes() {
for (Iterator<StandardCodes.CodeType> typeIt = sc.getAvailableTypesEnum().iterator();
typeIt.hasNext(); ) {
StandardCodes.CodeType type = typeIt.next();
int typeNum = type.toCldrTypeNum();
if (typeNum == CLDRFile.NO_NAME) {
NameType nameType = type.toNameType();
if (nameType == NameType.NONE) {
System.out.println("listObsoletes skipping " + type);
continue;
}
Expand All @@ -285,7 +285,7 @@ private static void listObsoletes() {
if (list.size() < 3) continue;
String replacementCode = list.get(2);
if (replacementCode == null || replacementCode.length() == 0) continue;
String name = englishNameGetter.getNameFromTypenumCode(typeNum, replacementCode);
String name = englishNameGetter.getNameFromTypeEnumCode(nameType, replacementCode);
System.out.println(code + " => " + replacementCode + "; " + name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2655,97 +2655,6 @@ private Collection<String> getContainedCollection(String start, int depth) {
return contains;
}

/**
* @param table TODO
*/
public void printMissing(PrintWriter pw, int source, int table) {
Set<String> missingItems = new HashSet<>();
String type = null;
if (source == CLDRFile.TERRITORY_NAME) {
type = "territory";
missingItems.addAll(sc.getAvailableCodes(type));
missingItems.removeAll(territory_languages.keySet());
missingItems.removeAll(supplementalDataInfo.getContainmentCore().keySet());
missingItems.remove("200"); // czechoslovakia
} else if (source == CLDRFile.SCRIPT_NAME) {
type = "script";
missingItems.addAll(sc.getAvailableCodes(type));
missingItems.removeAll(script_languages.keySet());
} else if (source == CLDRFile.LANGUAGE_NAME) {
type = "language";
missingItems.addAll(sc.getAvailableCodes(type));
if (table == CLDRFile.SCRIPT_NAME)
missingItems.removeAll(language_scripts.keySet());
if (table == CLDRFile.TERRITORY_NAME)
missingItems.removeAll(language_territories.keySet());
} else {
throw new IllegalArgumentException("Illegal code");
}
Set<String> missingItemsNamed = new TreeSet<String>(col);
for (Iterator<String> it = missingItems.iterator(); it.hasNext(); ) {
String item = it.next();
List<String> data = sc.getFullData(type, item);
if (data.get(0).equals("PRIVATE USE")) continue;
if (data.size() < 3) continue;
if (!"".equals(data.get(2))) continue;
NameType nameType = NameType.fromCldrInt(source);
String itemName = getName(nameType, item, true);
missingItemsNamed.add(itemName);
}
pw.println("<div align='center'><table>");
for (Iterator<String> it = missingItemsNamed.iterator(); it.hasNext(); ) {
pw.println("<tr><td class='target'>" + it.next() + "</td></tr>");
}
pw.println("</table></div>");
}

// source, eg english.TERRITORY_NAME
// target, eg english.LANGUAGE_NAME
public void print(PrintWriter pw, int source, int target) {
Multimap<String, String> data =
source == CLDRFile.TERRITORY_NAME && target == CLDRFile.LANGUAGE_NAME
? territory_languages
: source == CLDRFile.LANGUAGE_NAME && target == CLDRFile.TERRITORY_NAME
? language_territories
: source == CLDRFile.SCRIPT_NAME
&& target == CLDRFile.LANGUAGE_NAME
? script_languages
: source == CLDRFile.LANGUAGE_NAME
&& target == CLDRFile.SCRIPT_NAME
? language_scripts
: null;
// transform into names, and sort
Map<String, Set<String>> territory_languageNames =
new TreeMap<String, Set<String>>(col);
for (Iterator<String> it = data.keySet().iterator(); it.hasNext(); ) {
String territory = it.next();
String territoryName = getName(NameType.fromCldrInt(source), territory, true);
Set<String> s = territory_languageNames.get(territoryName);
if (s == null)
territory_languageNames.put(territoryName, s = new TreeSet<String>(col));
for (Iterator<String> it2 = data.get(territory).iterator(); it2.hasNext(); ) {
String language = it2.next();
String languageName = getName(NameType.fromCldrInt(target), language, true);
s.add(languageName);
}
}

pw.println("<div align='center'><table>");

for (Iterator<String> it = territory_languageNames.keySet().iterator();
it.hasNext(); ) {
String territoryName = it.next();
pw.println("<tr><td class='source' colspan='2'>" + territoryName + "</td></tr>");
Set<String> s = territory_languageNames.get(territoryName);
for (Iterator<String> it2 = s.iterator(); it2.hasNext(); ) {
String languageName = it2.next();
pw.println(
"<tr><td>&nbsp;</td><td class='target'>" + languageName + "</td></tr>");
}
}
pw.println("</table></div>");
}

private String getName(NameType nameType, String oldcode, boolean codeFirst) {
if (oldcode.contains(" ")) {
String[] result = oldcode.split("\\s+");
Expand Down Expand Up @@ -2957,14 +2866,14 @@ private static Map<String, Set<String>> getInverse(
}

static final Map<String, String> NAME_TO_REGION =
getNameToCode(CodeType.territory, CLDRFile.TERRITORY_NAME);
getNameToCode(CodeType.territory, NameType.TERRITORY);
static final Map<String, String> NAME_TO_CURRENCY =
getNameToCode(CodeType.currency, CLDRFile.CURRENCY_NAME);
getNameToCode(CodeType.currency, NameType.CURRENCY);

private static SortedMap<String, String> getNameToCode(CodeType codeType, int cldrCodeType) {
private static SortedMap<String, String> getNameToCode(CodeType codeType, NameType nameType) {
SortedMap<String, String> temp = new TreeMap<String, String>(col);
for (String territory : StandardCodes.make().getAvailableCodes(codeType)) {
String name = englishNameGetter.getNameFromTypenumCode(cldrCodeType, territory);
String name = englishNameGetter.getNameFromTypeEnumCode(nameType, territory);
temp.put(name == null ? territory : name, territory);
}
temp = Collections.unmodifiableSortedMap(temp);
Expand Down
Loading

0 comments on commit e79ee94

Please sign in to comment.