1 package org.cacert.gigi.localisation;
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.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.NodeList;
16 import org.xml.sax.SAXException;
18 public class Language {
20 private static HashMap<String, Language> langs = new HashMap<String, Language>();
22 private HashMap<String, String> translations = new HashMap<String, String>();
26 protected Language(String language) throws ParserConfigurationException, IOException, SAXException {
27 if (language.contains("_")) {
28 String[] parts = language.split("_");
29 l = new Locale(parts[0], parts[1]);
31 l = new Locale(language);
34 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
35 DocumentBuilder db = dbf.newDocumentBuilder();
36 Document d = db.parse(new FileInputStream(new File("locale", language + ".xml")));
37 NodeList nl = d.getDocumentElement().getChildNodes();
38 for (int i = 0; i < nl.getLength(); i++) {
39 if ( !(nl.item(i) instanceof Element)) {
42 Element e = (Element) nl.item(i);
43 Element id = (Element) e.getElementsByTagName("id").item(0);
44 Element msg = (Element) e.getElementsByTagName("msg").item(0);
45 translations.put(id.getTextContent(), msg.getTextContent());
47 System.out.println(translations.size() + " strings loaded.");
50 public String getTranslation(String text) {
51 String string = translations.get(text);
52 if (string == null || string.equals("")) {
58 public static Language getInstance(String language) {
59 Language l = langs.get(language);
62 l = new Language(language);
63 langs.put(language, l);
64 } catch (ParserConfigurationException e) {
66 } catch (IOException e) {
68 } catch (SAXException e) {
75 public Locale getLocale() {