Adding Google Translate API to Android apps

Amrit Sanjeev
Sep 4, 2017 · 2 min read

Here are the steps required to add Google Translate API to your Android apps. The intention of this document is to share an approach to implementing the functionality and implementation can be optimized further.

Image credits : https://www.teachingenglish.org.uk

Some things to keep in mind

  1. This is a paid API and hence you need to keep an eye on the usage.
  2. The shared Proguard config is just a working sample. It can still be improved.
  3. You should limit the key usage to just the android app using the console to avoid quota theft. Steps to do that are not in the scope of this article but is relatively straight forward to implement.

Gradle changes

Sample code change

Interface for callback handling

Translation function

Note: Translate function internally runs a network call so it cannot be called on the main UI thread. Hence the above function has to executed on a different thread.

Proguard Config

Some recommendations

  • App size and method count increase is considerable even though we are using only translate API. The web call wrapper approach works better if you have strong need to keep app size small but is less secure against quota theft.
  • Keep a close track on the usage and cost incurred. This API can become expensive for unicode based languages if the number of translations performed are high.
  • Keep a remote config switch to turn the feature off is a good idea. Firebase remote config tool can be used for the same.

Conclusion

Hope this article provides direction to those of you working with Google Translate API on android apps and in case you find improvements over what I have shared here, please do let me know. Thanks in advance.

Amrit Sanjeev

Written by

Technophile , Organizer@BlrDroid , Google Developer Expert

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade