]> WPIA git - gigi.git/blob - src/org/cacert/gigi/localisation/Language.java
FIX: syncing issues for Language initialisation.
[gigi.git] / src / org / cacert / gigi / localisation / Language.java
1 package org.cacert.gigi.localisation;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashMap;
9 import java.util.LinkedList;
10 import java.util.Locale;
11
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19 import org.xml.sax.SAXException;
20
21 public class Language {
22
23     public static final String SESSION_ATTRIB_NAME = "lang";
24
25     private static Locale[] supportedLocales;
26
27     static {
28         LinkedList<Locale> supported = new LinkedList<>();
29         File locales = new File("locale");
30         for (File f : locales.listFiles()) {
31             if ( !f.getName().endsWith(".xml")) {
32                 continue;
33             }
34             String language = f.getName().split("\\.", 2)[0];
35             supported.add(getLocaleFromString(language));
36         }
37         Collections.sort(supported, new Comparator<Locale>() {
38
39             @Override
40             public int compare(Locale o1, Locale o2) {
41                 return o1.toString().compareTo(o2.toString());
42             }
43
44         });
45         supportedLocales = supported.toArray(new Locale[supported.size()]);
46     }
47
48     public static Locale getLocaleFromString(String language) {
49         if (language.contains("_")) {
50             String[] parts = language.split("_", 2);
51             return new Locale(parts[0], parts[1]);
52
53         } else {
54             return new Locale(language);
55         }
56     }
57
58     public static Locale[] getSupportedLocales() {
59         return supportedLocales;
60     }
61
62     private static HashMap<String, Language> langs = new HashMap<String, Language>();
63
64     private HashMap<String, String> translations = new HashMap<String, String>();
65
66     private Locale locale;
67
68     private static Locale project(Locale locale) {
69         if (locale == null) {
70             return Locale.getDefault();
71         }
72         File file = new File("locale", locale.toString() + ".xml");
73         if ( !file.exists()) {
74             return new Locale(locale.getLanguage());
75         }
76         return locale;
77     }
78
79     protected Language(Locale locale) throws ParserConfigurationException, IOException, SAXException {
80         File file = new File("locale", locale.toString() + ".xml");
81         this.locale = locale;
82         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
83         DocumentBuilder db = dbf.newDocumentBuilder();
84         Document d = db.parse(new FileInputStream(file));
85         NodeList nl = d.getDocumentElement().getChildNodes();
86         for (int i = 0; i < nl.getLength(); i++) {
87             if ( !(nl.item(i) instanceof Element)) {
88                 continue;
89             }
90             Element e = (Element) nl.item(i);
91             Element id = (Element) e.getElementsByTagName("id").item(0);
92             Element msg = (Element) e.getElementsByTagName("msg").item(0);
93             translations.put(id.getTextContent(), msg.getTextContent());
94         }
95         System.out.println(translations.size() + " strings loaded.");
96     }
97
98     public String getTranslation(String text) {
99         String string = translations.get(text);
100         if (string == null || string.equals("")) {
101             return text;
102         }
103         return string;
104     }
105
106     public static Language getInstance(Locale locale) {
107         locale = project(locale);
108         File file = new File("locale", locale.toString() + ".xml");
109         if ( !file.exists()) {
110             return null;
111         }
112         Language lang = langs.get(locale.toString());
113         if (lang == null) {
114             synchronized (Language.class) {
115                 lang = langs.get(locale.toString());
116                 if (lang != null) {
117                     return lang;
118                 }
119                 try {
120                     lang = new Language(locale);
121                     langs.put(locale.toString(), lang);
122                 } catch (ParserConfigurationException e) {
123                     e.printStackTrace();
124                 } catch (IOException e) {
125                     e.printStackTrace();
126                 } catch (SAXException e) {
127                     e.printStackTrace();
128                 }
129             }
130         }
131         return lang;
132     }
133
134     public Locale getLocale() {
135         return locale;
136     }
137
138 }