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 File[] listFiles = locales.listFiles();
31 if (listFiles != null) {
32 for (File f : listFiles) {
33 if ( !f.getName().endsWith(".xml")) {
36 String language = f.getName().split("\\.", 2)[0];
37 supported.add(getLocaleFromString(language));
40 Collections.sort(supported, new Comparator<Locale>() {
43 public int compare(Locale o1, Locale o2) {
44 return o1.toString().compareTo(o2.toString());
48 supportedLocales = supported.toArray(new Locale[supported.size()]);
51 public static Locale getLocaleFromString(String language) {
52 if (language.contains("_")) {
53 String[] parts = language.split("_", 2);
54 return new Locale(parts[0], parts[1]);
57 return new Locale(language);
61 public static Locale[] getSupportedLocales() {
62 return supportedLocales;
65 private static HashMap<String, Language> langs = new HashMap<String, Language>();
67 private HashMap<String, String> translations = new HashMap<String, String>();
69 private Locale locale;
71 private static Locale project(Locale locale) {
73 return Locale.getDefault();
75 File file = new File("locale", locale.toString() + ".xml");
76 if ( !file.exists()) {
77 return new Locale(locale.getLanguage());
82 protected Language(Locale locale) throws ParserConfigurationException, IOException, SAXException {
83 File file = new File("locale", locale.toString() + ".xml");
85 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
86 DocumentBuilder db = dbf.newDocumentBuilder();
87 Document d = db.parse(new FileInputStream(file));
88 NodeList nl = d.getDocumentElement().getChildNodes();
89 for (int i = 0; i < nl.getLength(); i++) {
90 if ( !(nl.item(i) instanceof Element)) {
93 Element e = (Element) nl.item(i);
94 Element id = (Element) e.getElementsByTagName("id").item(0);
95 Element msg = (Element) e.getElementsByTagName("msg").item(0);
96 translations.put(id.getTextContent(), msg.getTextContent());
98 System.out.println(translations.size() + " strings loaded.");
101 public String getTranslation(String text) {
102 String string = translations.get(text);
103 if (string == null || string.equals("")) {
109 public static Language getInstance(Locale locale) {
110 locale = project(locale);
111 File file = new File("locale", locale.toString() + ".xml");
112 if ( !file.exists()) {
115 synchronized (Language.class) {
116 Language lang = langs.get(locale.toString());
121 lang = new Language(locale);
122 langs.put(locale.toString(), lang);
123 } catch (ParserConfigurationException e) {
125 } catch (IOException e) {
127 } catch (SAXException e) {
134 public Locale getLocale() {