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 {
26 public static final String DOWNLOAD_SERVER = "translations.cacert.org";
28 public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER + "/export/cacert/%/messages.po";
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"
34 public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
35 System.out.println("downloading locales ...");
36 File locale = new File("locale");
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();
51 if (s.startsWith("msgid")) {
52 contents.delete(0, contents.length());
53 s = readString(s, sc, contents);
54 id = contents.toString();
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")) {
67 System.out.println("unknown line: " + s);
71 TransformerFactory tFactory = TransformerFactory.newInstance();
72 Transformer transformer = tFactory.newTransformer();
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);
82 System.out.println("Done.");
85 private static String readLine(Scanner sc) {
86 String line = sc.findWithinHorizon("[^\n]*\n", 0);
90 return line.substring(0, line.length() - 1);
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");
100 e = doc.createElement("msg");
101 e.appendChild(textN);
103 doc.getDocumentElement().appendChild(tr);
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));
110 while ((s = readLine(sc)) != null) {
111 if ( !s.startsWith("\"")) {
114 contents.append(s.substring(1, s.length() - 1));