]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Language.java
HTMLEncode all language text before outputting it.
[gigi.git] / src / org / cacert / gigi / Language.java
1 package org.cacert.gigi;
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.Locale;
8
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
12
13 import org.cacert.gigi.util.HTMLEncoder;
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         private static HashMap<String, Language> langs = new HashMap<String, Language>();
21         HashMap<String, String> translations = new HashMap<String, String>();
22         Locale l;
23
24         private Language(String language) throws ParserConfigurationException, IOException, SAXException {
25                 if (language.contains("_")) {
26                         String[] parts = language.split("_");
27                         l = new Locale(parts[0], parts[1]);
28                 } else {
29                         l = new Locale(language);
30                 }
31
32                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
33                 DocumentBuilder db = dbf.newDocumentBuilder();
34                 Document d = db.parse(new FileInputStream(new File("locale", language + ".xml")));
35                 NodeList nl = d.getDocumentElement().getChildNodes();
36                 for (int i = 0; i < nl.getLength(); i++) {
37                         if (!(nl.item(i) instanceof Element)) {
38                                 continue;
39                         }
40                         Element e = (Element) nl.item(i);
41                         Element id = (Element) e.getElementsByTagName("id").item(0);
42                         Element msg = (Element) e.getElementsByTagName("msg").item(0);
43                         translations.put(id.getTextContent(), HTMLEncoder.encodeHTML(msg.getTextContent()));
44                 }
45                 System.out.println(translations.size() + " strings loaded.");
46         }
47
48         public String getTranslation(String text) {
49                 String string = translations.get(text);
50                 if (string == null || string.equals("")) {
51                         return text;
52                 }
53                 return string;
54         }
55
56         public static Language getInstance(String language) {
57                 Language l = langs.get(language);
58                 if (l == null) {
59                         try {
60                                 l = new Language(language);
61                                 langs.put(language, l);
62                         } catch (ParserConfigurationException e) {
63                                 e.printStackTrace();
64                         } catch (IOException e) {
65                                 e.printStackTrace();
66                         } catch (SAXException e) {
67                                 e.printStackTrace();
68                         }
69                 }
70                 return l;
71         }
72
73         public Locale getLocale() {
74                 return l;
75         }
76
77 }