?? preparetranslation.java
字號:
} else {
template.append(parser.getToken());
}
tag = name;
} else if (event == XMLParser.END_ELEMENT) {
String name = parser.getName();
if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name)
|| "em".equals(name)) {
if (ignoreEnd) {
if (buff.length() > 0) {
if (templateIsCopy) {
template.append(buff.toString());
} else {
template.append("${" + nextKey + "}");
}
add(prop, nextKey, buff);
}
template.append(parser.getToken());
} else {
if (buff.length() > 0) {
buff.append(parser.getToken());
buff.append(' ');
}
}
} else {
if (buff.length() > 0) {
if (templateIsCopy) {
template.append(buff.toString());
} else {
template.append("${" + nextKey + "}");
}
add(prop, nextKey, buff);
}
template.append(parser.getToken());
}
tag = (String) stack.pop();
} else if (event == XMLParser.DTD) {
template.append(parser.getToken());
} else if (event == XMLParser.COMMENT) {
template.append(parser.getToken());
} else {
int eventType = parser.getEventType();
throw new Exception("Unexpected event " + eventType + " at " + parser.getRemaining());
}
// if(!xml.startsWith(template.toString())) {
// System.out.println(nextKey);
// System.out.println(template.substring(template.length()-60)
// +";");
// System.out.println(xml.substring(template.length()-60,
// template.length()));
// System.out.println(template.substring(template.length()-55)
// +";");
// System.out.println(xml.substring(template.length()-55,
// template.length()));
// break;
// }
}
new File(target).mkdirs();
String propFileName = target + "/_docs_" + MAIN_LANGUAGE + ".properties";
Properties old = FileUtils.loadProperties(propFileName);
prop.putAll(old);
PropertiesToUTF8.storeProperties(prop, propFileName);
String t = template.toString();
if (templateIsCopy && !t.equals(xml)) {
for (int i = 0; i < Math.min(t.length(), xml.length()); i++) {
if (t.charAt(i) != xml.charAt(i)) {
int start = Math.max(0, i - 30), end = Math.min(i + 30, xml.length());
t = t.substring(start, end);
xml = xml.substring(start, end);
}
}
System.out.println("xml--------------------------------------------------: ");
System.out.println(xml);
System.out.println("t---------------------------------------------------: ");
System.out.println(t);
System.exit(1);
}
return t;
}
private static String clean(String text) {
if (text.indexOf('\r') < 0 && text.indexOf('\n') < 0) {
return text;
}
text = text.replace('\r', ' ');
text = text.replace('\n', ' ');
text = StringUtils.replaceAll(text, " ", " ");
text = StringUtils.replaceAll(text, " ", " ");
return text;
}
private static void add(Properties prop, String document, StringBuffer text) {
String s = text.toString().trim();
text.setLength(0);
prop.setProperty(document, s);
}
private void prepare(String baseDir, String path) throws IOException {
File dir = new File(path);
File[] list = dir.listFiles();
File main = null;
ArrayList translations = new ArrayList();
for (int i = 0; list != null && i < list.length; i++) {
File f = list[i];
if (f.getName().endsWith(".properties")) {
if (f.getName().endsWith("_" + MAIN_LANGUAGE + ".properties")) {
main = f;
} else {
translations.add(f);
}
}
}
Properties p = FileUtils.loadProperties(main.getAbsolutePath());
Properties base = FileUtils.loadProperties(baseDir + "/" + main.getName());
PropertiesToUTF8.storeProperties(p, main.getAbsolutePath());
for (int i = 0; i < translations.size(); i++) {
File trans = (File) translations.get(i);
String language = trans.getName();
language = language.substring(language.lastIndexOf('_') + 1, language.lastIndexOf('.'));
prepare(p, base, trans, language);
}
PropertiesToUTF8.storeProperties(p, baseDir + "/" + main.getName());
}
private void prepare(Properties main, Properties base, File trans, String language) throws IOException {
Properties p = FileUtils.loadProperties(trans.getAbsolutePath());
Properties oldTranslations = new Properties();
for (Iterator it = base.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String m = base.getProperty(key);
String t = p.getProperty(key);
if (t != null && !t.startsWith("#")) {
oldTranslations.setProperty(m, t);
}
}
HashSet toTranslate = new HashSet();
// add missing keys, using # and the value from the main file
Iterator it = main.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String now = main.getProperty(key);
if (!p.containsKey(key)) {
String t = oldTranslations.getProperty(now);
if (t == null) {
if (AUTO_TRANSLATE) {
toTranslate.add(key);
} else {
System.out.println(trans.getName() + ": key " + key + " not found in translation file; added dummy # 'translation'");
t = "#" + now;
p.put(key, t);
}
} else {
p.put(key, t);
}
} else {
String t = p.getProperty(key);
String last = base.getProperty(key);
if (t.startsWith("#") && !t.startsWith("##")) {
// not translated before
t = oldTranslations.getProperty(now);
if (t == null) {
t = "#" + now;
}
p.put(key, t);
} else if (last != null && !last.equals(now)) {
t = oldTranslations.getProperty(now);
if (t == null) {
// main data changed since the last run: review translation
System.out.println(trans.getName() + ": key " + key + " changed, please review; last=" + last
+ " now=" + now);
if (AUTO_TRANSLATE) {
toTranslate.add(key);
} else {
String old = p.getProperty(key);
t = "#" + now + " #" + old;
p.put(key, t);
}
} else {
p.put(key, t);
}
}
}
}
Map autoTranslated = new HashMap();
if (AUTO_TRANSLATE) {
HashSet set = new HashSet();
for (it = toTranslate.iterator(); it.hasNext();) {
String key = (String) it.next();
String now = main.getProperty(key);
set.add(now);
}
if ("de".equals(language)) {
autoTranslated = autoTranslate(set, "en", language);
}
}
for (it = toTranslate.iterator(); it.hasNext();) {
String key = (String) it.next();
String now = main.getProperty(key);
String t;
if (AUTO_TRANSLATE) {
t = "##" + autoTranslated.get(now);
} else {
System.out.println(trans.getName() + ": key " + key + " not found in translation file; added dummy # 'translation'");
t = "#" + now;
}
p.put(key, t);
}
// remove keys that don't exist in the main file (deleted or typo in the key)
it = new ArrayList(p.keySet()).iterator();
while (it.hasNext()) {
String key = (String) it.next();
if (!main.containsKey(key) && !key.startsWith(DELETED_PREFIX)) {
String newKey = key;
while (true) {
newKey = DELETED_PREFIX + newKey;
if (!p.containsKey(newKey)) {
break;
}
}
System.out.println(trans.getName() + ": key " + key + " not found in main file; renamed to " + newKey);
p.put(newKey, p.getProperty(key));
p.remove(key);
}
}
PropertiesToUTF8.storeProperties(p, trans.getAbsolutePath());
}
private Map autoTranslate(Set toTranslate, String sourceLanguage, String targetLanguage) {
HashMap results = new HashMap();
if (toTranslate.size() == 0) {
return results;
}
int maxLength = 1500;
int minSeparator = 100000;
HashMap keyMap = new HashMap(toTranslate.size());
StringBuffer buff = new StringBuffer(maxLength);
// TODO make sure these numbers don't occur in the original text
int separator = minSeparator;
for (Iterator it = toTranslate.iterator(); it.hasNext();) {
String original = (String) it.next();
if (original != null) {
original = original.trim();
if (buff.length() + original.length() > maxLength) {
System.out.println("remaining: " + (toTranslate.size() - separator + minSeparator));
translateChunk(buff, separator, sourceLanguage, targetLanguage, keyMap, results);
}
keyMap.put(new Integer(separator), original);
buff.append(separator);
buff.append(' ');
buff.append(original);
buff.append(' ');
separator++;
}
}
translateChunk(buff, separator, sourceLanguage, targetLanguage, keyMap, results);
return results;
}
private void translateChunk(StringBuffer buff, int separator, String source, String target, HashMap keyMap, HashMap results) {
buff.append(separator);
String original = buff.toString();
String translation = "";
try {
translation = translate(original, source, target);
System.out.println("original: " + original);
System.out.println("translation: " + translation);
} catch (Throwable e) {
System.out.println("Exception translating [" + original + "]: " + e);
e.printStackTrace();
}
for (Iterator it = keyMap.entrySet().iterator(); it.hasNext();) {
Entry entry = (Entry) it.next();
separator = ((Integer) entry.getKey()).intValue();
String o = (String) entry.getValue();
String startSeparator = String.valueOf(separator);
int start = translation.indexOf(startSeparator);
int end = translation.indexOf(String.valueOf(separator + 1));
if (start < 0 || end < 0) {
System.out.println("No translation for " + o);
results.put(o, "#" + o);
} else {
String t = translation.substring(start + startSeparator.length(), end);
t = t.trim();
results.put(o, t);
}
}
keyMap.clear();
buff.setLength(0);
}
/**
* Translate the text using Google Translate
*/
String translate(String text, String sourceLanguage, String targetLanguage) throws Exception {
Thread.sleep(4000);
String url = "http://translate.google.com/translate_t?langpair=" +
sourceLanguage + "|" + targetLanguage +
"&text=" + URLEncoder.encode(text, "UTF-8");
HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; Java)");
String result = IOUtils.readStringAndClose(IOUtils.getReader(conn.getInputStream()), -1);
int start = result.indexOf("<div id=result_box");
start = result.indexOf('>', start) + 1;
int end = result.indexOf("</div>", start);
return result.substring(start, end);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -