]> WPIA git - gigi.git/blob - util/org/cacert/gigi/util/FetchLocales.java
UPD: Added "Done." message to locale fetcher
[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         public static final String DOWNLOAD_SERVER = "translations.cacert.org";
26         public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER + "/export/cacert/%/messages.po";
27         public static final String[] AUTO_LANGS = new String[] { "en", "de", "nl", "pt_BR", "fr", "sv", "it", "es", "hu",
28                         "fi", "ja", "bg", "pt", "da", "pl", "zh_CN", "ru", "lv", "cs", "zh_TW", "el", "tr", "ar" };
29
30         public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
31                 System.out.println("downloading locales ...");
32                 File locale = new File("locale");
33                 locale.mkdir();
34
35                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
36                 DocumentBuilder db = dbf.newDocumentBuilder();
37                 for (String lang : AUTO_LANGS) {
38                         Document doc = db.newDocument();
39                         doc.appendChild(doc.createElement("translations"));
40                         URL fetch = new URL(PO_URL_TEMPLATE.replace("%", lang));
41                         URLConnection uc = fetch.openConnection();
42                         Scanner sc = new Scanner(uc.getInputStream());
43                         String s = readLine(sc);
44                         StringBuffer contents = new StringBuffer();
45                         String id = "";
46                         while (s != null) {
47                                 if (s.startsWith("msgid")) {
48                                         contents.delete(0, contents.length());
49                                         s = readString(s, sc, contents);
50                                         id = contents.toString();
51                                         continue;
52                                 } else if (s.startsWith("msgstr")) {
53                                         contents.delete(0, contents.length());
54                                         // System.out.println("msgstr");
55                                         s = readString(s, sc, contents);
56                                         String msg = contents.toString().replace("\\\"", "\"").replace("\\n", "\n");
57                                         insertTranslation(doc, id, msg);
58                                 } else if (s.startsWith("#")) {
59                                         // System.out.println(s);
60                                 } else if (s.equals("") || s.equals("\r")) {
61
62                                 } else {
63                                         System.out.println("unknown line: " + s);
64                                 }
65                                 s = readLine(sc);
66                         }
67                         TransformerFactory tFactory = TransformerFactory.newInstance();
68                         Transformer transformer = tFactory.newTransformer();
69
70                         DOMSource source = new DOMSource(doc);
71                         FileOutputStream fos = new FileOutputStream(new File(locale, lang + ".xml"));
72                         StreamResult result = new StreamResult(fos);
73                         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
74                         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
75                         transformer.transform(source, result);
76                         fos.close();
77                 }
78                 System.out.println("Done.");
79         }
80
81         private static String readLine(Scanner sc) {
82                 String line = sc.findWithinHorizon("[^\n]*\n", 0);
83                 if (line == null) {
84                         return null;
85                 }
86                 return line.substring(0, line.length() - 1);
87         }
88
89         private static void insertTranslation(Document doc, String id, String msg) {
90                 Node idN = doc.createTextNode(id);
91                 Node textN = doc.createTextNode(msg);
92                 Element tr = doc.createElement("translation");
93                 Element e = doc.createElement("id");
94                 e.appendChild(idN);
95                 tr.appendChild(e);
96                 e = doc.createElement("msg");
97                 e.appendChild(textN);
98                 tr.appendChild(e);
99                 doc.getDocumentElement().appendChild(tr);
100         }
101
102         private static String readString(String head, Scanner sc, StringBuffer contents) throws IOException {
103                 head = head.split(" ", 2)[1];
104                 contents.append(head.substring(1, head.length() - 1));
105                 String s;
106                 while ((s = readLine(sc)) != null) {
107                         if (!s.startsWith("\"")) {
108                                 break;
109                         }
110                         contents.append(s.substring(1, s.length() - 1));
111                 }
112                 return s;
113         }
114
115 }