upd: get translations from new translation server
authorFelix Dörre <felix@dogcraft.de>
Fri, 10 Jun 2016 15:58:47 +0000 (17:58 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 11 Jun 2016 17:59:29 +0000 (19:59 +0200)
+ empty english lang-file

Change-Id: I5f8e2e12654c0d4240237d6fdb36282418d41aad

build.xml
doc/scripts/gigi
util/org/cacert/gigi/util/FetchLocales.java

index 209f1be..be1b127 100644 (file)
--- a/build.xml
+++ b/build.xml
                <java classname="org.cacert.gigi.util.FetchLocales" failonerror="true"
                        fork="yes">
                        <classpath refid="cacert-gigi.classpath" />
+                       <arg value="${localePath}"/>
                </java>
        </target>
        <target name="check-generateKeys">
index dd09c69..14bccc4 100755 (executable)
@@ -34,7 +34,11 @@ then
        java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.DatabaseManager
 elif [ "$1" == "fetch-locales" ]
 then
-       java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.FetchLocales
+    if [ "$2" == "" ]; then
+               java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.FetchLocales
+       else
+               java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.FetchLocales "$2"
+       fi
 elif [ "$1" == "signer-conf" ]
 then
        mkdir /var/lib/cacert-gigi/config
index aed8973..f3097af 100644 (file)
@@ -1,9 +1,11 @@
 package org.cacert.gigi.util;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.Scanner;
@@ -13,8 +15,10 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
@@ -24,15 +28,18 @@ import org.w3c.dom.Node;
 
 public class FetchLocales {
 
-    public static final String DOWNLOAD_SERVER = "translations.cacert.org";
+    public static final String DOWNLOAD_SERVER = "pootle.cacert1.dogcraft.de";
 
-    public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER + "/export/cacert/%/messages.po";
+    public static String PO_URL_TEMPLATE = "https://" + DOWNLOAD_SERVER + "/%/gigi/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"
+        "de"
     };
 
     public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
+        if (args.length != 0) {
+            PO_URL_TEMPLATE = args[0];
+        }
         System.out.println("downloading locales ...");
         File locale = new File("locale");
         if ( !locale.isDirectory() && !locale.mkdir()) {
@@ -44,47 +51,65 @@ public class FetchLocales {
         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(new InputStreamReader(uc.getInputStream(), "UTF-8"));
-            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();
+
+            int count = addTranslationsFromPo(doc, new URL(PO_URL_TEMPLATE.replace("%", lang)));
+
+            System.out.println("Strings for language " + lang + ": " + count);
+
+            writeTranslationToFile(doc, new File(locale, lang + ".xml"));
         }
+        Document doc = db.newDocument();
+        doc.appendChild(doc.createElement("translations"));
+        System.out.println("Creating empty en.xml");
+        writeTranslationToFile(doc, new File(locale, "en.xml"));
         System.out.println("Done.");
     }
 
+    private static void writeTranslationToFile(Document doc, File file) throws TransformerFactoryConfigurationError, TransformerConfigurationException, FileNotFoundException, TransformerException, IOException {
+        TransformerFactory tFactory = TransformerFactory.newInstance();
+        Transformer transformer = tFactory.newTransformer();
+
+        DOMSource source = new DOMSource(doc);
+        FileOutputStream fos = new FileOutputStream(file);
+        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 int addTranslationsFromPo(Document doc, URL fetch) throws IOException, UnsupportedEncodingException {
+        URLConnection uc = fetch.openConnection();
+        Scanner sc = new Scanner(new InputStreamReader(uc.getInputStream(), "UTF-8"));
+        String s = readLine(sc);
+        StringBuffer contents = new StringBuffer();
+        String id = "";
+        int count = 0;
+        while (s != null) {
+            if (s.startsWith("msgid")) {
+                count++;
+                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);
+        }
+        return count;
+    }
+
     private static String readLine(Scanner sc) {
         String line = sc.findWithinHorizon("[^\n]*\n", 0);
         if (line == null) {