⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preparetranslation.java

📁 非常棒的java数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
 * (license2)
 * Initial Developer: H2 Group
 */
package org.h2.tools.i18n;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;

import org.h2.server.web.PageParser;
import org.h2.tools.doc.XMLParser;
import org.h2.util.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.SortedProperties;
import org.h2.util.StringUtils;

/**
 * This class updates the translation source code files by parsing
 * the HTML documentation. It also generates the translated HTML
 * documentation.
 */
public class PrepareTranslation {
    private static final String MAIN_LANGUAGE = "en";
    private static final String DELETED_PREFIX = "~";
    private static final boolean AUTO_TRANSLATE = false;
    private static final String[] EXCLUDE = { "datatypes.html", "functions.html", "grammar.html" };

    public static void main(String[] args) throws Exception {
        new PrepareTranslation().run(args);
    }

    private void run(String[] args) throws Exception {
        String baseDir = "src/docsrc/textbase";
        prepare(baseDir, "src/main/org/h2/res");
        prepare(baseDir, "src/main/org/h2/server/web/res");

        // convert the txt files to properties files
        PropertiesToUTF8.textUTF8ToProperties("src/docsrc/text/_docs_de.utf8.txt",
                "src/docsrc/text/_docs_de.properties");
        PropertiesToUTF8.textUTF8ToProperties("src/docsrc/text/_docs_ja.utf8.txt",
                "src/docsrc/text/_docs_ja.properties");

        // create the .jsp files and extract the text in the main language
        extractFromHtml("src/docsrc/html", "src/docsrc/text", MAIN_LANGUAGE);

        // add missing translations and create a new baseline
        prepare(baseDir, "src/docsrc/text");

        // create the translated documentation
        buildHtml("src/docsrc/text", "docs/html", "en");
        // buildHtml("src/docsrc/text", "docs/html", "de");
        buildHtml("src/docsrc/text", "docs/html", "ja");

        // convert the properties files back to utf8 text files, including the
        // main language (to be used as a template)
        PropertiesToUTF8.propertiesToTextUTF8("src/docsrc/text/_docs_en.properties",
                "src/docsrc/text/_docs_en.utf8.txt");
        PropertiesToUTF8.propertiesToTextUTF8("src/docsrc/text/_docs_de.properties",
                "src/docsrc/text/_docs_de.utf8.txt");
        PropertiesToUTF8.propertiesToTextUTF8("src/docsrc/text/_docs_ja.properties",
                "src/docsrc/text/_docs_ja.utf8.txt");

        // delete temporary files
        File[] list = new File("src/docsrc/text").listFiles();
        for (int i = 0; i < list.length; i++) {
            if (!list[i].getName().endsWith(".utf8.txt")) {
                list[i].delete();
            }
        }
    }

    private static void buildHtml(String templateDir, String targetDir, String language) throws IOException {
        File[] list = new File(templateDir).listFiles();
        new File(targetDir).mkdirs();
        // load the main 'translation'
        String propName = templateDir + "/_docs_" + MAIN_LANGUAGE + ".properties";
        Properties prop = FileUtils.loadProperties(propName);
        propName = templateDir + "/_docs_" + language + ".properties";
        if (!(new File(propName)).exists()) {
            throw new IOException("Translation not found: " + propName);
        }
        Properties transProp = FileUtils.loadProperties(propName);
        for (Iterator it = transProp.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            String t = transProp.getProperty(key);
            // overload with translations, but not the ones starting with #
            if (t.startsWith("##")) {
                prop.put(key, t.substring(2));
            } else if (!t.startsWith("#")) {
                prop.put(key, t);
            }
        }
        // add spaces to each token
        for (Iterator it = prop.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            String t = prop.getProperty(key);
            prop.put(key, " " + t + " ");
        }

        ArrayList fileNames = new ArrayList();
        for (int i = 0; i < list.length; i++) {
            String name = list[i].getName();
            if (!name.endsWith(".jsp")) {
                continue;
            }
            // remove '.jsp'
            name = name.substring(0, name.length() - 4);
            fileNames.add(name);
        }
        for (int i = 0; i < list.length; i++) {
            String name = list[i].getName();
            if (!name.endsWith(".jsp")) {
                continue;
            }
            // remove '.jsp'
            name = name.substring(0, name.length() - 4);
            String template = IOUtils.readStringAndClose(new FileReader(templateDir + "/" + name + ".jsp"), -1);
            String html = PageParser.parse(null, template, prop);
            html = StringUtils.replaceAll(html, "lang=\"" + MAIN_LANGUAGE + "\"", "lang=\"" + language + "\"");
            for (int j = 0; j < fileNames.size(); j++) {
                String n = (String) fileNames.get(j);
                if ("frame".equals(n)) {
                    // don't translate 'frame.html' to 'frame_ja.html', 
                    // otherwise we can't switch back to English
                    continue;
                }
                html = StringUtils.replaceAll(html, n + ".html\"", n + "_" + language + ".html\"");
            }
            html = StringUtils.replaceAll(html, "_" + MAIN_LANGUAGE + ".html\"", ".html\"");
            String target;
            if (language.equals(MAIN_LANGUAGE)) {
                target = targetDir + "/" + name + ".html";
            } else {
                target = targetDir + "/" + name + "_" + language + ".html";
            }
            OutputStream out = new FileOutputStream(target);
            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            writer.write(html);
            writer.close();
        }
    }
    
