Search in sources :

Example 1 with TranslationSet

use of org.activityinfo.i18n.tools.model.TranslationSet in project activityinfo by bedatadriven.

the class Pull method updateJavaSource.

/**
 * Updates the @DefaultMessage/@DefaultStringValue annotations and javadoc in the Messages or Constants
 * interface Java source file.
 */
private void updateJavaSource(ResourceClass resourceClass, TranslationSet translations) throws IOException {
    CompilationUnit compilationUnit;
    try {
        compilationUnit = resourceClass.parseJavaSource();
    } catch (Exception e) {
        throw new IOException("Failed to parse " + resourceClass.getJavaSourcePath(), e);
    }
    TranslationSet validated = validateMessages(resourceClass, compilationUnit, translations);
    DefaultUpdatingVisitor visitor = new DefaultUpdatingVisitor();
    visitor.visit(compilationUnit, validated);
    if (visitor.isDirty()) {
        try {
            Files.write(compilationUnit.toString(), resourceClass.getJavaSourceFile(), Charsets.UTF_8);
        } catch (IOException e) {
            throw new IOException("Failed to write updated source file to " + resourceClass.getJavaSourcePath(), e);
        }
        System.out.println("Updated default translations in " + resourceClass.getJavaSourcePath());
    } else {
        System.out.println(resourceClass.getClassName() + " is up to date.");
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TranslationSet(org.activityinfo.i18n.tools.model.TranslationSet) DefaultUpdatingVisitor(org.activityinfo.i18n.tools.parser.DefaultUpdatingVisitor) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with TranslationSet

use of org.activityinfo.i18n.tools.model.TranslationSet in project activityinfo by bedatadriven.

the class Pull method updateProperties.

/**
 * Updates the properties files with the latest translations
 */
private void updateProperties(ResourceClass resourceClass, TranslationSet translations) throws IOException {
    // Validate messages first to avoid causing compile errors
    TranslationSet validated = validateMessages(resourceClass, translations);
    // Write out the properties file
    PropertiesBuilder properties = new PropertiesBuilder();
    properties.addAll(resourceClass, validated);
    File resourceFile = resourceClass.getResourceFile(translations.getLanguage());
    if (properties.getMissingCount() > 0) {
        System.err.println(resourceFile.getName() + " is missing " + properties.getMissingCount() + " translations.");
    }
    Files.write(properties.toString(), resourceFile, Charsets.UTF_8);
    System.err.println("Updated " + resourceFile.getName());
}
Also used : TranslationSet(org.activityinfo.i18n.tools.model.TranslationSet) File(java.io.File) PropertiesBuilder(org.activityinfo.i18n.tools.output.PropertiesBuilder)

Aggregations

TranslationSet (org.activityinfo.i18n.tools.model.TranslationSet)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 File (java.io.File)1 IOException (java.io.IOException)1 PropertiesBuilder (org.activityinfo.i18n.tools.output.PropertiesBuilder)1 DefaultUpdatingVisitor (org.activityinfo.i18n.tools.parser.DefaultUpdatingVisitor)1