Android

Tml for Android provides an integrated solution for continuous localization of your Android applications.

Installation

Tml for Android is distributed using Maven and is hosted on Maven Central. Add the following dependency to your pom file:

    <dependency>
      <groupId>com.translationexchange</groupId>
      <artifactId>tml-android</artifactId>
      <version>0.2.1</version>
    </dependency>

If you use Eclipse or IntelliJ IDEs, the dependencies will be automatically downloaded. This SDK depends on Tml Core library that will be installed and added to your classpath.

Integration

You now can initialize the SDK using the following code:

    Tml.init(Utils.buildOptions(
      "key", "YOUR-PROJECT-KEY"
      "token", "YOUR-PROJECT-TOKEN"
    ));

Keep in mind that all of the code in Tml Core is single threaded. It is up to the container application to ensure that certain methods are called in a separate thread. For instance, the above Tml initialization code will use network, if available, to download the latest language definitions for the current language. Therefore it is necessary to execute this code in a separate thread. A splash screen is a good way to initialize the application assets - and that would be a good place to put the above code.

Here is an example of how the initialization done in a splash screen: SplashScreenActivity0java

Using Tml in Java

You now can use the translation methods provided by Tml SDK. Below are some examples:

    Tml.translate("Hello World");

    Tml.translate("Number of messages: {count}", Utils.buildMap(
      "count", 5
    ));

    Tml.translate("Hello {user.name}, you are a {user.gender}", Utils.buildMap(
      "user", Utils.buildMap("name", "Michael", "gender", "male")
    ));

    Tml.translate("You have {count||message}", Utils.buildMap(
      "count", 5
    ));

    Tml.translate("{user| He, She} likes this movie.", Utils.buildMap(
      "user", Utils.buildMap("gender", "male")
    ));

    Tml.translate("[bold: Adjust fonts] using HTML.", Utils.buildMap(
      "bold", "<strong>{$0}</strong>"
    ));

    Tml.translate("[red: Change color] using HTML.", Utils.buildMap(
      "red", "<font color='red'>{$0}</font>"
    ));

    Tml.translate("Nest [bold]some bold and [italic: italic][/bold] using HTML.", Utils.buildMap(
      "italic", "<i>{$0}</i>", "bold", "<strong>{$0}</strong>"
    ));

    Tml.translateSpannableString("[link: {actor}] uploaded [bold: {count|a document, #count# documents}] to a public folder.", Utils.buildMap(
        "actor", getActor(),
        "count", getCount(),
        "link", Utils.buildMap("color", "blue"),
        "bold", Utils.buildMap("style", "bold")
    ));

    Tml.translateSpannableString("[link: {actor}] tagged [link: {target}] in [link: {owner::pos}] photo.", Utils.buildMap(
        "actor", getActor(),
        "target", getTarget(),
        "owner", getOwner(),
        "link", Utils.buildMap("color", "blue")
    ));

To learn more about TML, visit our Translation Markup Language section.

Sample Applications & Source Code

You've read this far! Thank you. You can find the source code for all of the above examples at the following GitHub repository.

https://github0com/translationexchange/tml-android-samples-wammer

$ git clone https://github.com/translationexchange/tml-android-samples-wammer.git

You can also use some of the sample from the basic app:

$ git clone https://github.com/translationexchange/tml-android-samples-basic.git