translation generator

This commit is contained in:
Athou
2013-05-17 18:26:40 +02:00
parent 54841a92de
commit 888795c02b
5 changed files with 105 additions and 2 deletions

View File

@@ -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
---------------------

52
pom.xml
View File

@@ -515,5 +515,57 @@
</plugins>
</build>
</profile>
<profile>
<id>i18n</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<configuration>
<properties>
<dir>${basedir}/src/main/resources/i18n/</dir>
</properties>
<scriptpath>
<element>${basedir}/src/main/script</element>
</scriptpath>
<source>
def dir = project.properties['dir'];
new
I18nGenerator().generate(dir);
</source>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>target/generated-sources/angularjs/</directory>
<targetPath>templates</targetPath>
</resource>
<resource>
<directory>src/main/script/</directory>
<includes>
<include>*.js</include>
</includes>
<targetPath>js</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -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);

View File

@@ -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();

View File

@@ -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<String> 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<String> newLines = new ArrayList<String>();
List<String> 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);
}
}
}