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.
Some things to keep in mind
- This is a paid API and hence you need to keep an eye on the usage.
- The shared Proguard config is just a working sample. It can still be improved.
- 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.
Sample code change
Interface for callback handling
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.
- 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.
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.