Shark
Shark

Reputation: 43

How to translate text from one language to another language android?

I have a response which I parse from json and displayed the result. Is it possible to convert to other language like French, Hindi, German?

When I browsed, I came to know google stopped on 2011 as free version and started pricing. Is there any free version to convert the response text to other language?

Piece of code is as follows:

  TextView text; // created an id.

  JSONObject jsono=new JSONObject(data);
  JSONArray jarray = jsono.getJSONArray("posts");
  for (int i = 0; i < jarray.length(); i++) {
   JSONObject object = jarray.getJSONObject(i);
  String name= object.getString("name");
  text.setText(name);// how to convert this to other language. 

Say for eg: response what I get is Good morning. which I need to translate and display in textview as Bonjour which is in French.

Upvotes: 4

Views: 28582

Answers (2)

Arnav Rao
Arnav Rao

Reputation: 7002

Text translation between languages can be done using Firebase ML kit translator api. Input to the api is text to be translated and source and target languages. The api downloads the source and target language models if not available on the device and performs the translation. You can find complete android example here. https://www.zoftino.com/android-translate-text-example

FirebaseTranslatorOptions options =
        new FirebaseTranslatorOptions.Builder()
                .setSourceLanguage(FirebaseTranslateLanguage.ES)
                .setTargetLanguage(FirebaseTranslateLanguage.EN)
                .build();
FirebaseTranslator Translator =
        FirebaseNaturalLanguage.getInstance().getTranslator(options);

translator.translate(inputText)
      .addOnSuccessListener(
          new OnSuccessListener<String>() {
            @Override
            public void onSuccess(@NonNull String translatedText) {

            }
          })
      .addOnFailureListener(
          new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
     });

Upvotes: 1

bachr
bachr

Reputation: 6006

Here is a detailed blog post on using different translation services on an Android app. The source code is on github with a sample of using MyMemory service for translation.

/** Translate a given text between a source and a destination language */
public String translate(String text) {      
    String translated = null;
    try {
        String query = URLEncoder.encode(text, "UTF-8");
        String langpair = URLEncoder.encode(srcLanguage.getLanguage()+"|"+dstLanguage.getLanguage(), "UTF-8");
        String url = "http://mymemory.translated.net/api/get?q="+query+"&langpair="+langpair;
        HttpClient hc = new DefaultHttpClient();                 
        HttpGet hg = new HttpGet(url);
        HttpResponse hr = hc.execute(hg);
        if(hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                
            JSONObject response = new JSONObject(EntityUtils.toString(hr.getEntity()));
            translated = response.getJSONObject("responseData").getString("translatedText");                
        }
    } catch (Exception e) {
        e.printStackTrace();
    }       
    return translated;      
}

Upvotes: 4

Related Questions