1

I have used geocoder to get the address through the location. However, the city name is in English and I want to show it in Arabic.

This is the code to get the address :

public String getAddress(double LATITUDE, double LONGITUDE) {
        String city = "";
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
            if (addresses != null && addresses.size() > 0) {

                String address = addresses.get(0).getAddressLine(0); 
                city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                String postalCode = addresses.get(0).getPostalCode();
                String knownName = addresses.get(0).getFeatureName(); 

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return city;
    }

And then this is where I show the city name in a TextView :

String country_and_city = getAddress(latitude, longitude);

TextView city_name = findViewById(R.id.city);  
city_name.setText(country_and_city);

Here, the city name will be displayed in English, how can I translate it or use other methods to show it in Arabic? For example, maybe using google-translate api or something if that is even possible

John Sall
  • 1,027
  • 1
  • 12
  • 25
  • https://cloud.google.com/translate – SamHoque May 31 '21 at 09:52
  • Does this answer your question? [How to use Google Translate API in my Java application?](https://stackoverflow.com/questions/8147284/how-to-use-google-translate-api-in-my-java-application) – sanjeevRm May 31 '21 at 09:53

1 Answers1

0

In order to use the Google Cloud Translation API, you need to have a project which has this API enabled, so you can make authenticated calls. You can set it up here.

Google provide a lot of sample codes, here, which you can use. The code below is used to translate a string Spanish(es) to German(de),

Translation translation =
    translate.translate(
        "Hola Mundo!",
        Translate.TranslateOption.sourceLanguage("es"),
        Translate.TranslateOption.targetLanguage("de"),
        // Use "base" for standard edition, "nmt" for the premium model.
        Translate.TranslateOption.model("nmt"));

System.out.printf("TranslatedText:\nText: %s\n", translation.getTranslatedText());

This article explains in details how to integrate the API within your Android Studio project, it can give you a better overview of the whole process.

Lastly, I should point out that according to the documentation,

Prices are pro rata (proportional and incremental). Charges are scaled to the number of characters actually provided to Cloud Translation. For example, if you send 575,000 characters for processing within a month, you are charged $1.50. The first 500,000 characters are free, and then you are charged for the additional 75,000 characters sent for detection, translation, or both.

As another alternative you can also check the googletrans library, which according to the documentation:

Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.

Alexandre Moraes
  • 3,892
  • 1
  • 6
  • 13