* store available languages in .properties file instead of a .xml file because JAXB class-loading takes ~300ms

This commit is contained in:
Reinhard Pointner 2009-05-31 16:38:13 +00:00
parent ffc6a8b931
commit 5b9931ba45
4 changed files with 85 additions and 114 deletions

View File

@ -2,40 +2,21 @@
package net.sourceforge.filebot.ui.panel.subtitle; package net.sourceforge.filebot.ui.panel.subtitle;
import java.util.Arrays; import java.util.Comparator;
import java.util.Collections; import java.util.MissingResourceException;
import java.util.List; import java.util.ResourceBundle;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
class Language { class Language {
@XmlAttribute(name = "name") private final String code;
private String name; private final String name;
@XmlAttribute(name = "code")
private String code;
protected Language() { public Language(String code, String name) {
// used by JAXB
}
public Language(String name, String code) {
this.name = name;
this.code = code; this.code = code;
} this.name = name;
public String getName() {
return name;
} }
@ -44,9 +25,8 @@ class Language {
} }
@Override public String getName() {
public Language clone() { return name;
return new Language(name, code);
} }
@ -56,50 +36,47 @@ class Language {
} }
public static Language getLanguage(String languageCode) { @Override
for (Language language : Languages.getInstance().elements()) { public Language clone() {
if (language.getCode().equalsIgnoreCase(languageCode)) return new Language(code, name);
return language; }
public static final Comparator<Language> ALPHABETIC_ORDER = new Comparator<Language>() {
@Override
public int compare(Language o1, Language o2) {
return o1.name.compareToIgnoreCase(o2.name);
}
};
public static Language getLanguage(String code) {
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
try {
return new Language(code, bundle.getString(code));
} catch (MissingResourceException e) {
// ignore
} }
return null; return null;
} }
public static List<Language> availableLanguages() { public static Language[] availableLanguages() {
return Collections.unmodifiableList(Arrays.asList(Languages.getInstance().elements())); ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
} Set<String> languageCodeSet = bundle.keySet();
Language[] languages = new Language[languageCodeSet.size()];
int size = 0;
@XmlRootElement(name = "languages") // fill languages array
private static class Languages { for (String code : languageCodeSet) {
languages[size++] = new Language(code, bundle.getString(code));
@XmlElement(name = "language")
private Language[] elements;
// keep singleton instance of all available languages
private static Languages instance;
public static Languages getInstance() {
if (instance == null) {
try {
Unmarshaller unmarshaller = JAXBContext.newInstance(Languages.class).createUnmarshaller();
// load languages from xml files
return (Languages) unmarshaller.unmarshal(Language.class.getResource("languages.xml"));
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
return instance;
} }
return languages;
public Language[] elements() {
return elements;
}
} }
} }

View File

@ -51,11 +51,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
// guess favorite languages // guess favorite languages
if (languageModel.favorites().isEmpty()) { if (languageModel.favorites().isEmpty()) {
for (Locale locale : new Locale[] { Locale.getDefault(), Locale.ENGLISH }) { for (Locale locale : new Locale[] { Locale.getDefault(), Locale.ENGLISH }) {
Language language = Language.getLanguage(locale.getLanguage()); languageModel.favorites().add(Language.getLanguage(locale.getLanguage()));
if (language != null) {
languageModel.favorites().add(language);
}
} }
} }

View File

@ -0,0 +1,43 @@
# available languages
sq: Albanian
ar: Arabic
hy: Armenian
pb: Brazilian
bg: Bulgarian
ca: Catalan
zh: Chinese
hr: Croatian
cs: Czech
da: Danish
nl: Dutch
en: English
et: Estonian
fi: Finnish
fr: French
de: German
el: Greek
he: Hebrew
hi: Hindi
hu: Hungarian
id: Indonesian
it: Italian
ja: Japanese
ko: Korean
lv: Latvian
lt: Lithuanian
mk: Macedonian
ms: Malay
no: Norwegian
fa: Persian
pl: Polish
pt: Portuguese
ro: Romanian
ru: Russian
sr: Serbian
sk: Slovak
sl: Slovenian
es: Spanish
sv: Swedish
th: Thai
tr: Turkish
vi: Vietnamese

View File

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<languages>
<language code="sq" name="Albanian" />
<language code="ar" name="Arabic" />
<language code="hy" name="Armenian" />
<language code="pb" name="Brazilian" />
<language code="bg" name="Bulgarian" />
<language code="ca" name="Catalan" />
<language code="zh" name="Chinese" />
<language code="hr" name="Croatian" />
<language code="cs" name="Czech" />
<language code="da" name="Danish" />
<language code="nl" name="Dutch" />
<language code="en" name="English" />
<language code="et" name="Estonian" />
<language code="fi" name="Finnish" />
<language code="fr" name="French" />
<language code="de" name="German" />
<language code="el" name="Greek" />
<language code="he" name="Hebrew" />
<language code="hi" name="Hindi" />
<language code="hu" name="Hungarian" />
<language code="id" name="Indonesian" />
<language code="it" name="Italian" />
<language code="ja" name="Japanese" />
<language code="ko" name="Korean" />
<language code="lv" name="Latvian" />
<language code="lt" name="Lithuanian" />
<language code="mk" name="Macedonian" />
<language code="ms" name="Malay" />
<language code="no" name="Norwegian" />
<language code="fa" name="Persian" />
<language code="pl" name="Polish" />
<language code="pt" name="Portuguese" />
<language code="ro" name="Romanian" />
<language code="ru" name="Russian" />
<language code="sr" name="Serbian" />
<language code="sk" name="Slovak" />
<language code="sl" name="Slovenian" />
<language code="es" name="Spanish" />
<language code="sv" name="Swedish" />
<language code="th" name="Thai" />
<language code="tr" name="Turkish" />
<language code="vi" name="Vietnamese" />
</languages>