mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Minor improvements to translator
git-svn-id: https://zxing.googlecode.com/svn/trunk@2589 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
4787132564
commit
ee5f46ff97
|
@ -37,6 +37,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -44,15 +45,17 @@ import java.util.regex.Pattern;
|
|||
* <p>A utility which auto-translates the English-language text in a directory of HTML documents using
|
||||
* Google Translate.</p>
|
||||
*
|
||||
* <p>Pass the Android client assets/ directory as first argument, and the language to translate to second.
|
||||
* Specify "all" for language to try to translate for all existing translations.
|
||||
* Optionally, you can specify the files to translate individually.
|
||||
* Usage: {@code HtmlAssetTranslator android/assets/ es [file1.html file2.html ...]}</p>
|
||||
* <p>Pass the Android client assets/ directory as first argument, and the language to translate to second
|
||||
* as a comma-separated list. Specify "all" for language to try to translate for all existing translations.
|
||||
* Each argument after this is the name of a file to translate; if the first one is "all", all files will
|
||||
* be translated.</p>
|
||||
*
|
||||
* <p>Usage: {@code HtmlAssetTranslator android/assets/ (all|lang1[,lang2 ...]) (all|file1.html[ file2.html ...])}</p>
|
||||
*
|
||||
* <p>This will translate .html files in subdirectory html-en to directory html-es, for example.
|
||||
* Note that only text nodes in the HTML document are translated. Any text that is a child of a node
|
||||
* with {@code class="notranslate"} will not be translated. It will also add a note at the end of
|
||||
* the translated page that indicates it was automatically translated.</p>
|
||||
* <p>{@code android/assets/ es all} will translate .html files in subdirectory html-en to
|
||||
* directory html-es, for example. Note that only text nodes in the HTML document are translated.
|
||||
* Any text that is a child of a node with {@code class="notranslate"} will not be translated. It will
|
||||
* also add a note at the end of the translated page that indicates it was automatically translated.</p>
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
|
@ -63,16 +66,21 @@ public final class HtmlAssetTranslator {
|
|||
private HtmlAssetTranslator() {}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length < 3) {
|
||||
System.err.println("Usage: HtmlAssetTranslator android/assets/ " +
|
||||
"(all|lang1[,lang2 ...]) (all|file1.html[ file2.html ...])");
|
||||
return;
|
||||
}
|
||||
File assetsDir = new File(args[0]);
|
||||
Collection<String> languagesToTranslate = parseLanguagesToTranslate(assetsDir, args[1]);
|
||||
Collection<String> filesToTranslate = parseFilesToTranslate(args);
|
||||
List<String> restOfArgs = Arrays.asList(args).subList(2, args.length);
|
||||
Collection<String> fileNamesToTranslate = parseFileNamesToTranslate(assetsDir, restOfArgs);
|
||||
for (String language : languagesToTranslate) {
|
||||
translateOneLanguage(assetsDir, language, filesToTranslate);
|
||||
translateOneLanguage(assetsDir, language, fileNamesToTranslate);
|
||||
}
|
||||
}
|
||||
|
||||
private static Collection<String> parseLanguagesToTranslate(File assetsDir,
|
||||
CharSequence languageArg) {
|
||||
private static Collection<String> parseLanguagesToTranslate(File assetsDir, CharSequence languageArg) {
|
||||
Collection<String> languages = new ArrayList<String>();
|
||||
if ("all".equals(languageArg)) {
|
||||
FileFilter fileFilter = new FileFilter() {
|
||||
|
@ -90,10 +98,23 @@ public final class HtmlAssetTranslator {
|
|||
return languages;
|
||||
}
|
||||
|
||||
private static Collection<String> parseFilesToTranslate(String[] args) {
|
||||
private static Collection<String> parseFileNamesToTranslate(File assetsDir, List<String> restOfArgs) {
|
||||
Collection<String> fileNamesToTranslate = new ArrayList<String>();
|
||||
for (int i = 2; i < args.length; i++) {
|
||||
fileNamesToTranslate.add(args[i]);
|
||||
if ("all".equals(restOfArgs.get(0))) {
|
||||
File htmlEnAssetDir = new File(assetsDir, "html-en");
|
||||
FileFilter fileFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return file.isFile() && file.getName().endsWith(".html");
|
||||
}
|
||||
};
|
||||
for (File file : htmlEnAssetDir.listFiles(fileFilter)) {
|
||||
fileNamesToTranslate.add(file.getName());
|
||||
}
|
||||
} else {
|
||||
for (String fileName : restOfArgs) {
|
||||
fileNamesToTranslate.add(fileName);
|
||||
}
|
||||
}
|
||||
return fileNamesToTranslate;
|
||||
}
|
||||
|
@ -154,7 +175,7 @@ public final class HtmlAssetTranslator {
|
|||
}
|
||||
if (node.getNodeType() == Node.TEXT_NODE) {
|
||||
String text = node.getTextContent();
|
||||
if (text.trim().length() > 0) {
|
||||
if (!text.trim().isEmpty()) {
|
||||
text = StringsResourceTranslator.translateString(text, language);
|
||||
node.setTextContent(' ' + text + ' ');
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package com.google.zxing;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
|
@ -62,7 +63,7 @@ public final class StringsResourceTranslator {
|
|||
|
||||
private static final String APACHE_2_LICENSE =
|
||||
"<!--\n" +
|
||||
" Copyright (C) 2011 ZXing authors\n" +
|
||||
" Copyright (C) 2013 ZXing authors\n" +
|
||||
'\n' +
|
||||
" Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
|
||||
" you may not use this file except in compliance with the License.\n" +
|
||||
|
@ -124,11 +125,12 @@ public final class StringsResourceTranslator {
|
|||
System.out.println("Translating " + language);
|
||||
|
||||
File resultTempFile = File.createTempFile(parentName, ".xml");
|
||||
resultTempFile.deleteOnExit();
|
||||
|
||||
boolean anyChange = false;
|
||||
Writer out = null;
|
||||
try {
|
||||
out = new OutputStreamWriter(new FileOutputStream(resultTempFile), UTF8);
|
||||
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resultTempFile), UTF8));
|
||||
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
||||
out.write(APACHE_2_LICENSE);
|
||||
out.write("<resources>\n");
|
||||
|
@ -166,6 +168,8 @@ public final class StringsResourceTranslator {
|
|||
System.out.println(" Writing translations");
|
||||
translatedFile.delete();
|
||||
resultTempFile.renameTo(translatedFile);
|
||||
} else {
|
||||
resultTempFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +212,7 @@ public final class StringsResourceTranslator {
|
|||
StringBuilder translateResult = new StringBuilder(200);
|
||||
Reader in = null;
|
||||
try {
|
||||
in = new InputStreamReader(connection.getInputStream(), UTF8);
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8));
|
||||
char[] buffer = new char[1024];
|
||||
int charsRead;
|
||||
while ((charsRead = in.read(buffer)) > 0) {
|
||||
|
@ -221,11 +225,14 @@ public final class StringsResourceTranslator {
|
|||
}
|
||||
|
||||
private static SortedMap<String,String> readLines(File file) throws IOException {
|
||||
SortedMap<String,String> entries = new TreeMap<String,String>();
|
||||
if (!file.exists()) {
|
||||
return entries;
|
||||
}
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF8));
|
||||
String line;
|
||||
SortedMap<String,String> entries = new TreeMap<String,String>();
|
||||
while ((line = reader.readLine()) != null) {
|
||||
Matcher m = ENTRY_PATTERN.matcher(line);
|
||||
if (m.find()) {
|
||||
|
|
Loading…
Reference in a new issue