Skip to content

Commit

Permalink
CLDR-15830 Refactor getName: new class NameGetter (unicode-org#4226)
Browse files Browse the repository at this point in the history
  • Loading branch information
btangmu authored Dec 6, 2024
1 parent 9c044e1 commit d4fffd7
Show file tree
Hide file tree
Showing 118 changed files with 1,341 additions and 884 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.unicode.cldr.util.Counter2;
import org.unicode.cldr.util.Factory;
import org.unicode.cldr.util.LanguageTagParser;
import org.unicode.cldr.util.NameGetter;
import org.unicode.cldr.util.StandardCodes;
import org.unicode.cldr.util.SupplementalDataInfo;
import org.unicode.cldr.util.SupplementalDataInfo.OfficialStatus;
Expand Down Expand Up @@ -44,8 +45,9 @@ private void run() throws IOException {
// Counter2<String> langToGDP = new Counter2<String>();
LanguageTagParser ltp = new LanguageTagParser();
Map<String, String> languageNameToCode = new TreeMap<>();
NameGetter nameGetter = english.nameGetter();
for (String languageCode : info.getLanguages()) {
languageNameToCode.put(english.getName(languageCode), languageCode);
languageNameToCode.put(nameGetter.getNameFromBCP47(languageCode), languageCode);
}
out.println("\n@sheet:CLDR County Data");
out.println("code\tgdp\tlit-pop\tpopulation\tliteracy");
Expand All @@ -71,7 +73,7 @@ private void run() throws IOException {
Map<String, Counter2<String>> langToCountriesOfficial = new TreeMap<>();

for (String languageCode : info.getLanguages()) {
String languageName = english.getName(languageCode);
String languageName = nameGetter.getNameFromBCP47(languageCode);

String baseLanguage = languageCode;
ltp.set(languageCode);
Expand All @@ -90,7 +92,8 @@ private void run() throws IOException {
// );
for (String territory : territories) {
PopulationData terrData = info.getPopulationDataForTerritory(territory);
String territoryName = english.getName(CLDRFile.TERRITORY_NAME, territory);
String territoryName =
nameGetter.getNameFromTypenumCode(CLDRFile.TERRITORY_NAME, territory);

PopulationData data =
info.getLanguageAndTerritoryPopulationData(languageCode, territory);
Expand Down Expand Up @@ -156,7 +159,7 @@ private void run() throws IOException {
}
// for (String language :langToPopulation.keySet()) {
// out.println(
// english.getName(language)
// english.nameGetter().getName(language)
// + "\t" + language
// + "\t" + langToPopulation.getCount(language)
// + "\t" + langToGDP.getCount(language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ private static void showDefaultContent(String... strings) {
break;
}
}
System.out.println(string + "\t" + defCon + "\t" + english.getName(defCon));
System.out.println(
string + "\t" + defCon + "\t" + english.nameGetter().getNameFromBCP47(defCon));
}
}

Expand Down Expand Up @@ -328,7 +329,7 @@ private static void showExemplarSize() {
if (temp != null) {
baseLanguage = temp.get0().get(0);
}
String englishName = english.getName(baseLanguage);
String englishName = english.nameGetter().getNameFromBCP47(baseLanguage);
CLDRFile cldrFile = factory.make(baseLanguage, false);
UnicodeSet set = cldrFile.getExemplarSet("", WinningChoice.WINNING);
int script = -1;
Expand All @@ -339,7 +340,7 @@ private static void showExemplarSize() {
break;
}
}
String nativeName = cldrFile.getName(baseLanguage);
String nativeName = cldrFile.nameGetter().getNameFromBCP47(baseLanguage);
nameAndInfo.add(
englishName
+ "\t"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,21 +969,29 @@ private static String fixedName(List<String> match) {
if (region.equals("*") || region.startsWith("$")) {
result.append(region);
} else {
result.append(english.getName(CLDRFile.TERRITORY_NAME, region));
result.append(
english.nameGetter()
.getNameFromTypenumCode(CLDRFile.TERRITORY_NAME, region));
}
case 2:
String script = alt.get(1);
if (script.equals("*")) {
result.insert(0, script);
} else {
result.insert(0, english.getName(CLDRFile.TERRITORY_NAME, script));
result.insert(
0,
english.nameGetter()
.getNameFromTypenumCode(CLDRFile.TERRITORY_NAME, script));
}
case 1:
String language = alt.get(0);
if (language.equals("*")) {
result.insert(0, language);
} else {
result.insert(0, english.getName(CLDRFile.TERRITORY_NAME, language));
result.insert(
0,
english.nameGetter()
.getNameFromTypenumCode(CLDRFile.TERRITORY_NAME, language));
}
}
return Joiner.on("; ").join(alt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public String getLocaleName(String locale) {
String name = localeNameCache.get(locale);
if (name != null) return name;
if (english == null) english = cldrFactory.make("en", true);
String result = english.getName(locale);
String result = english.nameGetter().getNameFromBCP47(locale);
/*
* Collection c = Utility.splitList(locale, '_', false, null);
* String[] pieces = new String[c.size()];
Expand Down Expand Up @@ -1047,7 +1047,11 @@ public Object transform(Object source) {
@Override
public Object transform(Object source) {
if (english == null) english = cldrFactory.make("en", true);
return english.getName("currency", source.toString()) + " (" + source + ")";
return english.nameGetter()
.getNameFromTypestrCode("currency", source.toString())
+ " ("
+ source
+ ")";
}
};

Expand Down Expand Up @@ -1189,7 +1193,7 @@ public void TestSupplementalData() {
logln("Missing English currency names");
for (Iterator<String> it = legalCurrencies.iterator(); it.hasNext(); ) {
String currency = it.next();
String name = english.getName("currency", currency);
String name = english.nameGetter().getNameFromTypestrCode("currency", currency);
if (name == null) {
String standardName = sc.getFullData("currency", currency).get(0);
logln("\t\t\t<currency type=\"" + currency + "\">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,8 @@ private static String getEnglishPathValue(String path) {
* @return
*/
private static String getLocaleAndName(String locale) {
String localizedName = CheckCLDR.getDisplayInformation().getName(locale);
String localizedName =
CheckCLDR.getDisplayInformation().nameGetter().getNameFromBCP47(locale);
if (localizedName == null || localizedName.equals(locale)) return locale;
return locale + " [" + localizedName + "]";
}
Expand All @@ -1998,7 +1999,8 @@ private static String getLocaleAndName(String locale) {
* @return
*/
private static String getNameAndLocale(String locale, boolean linkToXml) {
String localizedName = CheckCLDR.getDisplayInformation().getName(locale);
String localizedName =
CheckCLDR.getDisplayInformation().nameGetter().getNameFromBCP47(locale);
if (localizedName == null || localizedName.equals(locale)) return locale;
if (linkToXml) {
locale =
Expand All @@ -2012,7 +2014,8 @@ private static String getNameAndLocale(String locale, boolean linkToXml) {
}

private static String getLocaleName(String locale) {
String localizedName = CheckCLDR.getDisplayInformation().getName(locale);
String localizedName =
CheckCLDR.getDisplayInformation().nameGetter().getNameFromBCP47(locale);
if (localizedName == null || localizedName.equals(locale)) return locale;
return localizedName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.unicode.cldr.util.EmojiConstants;
import org.unicode.cldr.util.LanguageGroup;
import org.unicode.cldr.util.LocaleIDParser;
import org.unicode.cldr.util.NameGetter;
import org.unicode.cldr.util.Organization;
import org.unicode.cldr.util.StandardCodes;

Expand Down Expand Up @@ -130,6 +131,7 @@ public static void main(String[] args) {
System.out.println();
CLDRFile english = CLDRConfig.getInstance().getEnglish();
Set<String> locales = new HashSet<>();
NameGetter nameGetter = english.nameGetter();
for (String filename : SUBDIVISION_FILE_NAMES) {
if (filename.endsWith(".xml")) {
String locale = filename.substring(0, filename.length() - 4);
Expand All @@ -140,7 +142,13 @@ public static void main(String[] args) {
LanguageGroup group = LanguageGroup.get(ulocale);
int rank = LanguageGroup.rankInGroup(ulocale);
System.out.print(
group + "\t" + rank + "\t" + english.getName(locale) + "\t" + locale);
group
+ "\t"
+ rank
+ "\t"
+ nameGetter.getNameFromBCP47(locale)
+ "\t"
+ locale);
for (String sd : SUBDIVISIONS) {
System.out.print('\t');
System.out.print(map.get(sd));
Expand All @@ -161,7 +169,13 @@ public static void main(String[] args) {
LanguageGroup group = LanguageGroup.get(ulocale);
int rank = LanguageGroup.rankInGroup(ulocale);
System.out.println(
group + "\t" + rank + "\t" + english.getName(locale) + "\t" + locale);
group
+ "\t"
+ rank
+ "\t"
+ english.nameGetter().getNameFromBCP47(locale)
+ "\t"
+ locale);
}

// System.out.println(getSubdivisionIdToName("fr"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.unicode.cldr.util.ICUServiceBuilder;
import org.unicode.cldr.util.LanguageTagParser;
import org.unicode.cldr.util.Level;
import org.unicode.cldr.util.NameGetter;
import org.unicode.cldr.util.PathDescription;
import org.unicode.cldr.util.PatternCache;
import org.unicode.cldr.util.PluralSamples;
Expand Down Expand Up @@ -752,11 +753,12 @@ private void handlePersonName(XPathParts parts, String value, List<String> examp
final CLDRFile cldrFile2 = getCldrFile();
switch (parts.getElement(2)) {
case "nameOrderLocales":
NameGetter nameGetter2 = cldrFile2.nameGetter();
for (String localeId : PersonNameFormatter.SPLIT_SPACE.split(value)) {
final String name =
localeId.equals("und")
? "«any other»"
: cldrFile2.getName(localeId);
: nameGetter2.getNameFromBCP47(localeId);
examples.add(localeId + " = " + name);
}
break;
Expand Down Expand Up @@ -2408,12 +2410,22 @@ private void handleTimeZoneName(XPathParts parts, String value, List<String> exa
return; // fail, skip
}
} else {
result = setBackground(cldrFile.getName(CLDRFile.TERRITORY_NAME, countryCode));
result =
setBackground(
cldrFile.nameGetter()
.getNameFromTypenumCode(
CLDRFile.TERRITORY_NAME, countryCode));
}
} else if (parts.contains("zone")) { // {0} Time
result = value; // trivial -- is this beneficial?
} else if (parts.contains("regionFormat")) { // {0} Time
result = format(value, setBackground(cldrFile.getName(CLDRFile.TERRITORY_NAME, "JP")));
result =
format(
value,
setBackground(
cldrFile.nameGetter()
.getNameFromTypenumCode(
CLDRFile.TERRITORY_NAME, "JP")));
result =
addExampleResult(
format(
Expand Down Expand Up @@ -3101,10 +3113,11 @@ private void handleDisplayNames(
element.equals("localeKeyTypePattern") ? "uz-Arab-u-tz-etadd" : "uz-Arab-AF");
locales.add("uz-Arab-AF-u-tz-etadd-nu-arab");
// String[] examples = new String[locales.size()];
NameGetter nameGetter = cldrFile.nameGetter();
for (int i = 0; i < locales.size(); i++) {
examples.add(
invertBackground(
cldrFile.getName(
nameGetter.getNameFromBCP47Etc(
locales.get(i),
false,
localeKeyTypePattern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static void checkPaths() {
DisplayAndInputProcessor displayAndInputProcessor =
new DisplayAndInputProcessor(file, false);

System.out.println(locale + "\t-\t" + english.getName(locale));
System.out.println(locale + "\t-\t" + english.nameGetter().getNameFromBCP47(locale));
DtdType dtdType = null;

for (Iterator<String> it = file.iterator(); it.hasNext(); ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.unicode.cldr.util.Iso639Data;
import org.unicode.cldr.util.Iso639Data.Scope;
import org.unicode.cldr.util.Level;
import org.unicode.cldr.util.NameGetter;
import org.unicode.cldr.util.Pair;
import org.unicode.cldr.util.PatternCache;
import org.unicode.cldr.util.SimpleFactory;
Expand Down Expand Up @@ -236,8 +237,9 @@ private static void testWeights() {
int vote = Level.getDefaultWeight("google", desiredLocale);
rel.add(new Pair<>(vote, desiredLocale));
}
NameGetter nameGetter = english.nameGetter();
for (Pair<Integer, String> p : rel) {
System.out.println(p + "\t" + english.getName(p.getSecond()));
System.out.println(p + "\t" + nameGetter.getNameFromBCP47(p.getSecond()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.unicode.cldr.util.CLDRPaths;
import org.unicode.cldr.util.CldrUtility;
import org.unicode.cldr.util.Factory;
import org.unicode.cldr.util.NameGetter;
import org.unicode.cldr.util.Pair;
import org.unicode.cldr.util.PatternCache;
import org.unicode.cldr.util.StandardCodes;
Expand Down Expand Up @@ -152,6 +153,7 @@ static void checkAgainstLanguageScript() {
}
}
// compare them, listing differences
NameGetter nameGetter = english.nameGetter();
for (String territory : sc.getGoodAvailableCodes("territory")) {
Set<String> languages = supplementalData.getTerritoryToLanguages(territory);
Set<String> otherLanguages = otherTerritoryToLanguages.getAll(territory);
Expand All @@ -161,10 +163,11 @@ static void checkAgainstLanguageScript() {
languagesLeftover.removeAll(otherLanguages);
Set<String> otherLanguagesLeftover = new TreeSet<>(otherLanguages);
otherLanguagesLeftover.removeAll(languages);
String territoryString = english.getName(CLDRFile.TERRITORY_NAME, territory);
String territoryString =
nameGetter.getNameFromTypenumCode(CLDRFile.TERRITORY_NAME, territory);
if (otherLanguagesLeftover.size() != 0) {
for (String other : otherLanguagesLeftover) {
String name = english.getName(other);
String name = nameGetter.getNameFromBCP47(other);
System.out.println(
territoryString + "\t" + territory + "\t" + name + "\t" + other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.unicode.cldr.util.LanguageGroup;
import org.unicode.cldr.util.LanguageTagParser;
import org.unicode.cldr.util.LocaleIDParser;
import org.unicode.cldr.util.NameGetter;

public class ChartAnnotations extends Chart {

Expand Down Expand Up @@ -159,6 +160,7 @@ public void writeSubcharts(Anchors anchors) throws IOException {
Multimap<String, String> localeToSub = TreeMultimap.create();
LanguageTagParser ltp = new LanguageTagParser();

NameGetter nameGetter = ENGLISH.nameGetter();
for (String locale : locales) {
ltp.set(locale);
if (locale.equals("root")) {
Expand All @@ -176,7 +178,7 @@ public void writeSubcharts(Anchors anchors) throws IOException {
if (locale.startsWith("en")) {
int debug = 0;
}
String name = ENGLISH.getName(locale, true);
String name = nameGetter.getNameFromBCP47Bool(locale, true);
int baseEnd = locale.indexOf('_');
ULocale loc = new ULocale(baseEnd < 0 ? locale : locale.substring(0, baseEnd));
LanguageGroup group = LanguageGroup.get(loc);
Expand All @@ -187,7 +189,7 @@ public void writeSubcharts(Anchors anchors) throws IOException {
for (Entry<LanguageGroup, Set<R3<Integer, String, String>>> groupPairs :
groupToNameAndCodeSorted.keyValuesSet()) {
LanguageGroup group = groupPairs.getKey();
String ename = ENGLISH.getName("en", true);
String ename = nameGetter.getNameFromBCP47Bool("en", true);
nameToCode.clear();
nameToCode.put(ename, "en"); // always have english first

Expand Down Expand Up @@ -300,7 +302,8 @@ public static int getRegionalIndicator(int firstCodepoint) {
// int ri1 = getRegionalIndicator(cp.codePointAt(0));
// if (ri1 >= 0) {
// int ri2 = getRegionalIndicator(cp.codePointAt(2));
// return ENGLISH.getName(CLDRFile.TERRITORY_NAME, String.valueOf((char) ri1) +
// return ENGLISH.nameGetter().getName(CLDRFile.TERRITORY_NAME, String.valueOf((char)
// ri1) +
// String.valueOf((char) ri2));
// }
// String result = NAMES80.get(cp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ public void writeSubcharts(Anchors anchors) throws IOException {
if (!data.containsKey("standard")) {
addCollator(data, "standard", (RuleBasedCollator) null);
}
new Subchart(ENGLISH.getName(locale, true, CLDRFile.SHORT_ALTS), locale, data)
new Subchart(
ENGLISH.nameGetter()
.getNameFromBCP47BoolAlt(locale, true, CLDRFile.SHORT_ALTS),
locale,
data)
.writeChart(anchors);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void writeContents(FormattedFileWriter pw) throws IOException {
tablePrinter
.addRow()
.addCell(group)
.addCell(ENGLISH.getName(locale))
.addCell(ENGLISH.nameGetter().getNameFromBCP47(locale))
.addCell(locale)
// .addCell(type)
.addCell(df.format(time))
Expand Down
Loading

0 comments on commit d4fffd7

Please sign in to comment.