Skip to content

Instantly share code, notes, and snippets.

@srl295
Created December 29, 2015 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srl295/f87d06a1405a23e85827 to your computer and use it in GitHub Desktop.
Save srl295/f87d06a1405a23e85827 to your computer and use it in GitHub Desktop.
sample to print out all locales in Africa
// 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);
}
}
@srl295
Copy link
Author

srl295 commented Dec 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment