From 888795c02be058fc1de4e2c6b9d1fe18d4ee32bf Mon Sep 17 00:00:00 2001 From: Athou Date: Fri, 17 May 2013 18:26:40 +0200 Subject: [PATCH] translation generator --- README.md | 2 + pom.xml | 52 +++++++++++++++++++ ...InternationalizationDevelopmentFilter.java | 2 +- src/main/script/HTMLConcat.groovy | 2 +- src/main/script/I18nGenerator.groovy | 49 +++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/main/script/I18nGenerator.groovy diff --git a/README.md b/README.md index d73f937c..ee0493ca 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ To add a new language, create a new file in that directory. The name of the file should be the two-letters [ISO-639-1 language code](http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). The language has to be referenced in the `languages.properties` file to be picked up. +When adding new terms translations, add them in en.properties then run `mvn -e groovy:execute -Pi18n``. It will parse the english file and add new entries in the other translation files. + Copyright and license --------------------- diff --git a/pom.xml b/pom.xml index 9e3736a3..6716bf57 100644 --- a/pom.xml +++ b/pom.xml @@ -515,5 +515,57 @@ + + i18n + + + + org.codehaus.gmaven + gmaven-plugin + 1.4 + + + commons-io + commons-io + 2.4 + + + + + ${basedir}/src/main/resources/i18n/ + + + ${basedir}/src/main/script + + + def dir = project.properties['dir']; + new + I18nGenerator().generate(dir); + + + + + maven-war-plugin + 2.3 + + + + target/generated-sources/angularjs/ + templates + + + src/main/script/ + + *.js + + js + true + + + + + + + diff --git a/src/main/java/com/commafeed/frontend/utils/InternationalizationDevelopmentFilter.java b/src/main/java/com/commafeed/frontend/utils/InternationalizationDevelopmentFilter.java index 0eecb1ef..ce6307c1 100644 --- a/src/main/java/com/commafeed/frontend/utils/InternationalizationDevelopmentFilter.java +++ b/src/main/java/com/commafeed/frontend/utils/InternationalizationDevelopmentFilter.java @@ -101,7 +101,7 @@ public class InternationalizationDevelopmentFilter implements Filter { String var = m.group(1); Object replacement = props.get(var); String replacementValue = replacement == null ? var : replacement - .toString(); + .toString().split("#")[0].trim(); m.appendReplacement(sb, replacementValue); } m.appendTail(sb); diff --git a/src/main/script/HTMLConcat.groovy b/src/main/script/HTMLConcat.groovy index 98bf539f..dcce8e55 100644 --- a/src/main/script/HTMLConcat.groovy +++ b/src/main/script/HTMLConcat.groovy @@ -44,7 +44,7 @@ public class HTMLConcat { while (m.find()) { String var = m.group(1); Object replacement = props.get(var); - m.appendReplacement(sb, replacement.toString()); + m.appendReplacement(sb, replacement == null ? var : replacement.toString().split("#")[0].trim()); } m.appendTail(sb); return sb.toString(); diff --git a/src/main/script/I18nGenerator.groovy b/src/main/script/I18nGenerator.groovy new file mode 100644 index 00000000..1e8dc46b --- /dev/null +++ b/src/main/script/I18nGenerator.groovy @@ -0,0 +1,49 @@ +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; + +public class I18nGenerator { + + public void generate(String directory) throws Exception { + + File dir = new File(directory); + List enLines = FileUtils.readLines(new File(dir, + "en.properties"), "UTF-8"); + + for (File file : dir.listFiles()) { + if ("languages.properties".equals(file.getName()) + || "en.properties".equals(file.getName())) + continue; + + List newLines = new ArrayList(); + List langLines = FileUtils.readLines(file, "UTF-8"); + + int j = 0; + for (int i = 0; i < enLines.size(); i++) { + + String enLine = enLines.get(i); + String langLine = langLines.get(j); + + if (StringUtils.isNotBlank(enLine)) { + + String key = enLine.split("=")[0]; + if (langLine.startsWith(key)) { + newLines.add(langLine); + } else { + newLines.add(enLine + " ####### Needs translation"); + j--; + } + } else { + newLines.add(null); + } + j++; + } + newLines.add(null); + FileUtils.writeLines(file, "UTF-8", newLines); + + } + } +}