]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/localisation/Language.java
FIX: syncing issues for Language initialisation.
[gigi.git] / src / org / cacert / gigi / localisation / Language.java
index dc99d9a8a6ace810037a99881f8a28a26436e55c..4c6e39012094281c3736c5e4a73649b93c6bbe80 100644 (file)
@@ -3,7 +3,10 @@ package org.cacert.gigi.localisation;
 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;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -17,23 +20,68 @@ import org.xml.sax.SAXException;
 
 public class Language {
 
-    private static HashMap<String, Language> langs = new HashMap<String, Language>();
+    public static final String SESSION_ATTRIB_NAME = "lang";
+
+    private static Locale[] supportedLocales;
 
-    HashMap<String, String> translations = new HashMap<String, String>();
+    static {
+        LinkedList<Locale> supported = new LinkedList<>();
+        File locales = new File("locale");
+        for (File f : locales.listFiles()) {
+            if ( !f.getName().endsWith(".xml")) {
+                continue;
+            }
+            String language = f.getName().split("\\.", 2)[0];
+            supported.add(getLocaleFromString(language));
+        }
+        Collections.sort(supported, new Comparator<Locale>() {
 
-    Locale l;
+            @Override
+            public int compare(Locale o1, Locale o2) {
+                return o1.toString().compareTo(o2.toString());
+            }
 
-    protected Language(String language) throws ParserConfigurationException, IOException, SAXException {
+        });
+        supportedLocales = supported.toArray(new Locale[supported.size()]);
+    }
+
+    public static Locale getLocaleFromString(String language) {
         if (language.contains("_")) {
-            String[] parts = language.split("_");
-            l = new Locale(parts[0], parts[1]);
+            String[] parts = language.split("_", 2);
+            return new Locale(parts[0], parts[1]);
+
         } else {
-            l = new Locale(language);
+            return new Locale(language);
         }
+    }
 
+    public static Locale[] getSupportedLocales() {
+        return supportedLocales;
+    }
+
+    private static HashMap<String, Language> langs = new HashMap<String, Language>();
+
+    private HashMap<String, String> translations = new HashMap<String, String>();
+
+    private Locale locale;
+
+    private static Locale project(Locale locale) {
+        if (locale == null) {
+            return Locale.getDefault();
+        }
+        File file = new File("locale", locale.toString() + ".xml");
+        if ( !file.exists()) {
+            return new Locale(locale.getLanguage());
+        }
+        return locale;
+    }
+
+    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(new File("locale", language + ".xml")));
+        Document d = db.parse(new FileInputStream(file));
         NodeList nl = d.getDocumentElement().getChildNodes();
         for (int i = 0; i < nl.getLength(); i++) {
             if ( !(nl.item(i) instanceof Element)) {
@@ -55,25 +103,36 @@ public class Language {
         return string;
     }
 
-    public static Language getInstance(String language) {
-        Language l = langs.get(language);
-        if (l == null) {
-            try {
-                l = new Language(language);
-                langs.put(language, l);
-            } catch (ParserConfigurationException e) {
-                e.printStackTrace();
-            } catch (IOException e) {
-                e.printStackTrace();
-            } catch (SAXException e) {
-                e.printStackTrace();
+    public static Language getInstance(Locale locale) {
+        locale = project(locale);
+        File file = new File("locale", locale.toString() + ".xml");
+        if ( !file.exists()) {
+            return null;
+        }
+        Language lang = langs.get(locale.toString());
+        if (lang == null) {
+            synchronized (Language.class) {
+                lang = langs.get(locale.toString());
+                if (lang != null) {
+                    return lang;
+                }
+                try {
+                    lang = new Language(locale);
+                    langs.put(locale.toString(), lang);
+                } catch (ParserConfigurationException e) {
+                    e.printStackTrace();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                } catch (SAXException e) {
+                    e.printStackTrace();
+                }
             }
         }
-        return l;
+        return lang;
     }
 
     public Locale getLocale() {
-        return l;
+        return locale;
     }
 
 }