* 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;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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;
import java.util.Comparator;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
class Language {
@XmlAttribute(name = "name")
private String name;
@XmlAttribute(name = "code")
private String code;
protected Language() {
// used by JAXB
}
private final String code;
private final String name;
public Language(String name, String code) {
this.name = name;
public Language(String code, String name) {
this.code = code;
}
public String getName() {
return name;
this.name = name;
}
@ -44,9 +25,8 @@ class Language {
}
@Override
public Language clone() {
return new Language(name, code);
public String getName() {
return name;
}
@ -56,50 +36,47 @@ class Language {
}
public static Language getLanguage(String languageCode) {
for (Language language : Languages.getInstance().elements()) {
if (language.getCode().equalsIgnoreCase(languageCode))
return language;
@Override
public Language clone() {
return new Language(code, name);
}
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;
}
public static List<Language> availableLanguages() {
return Collections.unmodifiableList(Arrays.asList(Languages.getInstance().elements()));
}
@XmlRootElement(name = "languages")
private static class Languages {
public static Language[] availableLanguages() {
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
Set<String> languageCodeSet = bundle.keySet();
@XmlElement(name = "language")
private Language[] elements;
Language[] languages = new Language[languageCodeSet.size()];
int size = 0;
// 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;
// fill languages array
for (String code : languageCodeSet) {
languages[size++] = new Language(code, bundle.getString(code));
}
public Language[] elements() {
return elements;
}
return languages;
}
}

View File

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

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>