Ad

Change Language Of Google Places Autocomplete Prediction

- 1 answer

I'm using google map and google places API in my android application. I have a problem with Autocomplete prediction, I'm trying to search a Persian address,but I get result in English. I get primary text of result in Persian as well, but problem is in secondary text(Country and city name), that is English Always; Like this:

خیابان چارمردون، Iran, Tehran

I tried many ways to fix this issue, and when i changed my phone language to Persian, Results showed in Persian. Do anyone know how i can fix it?

This is my Autocomplete Filter:

AutocompleteFilter provideAutoCompleteFilter() {
    return new AutocompleteFilter.Builder()
            .setCountry("IR")
            .build();
}

This is my Adapter:

public class PlaceArrayAdapter extends ArrayAdapter<PlaceArrayAdapter.PlaceAutoComplete> implements Filterable {

    private static final String TAG = "PlaceArrayAdapter";
    private GoogleApiClient mGoogleApiClient;
    private AutocompleteFilter mPlaceFilter;
    private LatLngBounds mBounds;
    private ArrayList<PlaceAutoComplete> mResultList;
    private LayoutInflater inflater;


    public PlaceArrayAdapter(Context context, int resource, LatLngBounds bounds,
                             AutocompleteFilter filter) {
        super(context, resource);
        mBounds = bounds;
        mPlaceFilter = filter;
        inflater = LayoutInflater.from(getContext());
    }

    public void setGoogleApiClient(GoogleApiClient googleApiClient) {
        if (googleApiClient == null || !googleApiClient.isConnected()) {
            mGoogleApiClient = null;
        } else {
            mGoogleApiClient = googleApiClient;
        }
    }

    @Override
    public int getCount() {
        return mResultList.size();
    }

    @Override
    public PlaceAutoComplete getItem(int position) {
        return mResultList.get(position);
    }


    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                if (constraint != null) {
                    mResultList = getPredictions(constraint);

                    if (mResultList != null) {
                        results.values = mResultList;
                        results.count = mResultList.size();
                    }
                }

                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    // The API returned at least one result, update the data.
                    notifyDataSetChanged();
                } else {
                    // The API did not return any results, invalidate the data set.
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }


    private ArrayList<PlaceAutoComplete> getPredictions(CharSequence constraint) {

        if (mGoogleApiClient != null) {

            Log.i(TAG, "Executing autocomplete query for -----> " + constraint);

            PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi
                    .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                            mBounds, mPlaceFilter);

            // Wait for predictions, set the timeout.
            AutocompletePredictionBuffer autocompletePredictions = results
                    .await(60, TimeUnit.SECONDS);

            final Status status = autocompletePredictions.getStatus();
            if (!status.isSuccess()) {
                Log.e(TAG, "Error getting place predictions -----> " + status
                        .toString());
                autocompletePredictions.release();
                return null;
            }

            Log.i(TAG, "Query completed. Received -----> " + autocompletePredictions.getCount()
                    + " predictions.");
            Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
            ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
            while (iterator.hasNext()) {
                AutocompletePrediction prediction = iterator.next();
                resultList.add(new PlaceAutoComplete(prediction.getPlaceId(),
                        prediction.getFullText(null)));
            }
            // Buffer release
            autocompletePredictions.release();
            return resultList;
        }

        Log.e(TAG, " -----> Google API client is not connected.");
        return null;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.auto_complete_text_view,parent,false);
        TextView tv = viewGroup.findViewById(R.id.tv);
        tv.setText(getItem(position).description);
        return viewGroup;
    }



    public class PlaceAutoComplete {

        public CharSequence placeId;
        public CharSequence description;

        PlaceAutoComplete(CharSequence placeId, CharSequence description) {
            this.placeId = placeId;
            this.description = description;
        }

        @Override
        public String toString() {
            return description.toString();
        }
    }
}
Ad

Answer

I got it, The problem fixed by changing the application language, using the below code:

    Configuration config = getBaseContext().getResources().getConfiguration();
    Locale locale = new Locale("fa"); // <---- your target language
    Locale.setDefault(locale);
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

Maybe it's not the true way, but it worked!

Ad
source: stackoverflow.com
Ad