3

How to Build a Translation Function

 2 years ago
source link: https://www.codeproject.com/Tips/5331621/How-to-Build-a-Translation-Function
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

E-book_Reader_compressed.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 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, including:

  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:
    Copy Code
    MLApplication.getInstance().setAccessToken("your access token");

    Or an API key:

    Copy Code
    MLApplication.getInstance().setApiKey("your ApiKey");
  2. Create a real-time translator:
    Copy Code
    MLLocalTranslateSetting setting = new MLLocalTranslateSetting
            .Factory()
            .setSourceLangCode(mSourceLangCode)
            .setTargetLangCode(mTargetLangCode)
            .create();
    this.localTranslator =
    
    MLTranslatorFactory.getInstance().getLocalTranslator(setting);
  3. Query the languages supported by the service.
    Copy Code
    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.
    Copy Code
    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.
    Copy Code
    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.

History

  • 10th May, 2022: Initial version

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK