Background
Programmers are — or should be — voracious readers. To keep up with the latest updates in the world of software, we need to be constantly scrolling through books, forum articles, news, and more.
This process is certainly mentally enriching, but it can also be a tiring and tedious one due to one major obstacle: language. I used to struggle a lot with reading articles written in another language, because I was looking up every word that I couldn't understand in the dictionary in order to make sense of what I was reading — until I developed this.
No muss, no fuss. Just select the foreign text you don't understand and instantly translate it into a language that you want.
Now let's get to the development part. Not being much of a linguist, I knew that I would struggle to develop a translation feature for my app all on my own.
Luckily, I got a great helper — HMS Core ML Kit's translation service. It supports real-time and on-device translation, making translation possible even in the absence of an Internet connection. With the help of the translation service, language barriers become a thing of the past.
Now, I'll explain how I developed this function, using the source code for the demo above.
Development Process
Preparations
Make necessary preparations as detailed here. This includes the following:
- Configure the app information.
- Enable the service.
- Integrate the SDK of the service.
- Configure the obfuscation scripts.
- Declare necessary permissions.
Function Building
1) Set the app authentication information via an access token:
MLApplication.getInstance().setAccessToken("your access token");
Or an API key:
MLApplication.getInstance().setApiKey("your ApiKey");
2) Create a real-time translator.
MLLocalTranslateSetting setting = new MLLocalTranslateSetting
.Factory()
.setSourceLangCode(mSourceLangCode)
.setTargetLangCode(mTargetLangCode)
.create();
this.localTranslator =
MLTranslatorFactory.getInstance().getLocalTranslator(setting);
3) Query the languages supported by the service.
MLTranslateLanguage.getCloudAllLanguages().addOnSuccessListener(new OnSuccessListener<Set<String>>() {
@Override
public void onSuccess(Set<String> result) {
// Callback when the supported languages are obtained.
}
});
4) Translate the text.
localTranslator.preparedModel(downloadStrategy, modelDownloadListener).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
final Task<String> task = localTranslator.asyncTranslate(input);
task.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String text) {
displaySuccess(text, true);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
displayFailure(e);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
displayFailure(e);
}
});
5) Release resources occupied by the translator when the translation is complete.
if (localTranslator != null) {
localTranslator.stop();
}
And voila, the translation function is built.
Besides e-book readers, there are lots of other apps that can benefit greatly from having a translation function, such as travel apps, which can use the translation service to translate foreign road signs and menus for visitors. Translation is also useful for educational apps, to help users who are not familiar with the language used in the app.
That concludes my development journey for the demo e-book reader. What other ideas and suggestions do you have for using the translation function? Feel free to provide your thoughts in the comments section.
Top comments (0)