]> WPIA git - gigi.git/blob - util/org/cacert/gigi/util/FetchLocales.java
4c93021e9e38acf9f44e3ef947e2a885e27c9c8e
[gigi.git] / util / org / cacert / gigi / util / FetchLocales.java
1 package org.cacert.gigi.util;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.net.URL;
7 import java.net.URLConnection;
8 import java.util.Scanner;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.OutputKeys;
14 import javax.xml.transform.Transformer;
15 import javax.xml.transform.TransformerException;
16 import javax.xml.transform.TransformerFactory;
17 import javax.xml.transform.dom.DOMSource;
18 import javax.xml.transform.stream.StreamResult;
19
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23
24 public class FetchLocales {
25
26     public static final String DOWNLOAD_SERVER = "translations.cacert.org";
27
28     public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER + "/export/cacert/%/messages.po";
29
30     public static final String[] AUTO_LANGS = new String[] {
31             "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"
32     };
33
34     public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
35         System.out.println("downloading locales ...");
36         File locale = new File("locale");
37         locale.mkdir();
38
39         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
40         DocumentBuilder db = dbf.newDocumentBuilder();
41         for (String lang : AUTO_LANGS) {
42             Document doc = db.newDocument();
43             doc.appendChild(doc.createElement("translations"));
44             URL fetch = new URL(PO_URL_TEMPLATE.replace("%", lang));
45             URLConnection uc = fetch.openConnection();
46             Scanner sc = new Scanner(uc.getInputStream());
47             String s = readLine(sc);
48             StringBuffer contents = new StringBuffer();
49             String id = "";
50             while (s != null) {
51                 if (s.startsWith("msgid")) {
52                     contents.delete(0, contents.length());
53                     s = readString(s, sc, contents);
54                     id = contents.toString();
55                     continue;
56                 } else if (s.startsWith("msgstr")) {
57                     contents.delete(0, contents.length());
58                     // System.out.println("msgstr");
59                     s = readString(s, sc, contents);
60                     String msg = contents.toString().replace("\\\"", "\"").replace("\\n", "\n");
61                     insertTranslation(doc, id, msg);
62                 } else if (s.startsWith("#")) {
63                     // System.out.println(s);
64                 } else if (s.equals("") || s.equals("\r")) {
65
66                 } else {
67                     System.out.println("unknown line: " + s);
68                 }
69                 s = readLine(sc);
70             }
71             TransformerFactory tFactory = TransformerFactory.newInstance();
72             Transformer transformer = tFactory.newTransformer();
73
74             DOMSource source = new DOMSource(doc);
75             FileOutputStream fos = new FileOutputStream(new File(locale, lang + ".xml"));
76             StreamResult result = new StreamResult(fos);
77             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
78             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
79             transformer.transform(source, result);
80             fos.close();
81         }
82         System.out.println("Done.");
83     }
84
85     private static String readLine(Scanner sc) {
86         String line = sc.findWithinHorizon("[^\n]*\n", 0);
87         if (line == null) {
88             return null;
89         }
90         return line.substring(0, line.length() - 1);
91     }
92
93     private static void insertTranslation(Document doc, String id, String msg) {
94         Node idN = doc.createTextNode(id);
95         Node textN = doc.createTextNode(msg);
96         Element tr = doc.createElement("translation");
97         Element e = doc.createElement("id");
98         e.appendChild(idN);
99         tr.appendChild(e);
100         e = doc.createElement("msg");
101         e.appendChild(textN);
102         tr.appendChild(e);
103         doc.getDocumentElement().appendChild(tr);
104     }
105
106     private static String readString(String head, Scanner sc, StringBuffer contents) throws IOException {
107         head = head.split(" ", 2)[1];
108         contents.append(head.substring(1, head.length() - 1));
109         String s;
110         while ((s = readLine(sc)) != null) {
111             if ( !s.startsWith("\"")) {
112                 break;
113             }
114             contents.append(s.substring(1, s.length() - 1));
115         }
116         return s;
117     }
118
119 }