    private static boolean exclude(String fileName) {
        for (int i = 0; i < EXCLUDE.length; i++) {
            if (fileName.endsWith(EXCLUDE[i])) {
                return true;
            }
        }
        return false;
    }

    private static void extractFromHtml(String dir, String target, String language) throws Exception {
        File[] list = new File(dir).listFiles();
        for (int i = 0; i < list.length; i++) {
            File f = list[i];
            String name = f.getName();
            if (!name.endsWith(".html")) {
                continue;
            }
            if (exclude(name)) {
                continue;
            }
            // remove '.html'
            name = name.substring(0, name.length() - 5);
            if (name.indexOf('_') >= 0) {
                // ignore translated files
                continue;
            }
            String template = extract(name, f, target);
            FileWriter writer = new FileWriter(target + "/" + name + ".jsp");
            writer.write(template);
            writer.close();
        }
    }

    // private static boolean isText(String s) {
    // if (s.length() < 2) {
    // return false;
    // }
    // for (int i = 0; i < s.length(); i++) {
    // char c = s.charAt(i);
    // if (!Character.isDigit(c) && c != '.' && c != '-' && c != '+') {
    // return true;
    // }
    // }
    // return false;
    // }

    private static String getSpace(String s, boolean start) {
        if (start) {
            for (int i = 0; i < s.length(); i++) {
                if (!Character.isWhitespace(s.charAt(i))) {
                    if (i == 0) {
                        return "";
                    } else {
                        return s.substring(0, i);
                    }
                }
            }
            return s;
        } else {
            for (int i = s.length() - 1; i >= 0; i--) {
                if (!Character.isWhitespace(s.charAt(i))) {
                    if (i == s.length() - 1) {
                        return "";
                    } else {
                        return s.substring(i + 1, s.length());
                    }
                }
            }
            // if all spaces, return an empty string to avoid duplicate spaces
            return "";
        }
    }

    private static String extract(String documentName, File f, String target) throws Exception {
        String xml = IOUtils.readStringAndClose(new InputStreamReader(new FileInputStream(f), "UTF-8"), -1);
        StringBuffer template = new StringBuffer(xml.length());
        int id = 0;
        Properties prop = new SortedProperties();
        XMLParser parser = new XMLParser(xml);
        StringBuffer buff = new StringBuffer();
        Stack stack = new Stack();
        String tag = "";
        boolean ignoreEnd = false;
        String nextKey = "";
        boolean templateIsCopy = false;
        while (true) {
            int event = parser.next();
            if (event == XMLParser.END_DOCUMENT) {
                break;
            } else if (event == XMLParser.CHARACTERS) {
                String s = parser.getText();
                String trim = s.trim();
                if (trim.length() == 0) {
                    if (buff.length() > 0) {
                        buff.append(s);
                    } else {
                        template.append(s);
                    }
                } else if ("p".equals(tag) || "li".equals(tag) || "a".equals(tag) || "td".equals(tag)
                        || "th".equals(tag) || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag)
                        || "h4".equals(tag) || "body".equals(tag) || "b".equals(tag) || "code".equals(tag)
                        || "form".equals(tag) || "span".equals(tag) || "em".equals(tag)) {
                    if (buff.length() == 0) {
                        nextKey = documentName + "_" + (1000 + id++) + "_" + tag;
                        template.append(getSpace(s, true));
                    } else if (templateIsCopy) {
                        buff.append(getSpace(s, true));
                    }
                    if (templateIsCopy) {
                        buff.append(trim);
                        buff.append(getSpace(s, false));
                    } else {
                        buff.append(clean(trim));
                    }
                } else if ("pre".equals(tag) || "title".equals(tag) || "script".equals(tag) || "style".equals(tag)) {
                    // ignore, don't translate
                    template.append(s);
                } else {
                    System.out.println(f.getName() + " invalid wrapper tag for text: " + tag + " text: " + s);
                    System.out.println(parser.getRemaining());
                    throw new Exception();
                }
            } else if (event == XMLParser.START_ELEMENT) {
                stack.add(tag);
                String name = parser.getName();
                if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name)) {
                    // keep tags if wrapped, but not if this is the wrapper
                    if (buff.length() > 0) {
                        buff.append(' ');
                        buff.append(parser.getToken().trim());
                        ignoreEnd = false;
                    } else {
                        ignoreEnd = true;
                        template.append(parser.getToken());
                    }
                } else if ("p".equals(tag) || "li".equals(tag) || "td".equals(tag) || "th".equals(tag)
                        || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag) || "h4".equals(tag)
                        || "body".equals(tag) || "form".equals(tag)) {
                    if (buff.length() > 0) {
                        if (templateIsCopy) {
                            template.append(buff.toString());
                        } else {
                            template.append("${" + nextKey + "}");
                        }
                        add(prop, nextKey, buff);
                    }
                    template.append(parser.getToken());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -