Created
December 29, 2015 17:50
-
-
Save srl295/f87d06a1405a23e85827 to your computer and use it in GitHub Desktop.
sample to print out all locales in Africa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// by srl | |
// use: ( cd ${HOME}/cldr-trunk/tools/java ; ant jar ) ; javac -classpath ${HOME}/cldr-trunk/tools/java/cldr.jar AfriLang.java && java -DCLDR_DIR=\ | |
${HOME}/src/cldr-trunk -classpath ${HOME}/cldr-trunk/tools/java/cldr.jar:. AfriLang | |
import com.ibm.icu.impl.Relation; | |
import java.util.LinkedHashMap; | |
import java.util.LinkedHashSet; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import org.unicode.cldr.util.*; | |
public class AfriLang { | |
public static final CLDRConfig config = CLDRConfig.getInstance(); | |
public static final Factory f = config.getFullCldrFactory(); | |
public static final Set<CLDRLocale> ALL_LOCS = f.getAvailableCLDRLocales(); | |
public static final SupplementalDataInfo SUPP = config.getSupplementalDataInfo(); | |
public static final Relation<String,String> terrToContained = SUPP.getTerritoryToContained(); | |
public static final Relation<String,String> containedToTerr = Relation.of(new LinkedHashMap<String, Set<String>>(), | |
LinkedHashSet.class).addAllInverted(terrToContained); | |
/** | |
* Is this locale in Africa? | |
*/ | |
public static boolean isAfrica(final CLDRLocale l) { | |
final String rgn = l.getCountry(); | |
if(rgn == null) return false; // no country | |
return isAfricaRegion(rgn); | |
} | |
public static boolean isAfricaRegion(final String rgn) { | |
final Set<String> pars = containedToTerr.get(rgn); | |
if(pars == null) return false; | |
if(pars.contains("002")) return true; // Africa | |
for(final String par : pars) { | |
if(isAfricaRegion(par)) return true; | |
} | |
return false; | |
} | |
public static void printSorted(final Set<CLDRLocale> list) { | |
final Set<String> str = new TreeSet<String>(java.text.Collator.getInstance()); | |
for(final CLDRLocale l : list) { | |
Factory.SourceTreeType type = Factory.getSourceTreeType(f.getSourceDirectoryForLocale(l.toString())); | |
str.add(l.getDisplayName()+" - "+l.toLanguageTag() + " " + | |
((type!=Factory.SourceTreeType.common)?("["+type.toString().toUpperCase()+"]"):"")); | |
} | |
for(final String s: str) { | |
System.out.println(s); | |
} | |
} | |
public static void main(String args[]) { | |
// use the English CLDRfile for format | |
CLDRLocale.setDefaultFormatter(new CLDRLocale.CLDRFormatter(config.getEnglish())); | |
final Set<CLDRLocale> afriLocs = new LinkedHashSet<CLDRLocale>(); | |
for(final CLDRLocale l : ALL_LOCS) { | |
if(isAfrica(l)) { | |
afriLocs.add(l); | |
} | |
} | |
printSorted(afriLocs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: https://gist.github.com/srl295/b109b01ab7f63e0959b0