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