1 package org.cacert.gigi.localisation;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashMap;
9 import java.util.LinkedList;
10 import java.util.Locale;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19 import org.xml.sax.SAXException;
21 public class Language {
23 public static final String SESSION_ATTRIB_NAME = "lang";
25 private static Locale[] supportedLocales;
28 LinkedList<Locale> supported = new LinkedList<>();
29 File locales = new File("locale");
30 for (File f : locales.listFiles()) {
31 if ( !f.getName().endsWith(".xml")) {
34 String language = f.getName().split("\\.", 2)[0];
35 supported.add(getLocaleFromString(language));
37 Collections.sort(supported, new Comparator<Locale>() {
40 public int compare(Locale o1, Locale o2) {
41 return o1.toString().compareTo(o2.toString());
45 supportedLocales = supported.toArray(new Locale[supported.size()]);
48 public static Locale getLocaleFromString(String language) {
49 if (language.contains("_")) {
50 String[] parts = language.split("_", 2);
51 return new Locale(parts[0], parts[1]);
54 return new Locale(language);
58 public static Locale[] getSupportedLocales() {
59 return supportedLocales;
62 private static HashMap<String, Language> langs = new HashMap<String, Language>();
64 private HashMap<String, String> translations = new HashMap<String, String>();
68 private static Locale project(Locale l) {
70 return Locale.getDefault();
72 File file = new File("locale", l.toString() + ".xml");
73 if ( !file.exists()) {
74 return new Locale(l.getLanguage());
79 protected Language(Locale loc) throws ParserConfigurationException, IOException, SAXException {
80 File file = new File("locale", loc.toString() + ".xml");
82 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
83 DocumentBuilder db = dbf.newDocumentBuilder();
84 Document d = db.parse(new FileInputStream(file));
85 NodeList nl = d.getDocumentElement().getChildNodes();
86 for (int i = 0; i < nl.getLength(); i++) {
87 if ( !(nl.item(i) instanceof Element)) {
90 Element e = (Element) nl.item(i);
91 Element id = (Element) e.getElementsByTagName("id").item(0);
92 Element msg = (Element) e.getElementsByTagName("msg").item(0);
93 translations.put(id.getTextContent(), msg.getTextContent());
95 System.out.println(translations.size() + " strings loaded.");
98 public String getTranslation(String text) {
99 String string = translations.get(text);
100 if (string == null || string.equals("")) {
106 public static Language getInstance(Locale language) {
107 language = project(language);
108 File file = new File("locale", language.toString() + ".xml");
109 if ( !file.exists()) {
112 Language l = langs.get(language.toString());
115 l = new Language(language);
116 langs.put(language.toString(), l);
117 } catch (ParserConfigurationException e) {
119 } catch (IOException e) {
121 } catch (SAXException e) {
128 public Locale getLocale() {