import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* . <p/> @author George Wei
*/
public class I18nTool {
private static final String TEMP_RES_ENCODING = "gbk";
private static final String TEMP_RES_FILE = "temp.properties";
private static final String FINAL_PROPERTIES_FILE = "messages.properties";
public static final int BUFF_SIZE = 10240;
private static CodepageDetectorProxy detector;
private static Map<String, String> knownFileEncodings = new HashMap<String, String>();
private static enum TagStyle {jstl, spring};
private static TagStyle tagStyle;
private static boolean debug;
private static final Pattern CHINESE_PATTERN = Pattern.compile("[ \\w\\ufe30-\\uffa0\\u4e00-\\u9fa5]*[\\u4e00-\\u9fa5]+[ \\w\\ufe30-\\uffa0\\u4e00-\\u9fa5]*");
static {
detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
}
public static void main(String[] args) {
if (args.length <= 1) {
printUsage();
return;
}
parseKnownFileEncodings(args);
parseDebugMode(args);
String cmdType = args[0].toLowerCase();
if ("-get".equals(cmdType) && args.length >= 3)
getChineseWord(args[1], args[2]);
else if ("-put".equals(cmdType) && args.length >= 2) {
//parse tag style
if ((args.length >= 3) && "spring".equals(args[2].toLowerCase()))
tagStyle = TagStyle.spring;
else
tagStyle = TagStyle.jstl;
//write message tags
putChineseWord(args[1]);
}
else
printUsage();
}
private static void parseKnownFileEncodings(String[] args) {
for (int i = 0; i < args.length; i++) {
if ("-force".equalsIgnoreCase(args[i])) {
String[] fileEncodings = args[i+1].split(";");
for (String pair : fileEncodings) {
String[] encodingPair = pair.split("=");
String[] files = encodingPair[0].split(",");
for (String file : files) {
knownFileEncodings.put(file.toLowerCase(), encodingPair[1]);
}
}
break;
}
}
if (debug)
System.out.println("Known file encodings: " + knownFileEncodings.toString());
}
private static void parseDebugMode(String[] args) {
for (String argv : args) {
if ("-debug".equalsIgnoreCase(argv)) {
debug = true;
return;
}
}
}
public static void printUsage() {
System.out.println("JSP Internationalization Tool");
System.out.println("Usage: I18nTool <Get Options> | <Put Options> [Force Encoding] [Debug]");
System.out.println("");
System.out.println(" <Get Options> - Get Chinese strings from JSP files and save to properties-");
System.out.println(" like text file");
System.out.println(" Syntax: -get <Root folder of JSP files> <key prefix>");
System.out.println(" <Root folder of JSP files> - No need to explain");
System.out.println(" <key prefix> - Prefix of keys in target text file");
System.out.println(" Output: " + TEMP_RES_FILE + " (Intermedia properites-like file)");
System.out.println("");
System.out.println(" <Put Options> - Replace Chinese strings in JSP file with message tags");
System.out.println(" Syntax: -put <Root folder of JSP files> [Tag Style]");
System.out.println(" <Root folder of JSP files> - No need to explain");
System.out.println(" [Tag Style] - Message tag style, can be jstl or spring. If omitted, jstl");
System.out.println(" is used");
System.out.println(" Output: " + FINAL_PROPERTIES_FILE+ " (Final properties file)");
System.out.println("");
System.out.println(" [Force Encoding] - This tool uses cpdetector to automatically determine");
System.out.println(" encoding of JSP files, but it may not work correctly in some cases. If it");
System.out.println(" occurs, use this option to specify the correct encoding for these files.");
System.out.println(" Syntax: -force \"<File Encoding List>\"");
System.out.println(" <File Encoding List> - Semicolon (;) seperated encoding items. Each item");
System.out.println(" is a pair of equal sign (=) seperated file name(s)");
System.out.println(" and encoding name. If multi file names specified,");
System.out.println(" they are seperated by commas (,)");
System.out.println(" Example: -force \"file1,file2=encoding1,file3=encoding2\"");
System.out.println(" Note: File name(s) must be full qualified");
System.out.println("");
System.out.println(" [Debug] - If set, this tool will print debug info while running");
System.out.println(" Syntax: -debug");
}
private static String getFileEncoding(File file) {
java.nio.charset.Charset charset = null;
String knownEncoding = knownFileEncodings.get(file.getPath().toLowerCase());
if (knownEncoding != null)
return knownEncoding;
try {
charset = detector.detectCodepage(file.toURI().toURL());
} catch (Exception ex) {
ex.printStackTrace();
}
if (charset != null) {
return charset.name();
}
return null;
}
private static void putChineseWord(String path) {
List<File> files = new ArrayList<File>();
getJsps(new File(path), files);
Map<String, String> i18nMap = loadI18nMap(TEMP_RES_FILE);
for (File file : files) {
String content = file2String(file);
Matcher m = CHINESE_PATTERN.matcher(content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String key = i18nMap.get(m.group().trim());
m.appendReplacement(sb, key == null ? m.group() : getI18nString(key));
}
m.appendTail(sb);
string2File(sb.toString(), file);
}
saveI18nMap(i18nMap, FINAL_PROPERTIES_FILE);
}
private static void saveI18nMap(Map<String, String> map, String path) {
Properties ps = new Properties();
try {
FileOutputStream fos = new FileOutputStream(new File(path));
for (Map.Entry<String, String> entry : map.entrySet()) {
ps.setProperty(entry.getValue(), entry.getKey());
}
ps.store(fos, null);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static Map<String, String> loadI18nMap(String path) {
Map<String, String> map = new HashMap<String, String>();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (!line.startsWith("#")) {
String[] ss = line.split("=");
if (ss.length >