How to Build a Translation Function
Tags: java, programming, machinelearning, translation
Published: 2022-05-10 10:37:00

## 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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cic7y7j0a47eh1uyuddg.gif)
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](https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/real-time-translation-0000001050040079) 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](https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/config-agc-0000001050990353%20?ha_source=hms1). This includes the following:
1. Configure the app information.
2. Enable the service.
3. Integrate the SDK of the service.
4. Configure the obfuscation scripts.
5. 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.

## References

- [Why Translation Apps and Services are More Important Now](https://www.globalbankingandfinance.com/why-translation-apps-and-services-are-more-important-now/)

- [Why Translation is Important In A World Where English is Everywhere](https://www.k-international.com/blog/why-translation-is-important/)

- [ML Kit](https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/service-introduction-0000001050040017?ha_source=hms1)