backbone for i18n support (#55)

This commit is contained in:
Athou
2013-05-11 19:21:53 +02:00
parent 637c41f2ce
commit 3778b44588
7 changed files with 181 additions and 11 deletions

View File

@@ -3,26 +3,50 @@ import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLConcat {
public void concat(String source, String prefix, String destination)
private static final Pattern PATTERN = Pattern.compile('\\$\\{(.+?)\\}');
public void concat(String source, String prefix, String destination, String i18nPath)
throws IOException {
StringBuilder sb = new StringBuilder();
sb.append("<div>");
for (File file : new File(source).listFiles()) {
if (file.isFile()) {
for(File i18n : new File(i18nPath).listFiles()) {
StringBuilder sb = new StringBuilder();
sb.append("<div>");
for (File file : new File(source).listFiles()) {
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append(String.format(
"<script type=\"text/ng-template\" id=\"%s%s\">",
prefix, file.getName()));
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append(FileUtils.readFileToString(file));
sb.append(translate(FileUtils.readFileToString(file), i18n));
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append("</script>");
sb.append(SystemUtils.LINE_SEPARATOR);
}
sb.append("</div>");
String dest = destination.substring(0, destination.lastIndexOf(".")) + "." + i18n.getName().split("\\.")[0] + ".html";
FileUtils.writeStringToFile(new File(dest), sb.toString());
}
sb.append("</div>");
FileUtils.writeStringToFile(new File(destination), sb.toString());
}
public String translate(String content, File i18n) {
Properties props = new Properties();
props.load(new FileInputStream(i18n));
return replace(content, props);
}
public String replace(String content, Properties props) {
Matcher m = PATTERN.matcher(content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String var = m.group(1);
Object replacement = props.get(var);
m.appendReplacement(sb, replacement.toString());
}
m.appendTail(sb);
return sb.toString();
}
}