Fork me on GitHub

Export to Android consumable XML files

The trema:exportAndroid goal exports a Trema database into XML files suitable to be used in Android applications. This goal is available as of version 1.1.6.1 of the Trema Maven Plugin.

A file for each language is created which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generated file - do not edit -->
<resources>
  <string name="helloworld">Hallo Welt!</string>
  <string name="byeworld">Tsch�ss &amp; bis bald!</string>
</resources>

For more information about the android resources file format see http://developer.android.com/guide/topics/resources/string-resource.html

Important

  • String Arrays and Quantity Strings (Plurals) as described on the android page above are not supported, only simple Strings.
  • Android is more restrictive about the keys that can be used for texts. Allowed keys are [a-zA-Z_][a-zA-Z0-9_]*. This rule is checked during the exportAndroid goal, and violations will cause the build to fail. Note that as of version 1.1.8 dots (.) and hyphens (-) are also allowed in keys and are converted to underscores (_) before the above rule is checked.
  • The goal is only available through the trema-maven-plugin, it is NOT available in the eclipse plugin and the commandline interface
  • Escaping: The following characters are replaced with corresponding character entities. Note that since this is no real XML escaping, the output string.xml file is potentially not valid xml. However this is explicitly allowed by Android.
    ' is replaced with \\&#039; 
    
    \' is replaced with \\&#039; 
    
    & is replaced with &amp;

The snippet below shows a minimal configuration needed to export the Trema database (*.trm file) into several Android string resource XML files:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>ch.netcetera.nca-236-0</groupId>
      <artifactId>trema-maven-plugin</artifactId>      
      <executions>
        <execution>        
          <goals>
            <goal>exportAndroid</goal>
          </goals>
        </execution>
      </executions>      
    </plugin>
  </plugins>
</build>

The following defaults apply here:

  • The goal exportAndroid is executed during the process-sources phase of the build
  • The path to the trema database (.trm file) is expected to be: ${project.basedir}/src/main/resources/text.trm
  • all languages are exported
  • all states are exported
  • The following filenames are used for the generated xml files: ${project.build.directory}/generated/texts/values-<language>\strings.xml

In case the above defaults are not suitable, the goal can be configured. The following sample shows a more sophisticated configuration of the goal.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>ch.netcetera.nca-236-0</groupId>
      <artifactId>trema-maven-plugin</artifactId>      
      <executions>
        <execution>
          <goals>
            <goal>exportAndroid</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <tremaFile>${project.basedir}/src/main/resources/android/text.trm</tremaFile>
        <exportPath>${project.build.directory}/generated/android-resources/</exportPath>
        <defaultlanguage>de</defaultlanguage>    
      </configuration>
    </plugin> 
    ...
  </plugins>
</build>