X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fcacert%2Fgigi%2FLanguage.java;h=6c03b19ca59694020952ff79e3c6159eb96137e4;hb=1d24b8e1fe36651f22339ddbb7b1a3b34b48c081;hp=431f0b32a41989fa2ff87a4e45aae4eec47e218f;hpb=dd1ac40aa3d48cd24b9f0f0ec23a41fb5ef7c449;p=gigi.git diff --git a/src/org/cacert/gigi/Language.java b/src/org/cacert/gigi/Language.java index 431f0b32..6c03b19c 100644 --- a/src/org/cacert/gigi/Language.java +++ b/src/org/cacert/gigi/Language.java @@ -4,54 +4,77 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; +import java.util.Locale; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.cacert.gigi.util.HTMLEncoder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Language { - private static HashMap langs = new HashMap(); - HashMap translations = new HashMap(); - private Language(String language) throws ParserConfigurationException, - IOException, SAXException { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document d = db.parse(new FileInputStream(new File("locale", language - + ".xml"))); - NodeList nl = d.getDocumentElement().getChildNodes(); - for (int i = 0; i < nl.getLength(); i++) { - if (!(nl.item(i) instanceof Element)) { - continue; - } - Element e = (Element) nl.item(i); - Element id = (Element) e.getElementsByTagName("id").item(0); - Element msg = (Element) e.getElementsByTagName("msg").item(0); - translations.put(id.getTextContent(), msg.getTextContent()); - } - System.out.println(translations.size() + " strings loaded."); - } - public static Language getInstance(String language) { - Language l = langs.get(language); - if (l == null) { - try { - l = new Language(language); - langs.put(language, l); - } catch (ParserConfigurationException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (SAXException e) { - e.printStackTrace(); - } - } - return l; - } - public static void main(String[] args) { - Language.getInstance("de"); - } + + private static HashMap langs = new HashMap(); + + HashMap translations = new HashMap(); + + Locale l; + + private Language(String language) throws ParserConfigurationException, IOException, SAXException { + if (language.contains("_")) { + String[] parts = language.split("_"); + l = new Locale(parts[0], parts[1]); + } else { + l = new Locale(language); + } + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document d = db.parse(new FileInputStream(new File("locale", language + ".xml"))); + NodeList nl = d.getDocumentElement().getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + if ( !(nl.item(i) instanceof Element)) { + continue; + } + Element e = (Element) nl.item(i); + Element id = (Element) e.getElementsByTagName("id").item(0); + Element msg = (Element) e.getElementsByTagName("msg").item(0); + translations.put(id.getTextContent(), HTMLEncoder.encodeHTML(msg.getTextContent())); + } + System.out.println(translations.size() + " strings loaded."); + } + + public String getTranslation(String text) { + String string = translations.get(text); + if (string == null || string.equals("")) { + return text; + } + return string; + } + + public static Language getInstance(String language) { + Language l = langs.get(language); + if (l == null) { + try { + l = new Language(language); + langs.put(language, l); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } + } + return l; + } + + public Locale getLocale() { + return l; + } + }