import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
static {
LinkedList<Locale> supported = new LinkedList<>();
File locales = new File("locale");
- for (File f : locales.listFiles()) {
- if ( !f.getName().endsWith(".xml")) {
- continue;
+ File[] listFiles = locales.listFiles();
+ if (listFiles != null) {
+ for (File f : listFiles) {
+ if ( !f.getName().endsWith(".xml")) {
+ continue;
+ }
+ String language = f.getName().split("\\.", 2)[0];
+ supported.add(getLocaleFromString(language));
}
- String language = f.getName().split("\\.", 2)[0];
- supported.add(getLocaleFromString(language));
}
+ Collections.sort(supported, new Comparator<Locale>() {
+
+ @Override
+ public int compare(Locale o1, Locale o2) {
+ return o1.toString().compareTo(o2.toString());
+ }
+
+ });
supportedLocales = supported.toArray(new Locale[supported.size()]);
}
private HashMap<String, String> translations = new HashMap<String, String>();
- private Locale l;
+ private Locale locale;
- private static Locale project(Locale l) {
- if (l == null) {
+ private static Locale project(Locale locale) {
+ if (locale == null) {
return Locale.getDefault();
}
- File file = new File("locale", l.toString() + ".xml");
+ File file = new File("locale", locale.toString() + ".xml");
if ( !file.exists()) {
- return new Locale(l.getLanguage());
+ return new Locale(locale.getLanguage());
}
- return l;
+ return locale;
}
- protected Language(Locale loc) throws ParserConfigurationException, IOException, SAXException {
- File file = new File("locale", loc.toString() + ".xml");
- l = loc;
+ protected Language(Locale locale) throws ParserConfigurationException, IOException, SAXException {
+ File file = new File("locale", locale.toString() + ".xml");
+ this.locale = locale;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse(new FileInputStream(file));
return string;
}
- public static Language getInstance(Locale language) {
- language = project(language);
- File file = new File("locale", language.toString() + ".xml");
+ public static Language getInstance(Locale locale) {
+ locale = project(locale);
+ File file = new File("locale", locale.toString() + ".xml");
if ( !file.exists()) {
return null;
}
- Language l = langs.get(language.toString());
- if (l == null) {
+ synchronized (Language.class) {
+ Language lang = langs.get(locale.toString());
+ if (lang != null) {
+ return lang;
+ }
try {
- l = new Language(language);
- langs.put(language.toString(), l);
+ lang = new Language(locale);
+ langs.put(locale.toString(), lang);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (SAXException e) {
e.printStackTrace();
}
+ return lang;
}
- return l;
}
public Locale getLocale() {
- return l;
+ return locale;
}
}