]> WPIA git - gigi.git/commitdiff
Implement localization messages.
authorFelix Dörre <felix@dogcraft.de>
Fri, 20 Jun 2014 18:22:29 +0000 (20:22 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 21 Jun 2014 14:41:28 +0000 (16:41 +0200)
.classpath
locale/.gitignore [new file with mode: 0644]
src/org/cacert/gigi/Language.java [new file with mode: 0644]
util/org/cacert/gigi/util/FetchLocales.java [new file with mode: 0644]

index 0fba86b0775db39f7ce4a9047b50948e78c78c21..cbb4307ebd4d2f149fc740296b691289b3c8ecd3 100644 (file)
@@ -3,6 +3,7 @@
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="lib/servlet-api"/>
        <classpathentry kind="src" path="lib/jetty"/>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="lib/servlet-api"/>
        <classpathentry kind="src" path="lib/jetty"/>
+       <classpathentry kind="src" path="util"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/locale/.gitignore b/locale/.gitignore
new file mode 100644 (file)
index 0000000..917eade
--- /dev/null
@@ -0,0 +1,3 @@
+*
+!.gitignore
+a
\ No newline at end of file
diff --git a/src/org/cacert/gigi/Language.java b/src/org/cacert/gigi/Language.java
new file mode 100644 (file)
index 0000000..431f0b3
--- /dev/null
@@ -0,0 +1,57 @@
+package org.cacert.gigi;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+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<String, Language> langs = new HashMap<String, Language>();
+       HashMap<String, String> translations = new HashMap<String, String>();
+       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");
+       }
+}
diff --git a/util/org/cacert/gigi/util/FetchLocales.java b/util/org/cacert/gigi/util/FetchLocales.java
new file mode 100644 (file)
index 0000000..3839e8b
--- /dev/null
@@ -0,0 +1,116 @@
+package org.cacert.gigi.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Scanner;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+public class FetchLocales {
+       public static final String DOWNLOAD_SERVER = "translations.cacert.org";
+       public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER
+                       + "/export/cacert/%/messages.po";
+       public static final String[] AUTO_LANGS = new String[]{"en", "de", "nl",
+                       "pt_BR", "fr", "sv", "it", "es", "hu", "fi", "ja", "bg", "pt",
+                       "da", "pl", "zh_CN", "ru", "lv", "cs", "zh_TW", "el", "tr", "ar"};
+       public static void main(String[] args) throws IOException,
+                       ParserConfigurationException, TransformerException {
+               File locale = new File("locale");
+               locale.mkdir();
+
+               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+               DocumentBuilder db = dbf.newDocumentBuilder();
+               for (String lang : AUTO_LANGS) {
+                       Document doc = db.newDocument();
+                       doc.appendChild(doc.createElement("translations"));
+                       URL fetch = new URL(PO_URL_TEMPLATE.replace("%", lang));
+                       URLConnection uc = fetch.openConnection();
+                       Scanner sc = new Scanner(uc.getInputStream());
+                       String s = readLine(sc);
+                       StringBuffer contents = new StringBuffer();
+                       String id = "";
+                       while (s != null) {
+                               if (s.startsWith("msgid")) {
+                                       contents.delete(0, contents.length());
+                                       s = readString(s, sc, contents);
+                                       id = contents.toString();
+                                       continue;
+                               } else if (s.startsWith("msgstr")) {
+                                       contents.delete(0, contents.length());
+                                       // System.out.println("msgstr");
+                                       s = readString(s, sc, contents);
+                                       String msg = contents.toString().replace("\\\"", "\"")
+                                                       .replace("\\n", "\n");
+                                       insertTranslation(doc, id, msg);
+                               } else if (s.startsWith("#")) {
+                                       // System.out.println(s);
+                               } else if (s.equals("") || s.equals("\r")) {
+
+                               } else {
+                                       System.out.println("unknown line: " + s);
+                               }
+                               s = readLine(sc);
+                       }
+                       TransformerFactory tFactory = TransformerFactory.newInstance();
+                       Transformer transformer = tFactory.newTransformer();
+
+                       DOMSource source = new DOMSource(doc);
+                       FileOutputStream fos = new FileOutputStream(new File(locale, lang
+                                       + ".xml"));
+                       StreamResult result = new StreamResult(fos);
+                       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+                       transformer.setOutputProperty(
+                                       "{http://xml.apache.org/xslt}indent-amount", "2");
+                       transformer.transform(source, result);
+                       fos.close();
+               }
+       }
+       private static String readLine(Scanner sc) {
+               String line = sc.findWithinHorizon("[^\n]*\n", 0);
+               if (line == null) {
+                       return null;
+               }
+               return line.substring(0, line.length() - 1);
+       }
+       private static void insertTranslation(Document doc, String id, String msg) {
+               Node idN = doc.createTextNode(id);
+               Node textN = doc.createTextNode(msg);
+               Element tr = doc.createElement("translation");
+               Element e = doc.createElement("id");
+               e.appendChild(idN);
+               tr.appendChild(e);
+               e = doc.createElement("msg");
+               e.appendChild(textN);
+               tr.appendChild(e);
+               doc.getDocumentElement().appendChild(tr);
+       }
+       private static String readString(String head, Scanner sc,
+                       StringBuffer contents) throws IOException {
+               head = head.split(" ", 2)[1];
+               contents.append(head.substring(1, head.length() - 1));
+               String s;
+               while ((s = readLine(sc)) != null) {
+                       if (!s.startsWith("\"")) {
+                               break;
+                       }
+                       contents.append(s.substring(1, s.length() - 1));
+               }
+               return s;
+       }
+
+}