Implement localization messages.
[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
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         private Language(String language) throws ParserConfigurationException,
21                         IOException, SAXException {
22                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
23                 DocumentBuilder db = dbf.newDocumentBuilder();
24                 Document d = db.parse(new FileInputStream(new File("locale", language
25                                 + ".xml")));
26                 NodeList nl = d.getDocumentElement().getChildNodes();
27                 for (int i = 0; i < nl.getLength(); i++) {
28                         if (!(nl.item(i) instanceof Element)) {
29                                 continue;
30                         }
31                         Element e = (Element) nl.item(i);
32                         Element id = (Element) e.getElementsByTagName("id").item(0);
33                         Element msg = (Element) e.getElementsByTagName("msg").item(0);
34                         translations.put(id.getTextContent(), msg.getTextContent());
35                 }
36                 System.out.println(translations.size() + " strings loaded.");
37         }
38         public static Language getInstance(String language) {
39                 Language l = langs.get(language);
40                 if (l == null) {
41                         try {
42                                 l = new Language(language);
43                                 langs.put(language, l);
44                         } catch (ParserConfigurationException e) {
45                                 e.printStackTrace();
46                         } catch (IOException e) {
47                                 e.printStackTrace();
48                         } catch (SAXException e) {
49                                 e.printStackTrace();
50                         }
51                 }
52                 return l;
53         }
54         public static void main(String[] args) {
55                 Language.getInstance("de");
56         }
57 }