Example usage for java.text Collator getAvailableLocales

List of usage examples for java.text Collator getAvailableLocales

Introduction

In this page you can find the example usage for java.text Collator getAvailableLocales.

Prototype

public static synchronized Locale[] getAvailableLocales() 

Source Link

Document

Returns an array of all locales for which the getInstance methods of this class can return localized instances.

Usage

From source file:net.wastl.webmail.storage.FileStorage.java

protected void initLanguages() {
    log.info("Initializing available languages ... ");
    File f = new File(parent.getProperty("webmail.template.path") + System.getProperty("file.separator"));
    String[] flist = f.list(new FilenameFilter() {
        public boolean accept(File myf, String s) {
            if (myf.isDirectory() && s.equals(s.toLowerCase()) && (s.length() == 2 || s.equals("default"))) {
                return true;
            } else {
                return false;
            }/*from   w w  w . ja va 2s  .  co  m*/
        }
    });

    File cached = new File(
            parent.getProperty("webmail.data.path") + System.getProperty("file.separator") + "locales.cache");
    Locale[] available1 = null;

    /* Now we try to cache the Locale list since it takes really long to gather it! */
    boolean exists = cached.exists();
    if (exists) {
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(cached));
            available1 = (Locale[]) in.readObject();
            in.close();
            log.info("Using disk cache for langs... ");
        } catch (Exception ex) {
            exists = false;
        }
    }
    if (!exists) {
        // We should cache this on disk since it is so slow!
        available1 = Collator.getAvailableLocales();
        ObjectOutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(cached));
            os.writeObject(available1);
        } catch (IOException ioe) {
            log.error("Failed to write to storage", ioe);
        } finally {
            try {
                os.close();
            } catch (IOException ioe) {
                log.error("Failed to close stream", ioe);
            }
        }
    }

    // Do this manually, as it is not JDK 1.1 compatible ...
    //Vector available=new Vector(Arrays.asList(available1));
    Vector<Locale> available = new Vector<Locale>(available1.length);
    for (int i = 0; i < available1.length; i++) {
        available.addElement(available1[i]);
    }
    String s = "";
    int count = 0;
    for (int i = 0; i < flist.length; i++) {
        String cur_lang = flist[i];
        Locale loc = new Locale(cur_lang, "", "");
        Enumeration<Locale> enumVar = available.elements();
        boolean added = false;
        while (enumVar.hasMoreElements()) {
            Locale l = (Locale) enumVar.nextElement();
            if (l.getLanguage().equals(loc.getLanguage())) {
                s += l.toString() + " ";
                count++;
                added = true;
            }
        }
        if (!added) {
            s += loc.toString() + " ";
            count++;
        }
    }
    log.info(count + " languages initialized.");
    cs.configRegisterStringKey(this, "LANGUAGES", s, "Languages available in WebMail");
    setConfig("LANGUAGES", s);

    /*
      Setup list of themes for each language
    */
    for (int j = 0; j < flist.length; j++) {
        File themes = new File(parent.getProperty("webmail.template.path")
                + System.getProperty("file.separator") + flist[j] + System.getProperty("file.separator"));
        String[] themelist = themes.list(new FilenameFilter() {
            public boolean accept(File myf, String s3) {
                if (myf.isDirectory() && !s3.equals("CVS")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        String s2 = "";
        for (int k = 0; k < themelist.length; k++) {
            s2 += themelist[k] + " ";
        }
        cs.configRegisterStringKey(this, "THEMES_" + flist[j].toUpperCase(), s2,
                "Themes for language " + flist[j]);
        setConfig("THEMES_" + flist[j].toUpperCase(), s2);
    }
}

From source file:org.kitodo.dataeditor.ruleset.Labeled.java

/**
 * Lists the entries alphabetically by the translated label, taking into
 * account the language preferred by the user for alphabetical sorting. In
 * the auxiliary map, the keys are sorted by the sequence translated label +
 * separator + key, to account for the case where multiple keys are
 * (inadvertently) translated equally.//  ww w  .j a v  a 2s.  c o m
 *
 * @param ruleset
 *            The ruleset. From the ruleset, we learn which language is a
 *            label that does not have a language attribute, that is what is
 *            the default language of the rule set.
 * @param elements
 *            The items to sort. The function is so generic that you can
 *            sort divisions, keys, and options with it.
 * @param keyGetter
 *            A function to extract from the element the value used in the
 *            result map as key.
 * @param labelGetter
 *            A function that allows you to extract the list of labels from
 *            the element and then select the best translated one.
 * @param priorityList
 *            The list of languages spoken by the user human
 * @return a map in the order of the best-fitting translated label
 */
public static <T> LinkedHashMap<String, String> listByTranslatedLabel(Ruleset ruleset, Collection<T> elements,
        Function<T, String> keyGetter, Function<T, Collection<Label>> labelGetter,
        List<LanguageRange> priorityList) {

    Locale sortLocale = Locale.lookup(priorityList, Arrays.asList(Collator.getAvailableLocales()));
    TreeMap<String, Pair<String, String>> byLabelSorter = sortLocale != null
            ? new TreeMap<>(Collator.getInstance(sortLocale))
            : new TreeMap<>();
    for (T element : elements) {
        String key = keyGetter.apply(element);
        Labeled labeled = new Labeled(ruleset, key, labelGetter.apply(element));
        String label = labeled.getLabel(priorityList);
        byLabelSorter.put(label + '\037' + key, Pair.of(key, label));
    }
    LinkedHashMap<String, String> result = new LinkedHashMap<>((int) Math.ceil(elements.size() / 0.75));
    for (Pair<String, String> entry : byLabelSorter.values()) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}