11package org .unicode .cldr .json ;
22
3- import java .io .BufferedReader ;
43import java .io .File ;
54import java .io .IOException ;
65import java .io .PrintWriter ;
4443import com .ibm .icu .number .NumberFormatter ;
4544import com .ibm .icu .number .Precision ;
4645import com .ibm .icu .util .NoUnit ;
46+ import com .ibm .icu .util .ULocale ;
4747
4848/**
4949 * Utility methods to extract data from CLDR repository and export it in JSON
@@ -61,7 +61,7 @@ public class Ldml2JsonConverter {
6161 enum RunType {
6262 all ,
6363 main ,
64- supplemental (false , false ), // aka 'core'
64+ supplemental (false , false ), // aka 'cldr- core'
6565 segments , rbnf (false , true ), annotations , annotationsDerived , bcp47 (false , false );
6666
6767 private final boolean isTiered ;
@@ -146,7 +146,8 @@ private class AvailableLocales {
146146 .add ("identity" , 'i' , "(true|false)" , "true" ,
147147 "Whether to copy the identity info into all sections containing data" )
148148 .add ("konfig" , 'k' , ".*" , null , "LDML to JSON configuration file" )
149- .add ("pkgversion" , 'V' , ".*" , getDefaultVersion (), "Version to be used in writing package files" );
149+ .add ("pkgversion" , 'V' , ".*" , getDefaultVersion (), "Version to be used in writing package files" )
150+ .add ("Modern" , 'M' , "(true|false)" , "true" , "Whether to include the -modern tier" );
150151
151152 public static void main (String [] args ) throws Exception {
152153 options .parse (args , true );
@@ -186,7 +187,8 @@ static void processType(final String runType) throws Exception {
186187 options .get ("konfig" ).getValue (),
187188 options .get ("pkgversion" ).getValue (),
188189 Boolean .parseBoolean (options .get ("bcp47" ).getValue ()),
189- Boolean .parseBoolean (options .get ("bcp47-no-subtags" ).getValue ())
190+ Boolean .parseBoolean (options .get ("bcp47-no-subtags" ).getValue ()),
191+ Boolean .parseBoolean (options .get ("Modern" ).getValue ())
190192 );
191193
192194 DraftStatus status = DraftStatus .valueOf (options .get ("draftstatus" ).getValue ());
@@ -226,12 +228,14 @@ public int compareTo(JSONSection other) {
226228 private Set <String > packages ;
227229 final private String pkgVersion ;
228230 final private boolean strictBcp47 ;
231+ final private boolean writeModernPackage ;
229232 final private boolean skipBcp47LocalesWithSubtags ;
230233 private LdmlConfigFileReader configFileReader ;
231234
232235 public Ldml2JsonConverter (String cldrDir , String outputDir , String runType , boolean fullNumbers , boolean resolve , String coverage , String match ,
233236 boolean writePackages , String configFile , String pkgVersion ,
234- boolean strictBcp47 , boolean skipBcp47LocalesWithSubtags ) {
237+ boolean strictBcp47 , boolean skipBcp47LocalesWithSubtags , boolean writeModernPackage ) {
238+ this .writeModernPackage = writeModernPackage ;
235239 this .strictBcp47 = strictBcp47 ;
236240 this .skipBcp47LocalesWithSubtags = strictBcp47 && skipBcp47LocalesWithSubtags ;
237241 this .cldrCommonDir = cldrDir ;
@@ -432,7 +436,7 @@ private Map<JSONSection, List<CldrItem>> mapPathsToSections(AtomicInteger readCo
432436
433437 // discard draft before transforming
434438 final String pathNoDraft = CLDRFile .DRAFT_PATTERN .matcher (path ).replaceAll ("" );
435- final String fullPathNoDraft = CLDRFile .DRAFT_PATTERN .matcher (fullPath ).replaceAll ("" );
439+ final String fullPathNoDraft = CLDRFile .DRAFT_PATTERN .matcher (fullPath ).replaceAll ("" );
436440
437441 final String pathNoXmlSpace = CLDRFile .XML_SPACE_PATTERN .matcher (pathNoDraft ).replaceAll ("" );
438442 final String fullPathNoXmlSpace = CLDRFile .XML_SPACE_PATTERN .matcher (fullPathNoDraft ).replaceAll ("" );
@@ -559,7 +563,7 @@ private int convertCldrItems(AtomicInteger readCount, int totalCount,
559563 continue ;
560564 }
561565 final boolean isModernTier = localeIsModernTier (filename );
562- if (isModernTier ) {
566+ if (isModernTier && writeModernPackage ) {
563567 tier = MODERN_TIER_SUFFIX ;
564568 if (type == RunType .main ) {
565569 avl .modern .add (filenameAsLangTag );
@@ -598,9 +602,9 @@ private int convertCldrItems(AtomicInteger readCount, int totalCount,
598602 List <String > outputDirs = new ArrayList <>();
599603 outputDirs .add (outputDirname .toString ());
600604 if (writePackages && tier .equals (MODERN_TIER_SUFFIX ) && js .packageName != null ) {
601- // if it is in 'modern', add it to 'full' also.
605+ // if it is in 'modern', add it to 'full' and core also.
602606 outputDirs .add (outputDirname .toString ().replaceFirst (MODERN_TIER_SUFFIX , FULL_TIER_SUFFIX ));
603- // Also need to make sure that the full package is added
607+ // Also need to make sure that the full and core package is added
604608 packages .add (CLDR_PKG_PREFIX + js .packageName + FULL_TIER_SUFFIX );
605609 }
606610
@@ -746,12 +750,15 @@ private int convertCldrItems(AtomicInteger readCount, int totalCount,
746750 }
747751
748752 private boolean localeIsModernTier (String filename ) {
749- boolean isModernTier ;
750- {
751- final Level localeCoverageLevel = sc .getHighestLocaleCoverageLevel ("Cldr" , filename );
752- isModernTier = (localeCoverageLevel .getLevel () >= Level .MODERN .getLevel ()) || filename .equals (LocaleNames .ROOT ) || filename .equals (LocaleNames .UND );
753- }
754- return isModernTier ;
753+ Level lev = CalculatedCoverageLevels .getInstance ().getEffectiveCoverageLevel (filename );
754+ if (lev == null ) return false ;
755+ return lev .isAtLeast (Level .MODERN );
756+ }
757+
758+ private boolean localeIsBasicTier (String filename ) {
759+ Level lev = CalculatedCoverageLevels .getInstance ().getEffectiveCoverageLevel (filename );
760+ if (lev == null ) return false ;
761+ return lev .isAtLeast (Level .BASIC );
755762 }
756763
757764 /**
@@ -965,11 +972,10 @@ public void writeReadme(String outputDir, String packageName) throws IOException
965972 outf .println (configFileReader .getPackageDescriptions ().get (basePackageName ));
966973 outf .println ();
967974 if (packageName .endsWith (FULL_TIER_SUFFIX )) {
968- outf .println ("This package contains the complete set of locales, including what is in the `" +
969- CLDR_PKG_PREFIX + basePackageName + MODERN_TIER_SUFFIX + "` package." );
975+ outf .println ("This package contains all locales." );
970976 outf .println ();
971977 } else if (packageName .endsWith (MODERN_TIER_SUFFIX )) {
972- outf .println ("This package contains the set of locales listed as modern coverage. See also the `" +
978+ outf .println ("This package contains only the set of locales listed as modern coverage. See also the `" +
973979 CLDR_PKG_PREFIX + basePackageName + FULL_TIER_SUFFIX + "` package." );
974980 outf .println ();
975981 }
@@ -1047,7 +1053,7 @@ public void writePackageJson(String outputDir, String packageName) throws IOExce
10471053 final String basePackageName = getBasePackageName (packageName );
10481054 String description = configFileReader .getPackageDescriptions ().get (basePackageName );
10491055 if (packageName .endsWith (FULL_TIER_SUFFIX )) {
1050- description = description + " (complete )" ;
1056+ description = description + " (all locales )" ;
10511057 } else if (packageName .endsWith (MODERN_TIER_SUFFIX )) {
10521058 description = description + " (modern coverage locales)" ;
10531059 }
@@ -1119,32 +1125,33 @@ public void writeDefaultContent(String outputDir) throws IOException {
11191125
11201126 public void writeCoverageLevels (String outputDir ) throws IOException {
11211127 final Splitter SEMICOLON = Splitter .on (';' ).trimResults ();
1122- try (BufferedReader r = FileUtilities .openUTF8Reader (CLDRPaths .COMMON_DIRECTORY + "properties/" , "coverageLevels.txt" );
1123- PrintWriter outf = FileUtilities .openUTF8Writer (outputDir + "/cldr-core" , "coverageLevels.json" );) {
1124- final Map <String , String > covlocs = new TreeMap <>();
1125- System .out .println ("Creating packaging file => " + outputDir + "/cldr-core" + File .separator + "coverageLevels.json from coverageLevels.txt" );
1126- String line ;
1127- int no = 0 ;
1128- while ((line = r .readLine ()) != null ) {
1129- no ++;
1130- line = line .trim ();
1131- if (line .startsWith ("#" ) || line .isBlank ()) {
1132- continue ;
1133- }
1134- final List <String > l = SEMICOLON .splitToList (line );
1135- if (l .size () != 2 ) {
1136- throw new IllegalArgumentException ("coverageLevels.txt:" +no +": expected 2 fields, got " + l .size ());
1137- }
1138- final String uloc = l .get (0 );
1139- final String level = l .get (1 );
1140- final String bcp47loc = unicodeLocaleToString (uloc );
1141- if (covlocs .put (bcp47loc , level ) != null ) {
1142- throw new IllegalArgumentException ("coverageLevels.txt:" +no +": duplicate locale " + bcp47loc );
1143- }
1128+ try (PrintWriter outf = FileUtilities .openUTF8Writer (outputDir + "/cldr-core" , "coverageLevels.json" );) {
1129+ final Map <String , String > covlocs = new TreeMap <>();
1130+ System .out .println ("Creating packaging file => " + outputDir + "/cldr-core" + File .separator + "coverageLevels.json from coverageLevels.txt" );
1131+ CalculatedCoverageLevels ccl = CalculatedCoverageLevels .getInstance ();
1132+ for (final Map .Entry <String , org .unicode .cldr .util .Level > e : ccl .getLevels ().entrySet ()) {
1133+ final String uloc = e .getKey ();
1134+ final String level = e .getValue ().name ().toLowerCase ();
1135+ final String bcp47loc = unicodeLocaleToString (uloc );
1136+ if (covlocs .put (bcp47loc , level ) != null ) {
1137+ throw new IllegalArgumentException ("coverageLevels.txt: duplicate locale " + bcp47loc );
1138+ }
1139+ }
1140+ final Map <String , String > effectiveCovlocs = new TreeMap <>();
1141+ avl .full .forEach (loc -> {
1142+ final String uloc = ULocale .forLanguageTag (loc ).toString ();
1143+ final Level lev = ccl .getEffectiveCoverageLevel (uloc );
1144+ if (lev != null ) {
1145+ effectiveCovlocs .put (loc , lev .name ().toLowerCase ());
11441146 }
1145- JsonObject obj = new JsonObject ();
1146- obj .add ("coverageLevels" , gson .toJsonTree (covlocs ));
1147- outf .println (gson .toJson (obj ));
1147+ });
1148+ JsonObject obj = new JsonObject ();
1149+ // exactly what is in CLDR .txt file
1150+ obj .add ("coverageLevels" , gson .toJsonTree (covlocs ));
1151+
1152+ // resolved, including all available locales
1153+ obj .add ("effectiveCoverageLevels" , gson .toJsonTree (effectiveCovlocs ));
1154+ outf .println (gson .toJson (obj ));
11481155 }
11491156 }
11501157
@@ -1213,15 +1220,23 @@ public void writePackageList(String outputDir) throws IOException {
12131220 } else {
12141221 {
12151222 JsonObject packageEntry = new JsonObject ();
1216- packageEntry .addProperty ("description" , e .getValue () + " (full)" );
1223+ packageEntry .addProperty ("description" , e .getValue () + " (basic)" );
1224+ packageEntry .addProperty ("tier" , "full" );
1225+ packageEntry .addProperty ("name" , CLDR_PKG_PREFIX + baseName + FULL_TIER_SUFFIX );
1226+ packages .add (packageEntry );
1227+ pkgsToDesc .put (packageEntry .get ("name" ).getAsString (), packageEntry .get ("description" ).getAsString ());
1228+ }
1229+ {
1230+ JsonObject packageEntry = new JsonObject ();
1231+ packageEntry .addProperty ("description" , e .getValue () + " (basic)" );
12171232 packageEntry .addProperty ("tier" , "full" );
12181233 packageEntry .addProperty ("name" , CLDR_PKG_PREFIX + baseName + FULL_TIER_SUFFIX );
12191234 packages .add (packageEntry );
12201235 pkgsToDesc .put (packageEntry .get ("name" ).getAsString (), packageEntry .get ("description" ).getAsString ());
12211236 }
12221237 {
12231238 JsonObject packageEntry = new JsonObject ();
1224- packageEntry .addProperty ("description" , e .getValue () + " (modern only )" );
1239+ packageEntry .addProperty ("description" , e .getValue () + " (modern)" );
12251240 packageEntry .addProperty ("tier" , "modern" );
12261241 packageEntry .addProperty ("name" , CLDR_PKG_PREFIX + baseName + MODERN_TIER_SUFFIX );
12271242 packages .add (packageEntry );
@@ -1716,7 +1731,7 @@ public void processDirectory(String dirName, DraftStatus minimalDraftStatus)
17161731 }
17171732 return new Pair <>(dirName + "/" + filename , totalForThisFile );
17181733 })
1719- .filter (p -> p .getSecond () == 0 )
1734+ .filter (p -> p .getSecond () == 0 ) // filter out only files which produced no output
17201735 .map (p -> p .getFirst ())
17211736 .toArray ();
17221737 System .out .println (progressPrefix (total , total ) + " Completed parallel process of " + total + " file(s)" );
0 commit comments