Integrate translations into development process with Travis and CI’s magic

Translations are important to spread your app across the globe and make a considerable user base. But specially with startups and a small development team, it’s very hard to integrate the translated strings in your app and update them each time a new change is made upstream.

In this article we’ll see how to use Travis to automatically download the translated strings each time a new revision is available, implement them in the app and push the changes on your project’s GitHub. Cool, eh?

Prerequisites

First of all, make sure you’ve a working Travis configuration for your Android app. If you’re new to Travis, you can read how to get started here.

Once you have a working Travis setup, it’s time to pick a Localization Management Platform. At Glucosio, we use CrowdIn that automatically imports all the strings from the strings.xml file of your Android app and allows translators easily submit translations using a comfortable UI.

Write the Script

Let’s assume you’re using CrowdIn for translations.

Crowdin requires a crowdin.yaml configuration file in your project. Place it in your Android /res folder. Here’s Crowdin’s documentation about it. For example, this is Glucosio’s crowdin.yaml:

---
project_identifier: 'glucosio'
api_key_env: 'CROWDIN_API_KEY'
base_url: 'https://api.crowdin.com'

files:
-
source: '/values/strings.xml'
translation: '/values-%android_code%/%original_file_name%'

Now it’s time to write a script that downloads all the strings from Crowdin, copy them in your Android resource folder and finally uploads all on Git. First, let’s clone your repo and set the global variables for git.

#!/usr/bin/env bash
# go home and setup git
cd $HOME
git config — global user.email “your_email@here.com”
git config — global user.name “your_username”
# clone gh-pages branch
git clone https://https://github.com/example.git

A new folder will be created with the content your repo inside. Now we need to navigate to your Andorid res/ folder (where strings are stored).

cd example/app/src/main/res/

Crowdin offers crowdin-cli, a command line tool that allows you to manage and synchronize your localization resources with your Crowdin project. It’s also available as a .jar package. Let’s download and run it to download all the translated strings in the res folder.

wget https://crowdin.com/downloads/crowdin-cli.jar
java -jar crowdin-cli.jar download
#We don't need crowdin-cli anymore
rm crowdin-cli.jar

That’s all. Your res folder is now updated with latest translations and ready to be pushed on git.

git remote rm origin
git remote add origin https://https://github.com/example.git
git add -f .
git commit -m “Automatic translation import (build $TRAVIS_BUILD_NUMBER).”
git push -f origin develop > /dev/null

Finally, simply run the script adding it in the after_success section of your travis.yml file.

after_success:
- chmod +x ./import-translations-github.sh
- ./import-translations-github.sh

The script will run every time Travis builds a new revision of your app and will push the translations only if changes are made upstream. In fact, if there aren’t new translations available, Travis will have no changes to commit and it will not push anything.

But… let’s say it. You’re here only for the full script right? Here you are.

That’s the final script we’re using at Glucosio.

You may notice we’re not exposing sensitive keys. Read here how to use Travis environment variables, which can be accessed from any stage in your build process.

Happy coding.