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