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;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
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;
19 public class Language {
21 private static HashMap<String, Language> langs = new HashMap<String, Language>();
23 HashMap<String, String> translations = new HashMap<String, String>();
27 private Language(String language) throws ParserConfigurationException, IOException, SAXException {
28 if (language.contains("_")) {
29 String[] parts = language.split("_");
30 l = new Locale(parts[0], parts[1]);
32 l = new Locale(language);
35 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
36 DocumentBuilder db = dbf.newDocumentBuilder();
37 Document d = db.parse(new FileInputStream(new File("locale", language + ".xml")));
38 NodeList nl = d.getDocumentElement().getChildNodes();
39 for (int i = 0; i < nl.getLength(); i++) {
40 if ( !(nl.item(i) instanceof Element)) {
43 Element e = (Element) nl.item(i);
44 Element id = (Element) e.getElementsByTagName("id").item(0);
45 Element msg = (Element) e.getElementsByTagName("msg").item(0);
46 translations.put(id.getTextContent(), HTMLEncoder.encodeHTML(msg.getTextContent()));
48 System.out.println(translations.size() + " strings loaded.");
51 public String getTranslation(String text) {
52 String string = translations.get(text);
53 if (string == null || string.equals("")) {
59 public static Language getInstance(String language) {
60 Language l = langs.get(language);
63 l = new Language(language);
64 langs.put(language, l);
65 } catch (ParserConfigurationException e) {
67 } catch (IOException e) {
69 } catch (SAXException e) {
76 public Locale getLocale() {