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