1 package org.cacert.gigi;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.HashMap;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.NodeList;
15 import org.xml.sax.SAXException;
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
26 NodeList nl = d.getDocumentElement().getChildNodes();
27 for (int i = 0; i < nl.getLength(); i++) {
28 if (!(nl.item(i) instanceof Element)) {
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());
36 System.out.println(translations.size() + " strings loaded.");
38 public static Language getInstance(String language) {
39 Language l = langs.get(language);
42 l = new Language(language);
43 langs.put(language, l);
44 } catch (ParserConfigurationException e) {
46 } catch (IOException e) {
48 } catch (SAXException e) {
54 public static void main(String[] args) {
55 Language.getInstance("de");