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