1 package org.cacert.gigi.util;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
7 import java.net.URLConnection;
8 import java.util.Scanner;
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;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
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
27 + "/export/cacert/%/messages.po";
28 public static final String[] AUTO_LANGS = new String[]{"en", "de", "nl",
29 "pt_BR", "fr", "sv", "it", "es", "hu", "fi", "ja", "bg", "pt",
30 "da", "pl", "zh_CN", "ru", "lv", "cs", "zh_TW", "el", "tr", "ar"};
31 public static void main(String[] args) throws IOException,
32 ParserConfigurationException, TransformerException {
33 System.out.println("downloading locales ...");
34 File locale = new File("locale");
37 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
38 DocumentBuilder db = dbf.newDocumentBuilder();
39 for (String lang : AUTO_LANGS) {
40 Document doc = db.newDocument();
41 doc.appendChild(doc.createElement("translations"));
42 URL fetch = new URL(PO_URL_TEMPLATE.replace("%", lang));
43 URLConnection uc = fetch.openConnection();
44 Scanner sc = new Scanner(uc.getInputStream());
45 String s = readLine(sc);
46 StringBuffer contents = new StringBuffer();
49 if (s.startsWith("msgid")) {
50 contents.delete(0, contents.length());
51 s = readString(s, sc, contents);
52 id = contents.toString();
54 } else if (s.startsWith("msgstr")) {
55 contents.delete(0, contents.length());
56 // System.out.println("msgstr");
57 s = readString(s, sc, contents);
58 String msg = contents.toString().replace("\\\"", "\"")
59 .replace("\\n", "\n");
60 insertTranslation(doc, id, msg);
61 } else if (s.startsWith("#")) {
62 // System.out.println(s);
63 } else if (s.equals("") || s.equals("\r")) {
66 System.out.println("unknown line: " + s);
70 TransformerFactory tFactory = TransformerFactory.newInstance();
71 Transformer transformer = tFactory.newTransformer();
73 DOMSource source = new DOMSource(doc);
74 FileOutputStream fos = new FileOutputStream(new File(locale, lang
76 StreamResult result = new StreamResult(fos);
77 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
78 transformer.setOutputProperty(
79 "{http://xml.apache.org/xslt}indent-amount", "2");
80 transformer.transform(source, result);
84 private static String readLine(Scanner sc) {
85 String line = sc.findWithinHorizon("[^\n]*\n", 0);
89 return line.substring(0, line.length() - 1);
91 private static void insertTranslation(Document doc, String id, String msg) {
92 Node idN = doc.createTextNode(id);
93 Node textN = doc.createTextNode(msg);
94 Element tr = doc.createElement("translation");
95 Element e = doc.createElement("id");
98 e = doc.createElement("msg");
101 doc.getDocumentElement().appendChild(tr);
103 private static String readString(String head, Scanner sc,
104 StringBuffer contents) throws IOException {
105 head = head.split(" ", 2)[1];
106 contents.append(head.substring(1, head.length() - 1));
108 while ((s = readLine(sc)) != null) {
109 if (!s.startsWith("\"")) {
112 contents.append(s.substring(1, s.length() - 1));