Automatic App Translation by Data Binding + Google Translate

Inhalt

Recently while I was playing around with Data Binding newly introduced to Android, I found I could utilize this to provide an instant way to localize Android apps to any languages at runtime.

After a few days of coding I made “InAppTranslation for Android” which is an open source free tool to make your app support all the languages using the power of Google Translate.

This link includes the code of InAppTranslation and a sample app.

Install

Just add a line below to the app module’s build.gradle file (under dependencies section).

Then open the local.properties file and add your Google Translate API key as below.

Replace Your_Google_Translate_API_Key with your own. Google Translate API Key is available at https://cloud.google.com/translate/

local.properties file is normally not shared over GitHub, thus storing the key in this file will avoid you from making it public. (Double check whether your .gitignore having local.properties before sharing your app code.)

Data Binding

To use InAppTransaltion, you must enable Data Binding in your project.

Please follow the instruction below if your project isn’t Data Binding ready yet.

Tag

It is very simple to use InAppTranslation.

In each View, replace android:text tag with app:localizeText.

Below is an example of the tag in TextView.

That’s it!

You can specify a string resource defined in strings.xml.

Here is another example of using the tag with a string resource in Button.

app:localizeText tag supports Views listed below.

  • TextView
  • Button
  • CheckBox
  • RadioButton
  • CheckedTextView
  • EditText

For Switch and ToggleButton, InAppTranslation has two tags for the on and off states of the views.

Here is an example.

If you want to localize “hint” text of EditText, use app:localizeHint tag.

Your Edit text will have a hint localized.

Run the sample code, you will see all the texts translated to your phone language.

All the texts in sample app are translated to Japanese when running it on a phone with Japanese setting.

InAppTranslation reads out the user’s language setting upon launch, and will ask Google Translate to translate texts to that language.
Translation via Google Translate API is done in background, thus doesn’t block the user interface from interacting with your app’s users.

In case you want to translate “from” non-English texts

In the example above, I input English texts such as “Hello” to app:localizeText tag.
If you are developers in non-English speaking country (like me), you may want to use texts in your language as the input as below.

I used a Japanese word meaning “Hello” above.
In this case, translation is done from Japanese to a user’s phone language.

I didn’t specify the work I used is Japanese anywhere but Google Translate infers that “こんにちは” is Japanese automatically.

There is some rare case where this inference is not working correctly.

For example in French, “pain” means “bread”. If you input “pain” to Google Translate, Google Translate will think it as pain in English, and infers that the source language is English.

If you want to input non-English but alphabetic language, it might be safer to specify the source language when asking translation to Google Translate API.
To do that add a line of code as below to onCreate of your custom Application class.

(Replace “fr” with a lanauge of your case )

After that, all the request to Google Translate will have “fr” as the source language parameter.

Working with Switch.

Changing textOff and textOn property of Switch from Java code doesn't change the actual texts on the UI. It seems like a bug in Android.
Due to this limitation, dynamically translated texts for Switch textOn/Off properties are not displayed. To workaround this, I've added a class called IATSwitch.

To use this, first import IATSwitch by adding the code below in <data></data> section in your layout.xml file.

Then, replace Switch with IATSwtich as below

Caching Translation result

InAppTranslation has a cache storing translation. This makes the time to show text much faster and reduces the access to Google Translate API.

By combining Google Translate with Data Binding technology, I made a simple tag to translate user interface at runtime.

I focus on explaining how to use InAppTranslation in this post, but will take time to write more about inside InAppTranslation soon in the future.

I am a Tokyo-based Android / iOS app programmer and running a company called Goldrush Computing Inc.

If your have any question about InAppTranslation, Android / iOS app development, or our company, feel free to contact via@[email protected]

There is an iOS version of InAppTransaltion, which doesn’t use Google Translate but use your translation data. If you are interested please visit
https://inapptranslation.com/

I’m thinking about making the iOS version of InAppTranslation work with Google Translate as well.

Zusammenfassen
The article discusses the creation of 'InAppTranslation for Android,' an open-source tool that uses Google Translate to localize Android apps at runtime. Users can add a line to their app's build.gradle file and provide their Google Translate API key in the local.properties file. The tool requires enabling Data Binding in the project and involves replacing 'android:text' tags with 'app:localizeText' in various views. InAppTranslation reads the user's language setting and translates texts accordingly, without blocking the user interface. The article also addresses translating non-English texts and working with Switch components. The tool caches translations for faster text display and aims to simplify UI translation using Google Translate and Data Binding technology. The author, a Tokyo-based app programmer, plans to provide more insights into InAppTranslation in the future and welcomes inquiries about the tool or app development.