Get started with the LingoHub Android SDK - How to localize your Android Apps with Over The Air updates

Post featured image

This article shows you how to localize an Android app and make it available in multiple languages by using the LingoHub Android SDK. We will guide you through the process of adding the SDK and making your Mobile app ready to get text/translation updates without releasing a new version (Over The Air updates). With the Android SDK from LingoHub, we help you localize your Android apps efficiently.

Prerequisites

For the purpose of this tutorial, we are creating a new Android project called Funtastic. We are using Android Studio to create this project, but you can use any other IDE (like Eclipse for instance). Funtastic will have English as a source language and will be translated into German.

Project Layout

Project layout in Android Studio

We created a simple splash screen for the app, including some labels and buttons. The strings that are used in the layout are defined in the strings.xml file:

<resources>
  <string name="claim">Unleash your iamgination</string>
  <string name="login">Login</string>
  <string name="sign_up">Sign Up</string>
  <string name="forgot_password">Forgot your password?</string>
</resources>

Now let's assume we have finished our app project and published it to the Google Play Store (Yeah!). But shortly after the release we notice a typo in our strings file. Instead of imagination we typed iamgination. Of course, such a typo is embarrassing, so we want to quickly fix it. Without the LingoHub SDK, our only option would be to create a new release, which is quite cumbersome and slow. This is where the magic of the LingoHub SDK comes in handy.

Integrating the SDK

The LingoHub SDK is hosted on jitpack, so before you add the SDK to your gradle file, you have to add the jitpack maven repository:

allprojects {
    repositories {
        ...
        maven {
            url 'https://jitpack.io'
        }
    }
}

Now, add the LingoHub SDK dependency to your application-level .gradle file:

dependencies {
    ...
    implementation 'com.lingohub:lh-android-sdk:1.0.0-beta01'
}

Voila, now you have added the LingoHub SDK as a dependency to your project. That means you can now use the LingoHub classes in your code. However, before we do that, we have to configure LingoHub to get the API Key and the Project Id.

Connecting to LingoHub

Before you start with this section, make sure you have either an iOS or Android project on LingoHub. How you can create one, is in detail described in our help. To connect the SDK to LingoHub, two steps are necessary:

  1. Creating a package that contains the resource files
  2. Creating an API on LingoHub for the project

Creating a package

Creating a package is really simple and can be done through LingoHub's intuitive UI. By creating a package you have to enter the following information:

  • Platform: Either iOS or Android. This option tells the system what kind of files (.string or .xml) the package will contain. Note, although LingoHub's projects are associated with only one platform, it is possible to create an Android package in an iOS project and vice versa. So if you have one project for both, Android and iOS, you need to create two packages for OTA updates.
  • Stage: This is optional to use. Basically it allows you to distinguish between two environments, production and pre-production, so you can test the new texts before releasing it to production.
  • Version from and to: Since you normally don't have one version out in the wild, it is handy to specify for which versions a package is responsible. This allows you to manage different and incompatible versions. The from version is inclusive and the to version is exclusive, as it is in most programming languages.

A package consists of one or more releases. You can think of a release like a version of a package. Only one release can be active. Check the following graphic on how the SDK determines which package-release combo to use. Note: We created a package that is responsible for all Android apps, which means the texts stay compatible between the versions. The actual version of your app will be used against the package version.

Packages and releases explained

For this tutorial, we will use

  • Platform: Android
  • Stage: Production
  • Version from: 1
  • Version to: 3

LingoHub will automatically create a release for a new package.

Create package

Create package holding the translations from LingoHub

Creating an API Key

Now you need to add an API Key and the Project Id to your project, so that the SDK can access the LingoHub API. Creating the API is really easy, simply click the Create API Key under packages. Then a dialog shows up showing the new API Key + the Project Id. Important, the API key is not displayed again, you can only generate a new one, so please make sure that you save your API Key + Project Id somewhere safe!

Create LingoHub API Key

API Key and Project Id for connecting the SDK

Having now the necessary information, we need to set it in the SDK:

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        LingoHub.configure(this, "<api key>", "<project id>")

        // Add following line only if you want to use pre-production packages.
        LingoHub.setPreproductionEnabled()

        // Fetch the latest translations from LingoHub
        LingoHub.fetchStrings()
}

Replacing the texts

In order to be able to replace strings, the SDK needs to be wrapped around your activity context. In your activity override the attachBaseContext() as follows:

// we use a base class for all our activities
abstract class BaseActivity : AppCompatActivity() {
    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LingoHub.wrap(newBase))
    }
    override fun getResources(): Resources {
        return LingoHub.wrap(baseContext).resources
    }
}

That's it! Every time your app starts, LingoHub checks for updated strings and replaces them whenever a string is requested by your app.

Over The Air updates

Now everything is in place and the SDK listens to changes on LingoHub. So the next step is to inform the SDK when we want the texts updated. We can do this by creating a new release for the package that we created.

Important: As stated above a package can have multiple releases, but only one can be active. The active release will be delivered to the SDK. This way you have the ability to version your releases and if necessary quickly switch back. The SDK then will ping the LingoHub API and will be informed that new texts are available. So it will download and update the texts. This only happens once, the texts are then stored in your app.

Creating a new release

Releasing new texts via LingoHub

And like magic all your users get the new texts without the need for a new release:

Over The Air Updates

Summary

We hope we got you as excited as we are about this new feature. Check out the code of this tutorial and the SDK. If you have any feedback or questions, don't hesitate to contact us. We happily help you setting up OTA updates for your awesome app.

Btw. if you are not a LingoHub user yet, start the free trial and contact us!

Links: