1 package org.cacert.gigi;
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;
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>();
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]);
27 l = new Locale(language);
30 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
31 DocumentBuilder db = dbf.newDocumentBuilder();
32 Document d = db.parse(new FileInputStream(new File("locale", language
34 NodeList nl = d.getDocumentElement().getChildNodes();
35 for (int i = 0; i < nl.getLength(); i++) {
36 if (!(nl.item(i) instanceof Element)) {
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());
44 System.out.println(translations.size() + " strings loaded.");
46 public String getTranslation(String text) {
47 String string = translations.get(text);
48 if (string == null || string.equals("")) {
53 public static Language getInstance(String language) {
54 Language l = langs.get(language);
57 l = new Language(language);
58 langs.put(language, l);
59 } catch (ParserConfigurationException e) {
61 } catch (IOException e) {
63 } catch (SAXException e) {
69 public Locale getLocale() {