Slagathor
Slagathor

Reputation: 892

Set Facebook graph Request to always return English results

The language of the results returned depends on the settings on a users phone and/or their location. I want to force the request to only return English results. The request in question is just a simple "me" request for a user. So far I have tried to add "local" in the parameteres like this:

GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "albums, id, birthday, photos&locale=en_us");    
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

I have also tried to do it like this:

String graphPath = "/me?fields=id,name , first_name,albums, last_name&locale=en_us";
    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            Log.i(TAG, "response: " + response);
            Log.i(TAG, "object: " + response.getJSONObject());
        }
    });
    GraphResponse response = request.executeAsync();

Both without any luck. Last method actually works in the Facebooks Graph Api Explorer: https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dalbums%26locale%3Dnb_no&version=v2.5

Without adding the "&locale=en_us" the top graph request works as intended.

Upvotes: 3

Views: 1017

Answers (1)

Slagathor
Slagathor

Reputation: 892

It was actually a bug in the Facebook sdk version I had, so it would not return only english results. It works to set "locale" as an extra parameter. The correct way to do it is:

GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "gender, email, name, albums");
    param.putString("locale", "en_US");
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

If this does not work for you, try to upgrade your Facebook sdk. I'm now on version 'com.facebook.android:facebook-android-sdk:4.11.0' and this works perfectly.

Upvotes: 3

Related Questions