1 package org.unicode.cldr.web;
2 
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.TreeMap;
8 
9 import org.unicode.cldr.util.CLDRLocale;
10 import org.unicode.cldr.util.CLDRLocale.CLDRFormatter;
11 
12 import com.ibm.icu.text.RuleBasedCollator;
13 
14 public class LocaleTree {
15     CLDRFormatter displayLocale;
16 
LocaleTree(CLDRLocale.CLDRFormatter nf)17     public LocaleTree(CLDRLocale.CLDRFormatter nf) {
18         this.displayLocale = nf;
19     }
20 
add(CLDRLocale localeName)21     public void add(CLDRLocale localeName) {
22         localeNameToCode.put(getLocaleDisplayName(localeName), (localeName));
23         addLocaleToListMap((localeName));
24     }
25 
26     // TODO: object
27     /**
28      * TreeMap of all locales.
29      *
30      * localeListMap = TreeMap [ (String langScriptDisplayName) , (String
31      * localecode) ] subLocales = Hashtable [ localecode, TreeMap ] --> TreeMap
32      * [ langScriptDisplayName, String localeCode ] example
33      *
34      * localeListMap English -> en Serbian -> sr Serbian (Cyrillic) -> sr_Cyrl
35      * sublocales en -> [ "English (US)" -> en_US ], [ "English (Australia)" ->
36      * en_AU ] ... sr -> "Serbian (Yugoslavia)" -> sr_YU
37      */
38 
39     Map<String, CLDRLocale> localeListMap = new TreeMap<>(RuleBasedCollator.getInstance());
40     Map<String, CLDRLocale> localeNameToCode = new HashMap<>();
41     Map<CLDRLocale, Map<String, CLDRLocale>> subLocales = new HashMap<>();
42 
addLocaleToListMap(CLDRLocale localeName)43     private void addLocaleToListMap(CLDRLocale localeName) {
44         String l = localeName.getLanguage();
45         if ((l != null) && (l.length() == 0)) {
46             l = null;
47         }
48         String s = localeName.getScript();
49         if ((s != null) && (s.length() == 0)) {
50             s = null;
51         }
52         String t = localeName.getCountry();
53         if ((t != null) && (t.length() == 0)) {
54             t = null;
55         }
56         String v = localeName.getVariant();
57         if ((v != null) && (v.length() == 0)) {
58             v = null;
59         }
60 
61         if (l == null) {
62             return; // no language??
63         }
64 
65         String ls = ((s == null) ? l : (l + "_" + s)); // language and script
66         CLDRLocale lsl = CLDRLocale.getInstance(ls);
67 
68         localeListMap.put(getLocaleDisplayName(lsl), lsl);
69 
70         Map<String, CLDRLocale> lm = subLocales.get(lsl);
71         if (lm == null) {
72             lm = new TreeMap<>();
73             subLocales.put(lsl, lm);
74         }
75 
76         if (t != null || v != null) {
77             if (v == null) {
78                 lm.put(localeName.getDisplayCountry(displayLocale), localeName);
79             } else if (t != null) {
80                 lm.put(localeName.getDisplayCountry(displayLocale) + " (" + localeName.getDisplayVariant(displayLocale) + ")",
81                     localeName);
82             } else {
83                 lm.put("(" + localeName.getDisplayVariant(displayLocale) + ")",
84                     localeName);
85             }
86         }
87     }
88 
getLocaleCode(String localeName)89     public CLDRLocale getLocaleCode(String localeName) {
90         CLDRLocale loc = localeNameToCode.get(localeName);
91         if (loc == null) {
92             System.err.println("Cannot find locale for name '" + localeName + "'");
93             return CLDRLocale.ROOT;
94         }
95         return loc;
96     }
97 
getLocaleDisplayName(CLDRLocale locale)98     public String getLocaleDisplayName(CLDRLocale locale) {
99         return displayLocale.getDisplayName(locale, true, null);
100     }
101 
getMap()102     public Map<String, CLDRLocale> getMap() {
103         return localeListMap;
104     }
105 
106     /**
107      * @return a list of the 'top' locales (language, or language_script)
108      */
getTopLocales()109     public Set<String> getTopLocales() {
110         return localeListMap.keySet();
111     }
112 
getTopCLDRLocales()113     public Collection<CLDRLocale> getTopCLDRLocales() {
114         return localeListMap.values();
115     }
116 
getSubLocales(CLDRLocale locale)117     public Map<String, CLDRLocale> getSubLocales(CLDRLocale locale) {
118         return subLocales.get(locale);
119     }
120 
121 }
122