Get some fields in multiple locales. Is it possible to do this in a single API call?

Hi,

I would like to fetch a single entry from the Contentful API, and get some fields in multiple locales and some fields in a single locale.

Specifically, I have a “slug” field that I want in all locales, so I can populate hreflang tags on the page. But then, for every other field in the entry, I only want the values in the current locale.

Is it possible to do this in a single API call? Or do I need to do separate API calls?

Thanks!

Hi there,

The first point here is that each field gives you the ability to configure whether you want to enable localization or not.

The other one is that you could also use the wildcard locale parameter (locale=*) and that will retrieve the content from all locales.

Other than that, it wouldn’t be possible to control the granularity of retrieved locales on the basis of each field.

Hi @gabriel, thanks. @rneimesh and me are working together on this. Would we be able to get your advice on what the “best practice” is for the following situation?

In Contentful, we have an entry type for articles. Each article has all fields localized, including a “slug” field that indicates what the permalink is for each language. On our website, we have an article template (e.g. domain.tld/en/my-example-article, domain.tld/es/mi-articulo-de-ejemplo, etc…) which calls the Contentful API to retrieve the entry in the selected language (i.e. with locale parameter). But we also need to get every slug for that article in all languages in order to populate hreflang alternate tags for SEO

Right now, the only way we can achieve this is to do two API calls: one for the entry in the selected language, then a separate one just for the slug field in all locales. This seems very inefficient and adds unnecessary latency. We also don’t want to get the entry in all languages (to avoid the two API calls), because the payload of that request would be huge.

Is there a better way to achieve this?

I would guess this is a very common use case for any website dealing with localized content, and one that would be ideal if Contentful had a more efficient way to achieve the desired result.

Thanks!

1 Like

Hi @ralph, maybe you can solve this issue by creating a content-type which stores the url in an text field and connecting it with a localized reference field in the content-type of your article. Let me know, if this solution fits your needs.

Hey Ralph,

I’m running into the exact same problem. What solution did you eventually come to?

Hey @simon.somlai, this was a proof of concept we were working on, so we just ended up doing two API calls, which is not ideal.

Contentful should ideally open up their GraphQL API to all product tiers, so it would be easier to grab all of these things at once in a single request. In lieu of that, I guess one could set up some other “offline” process to capture and store the data locally, to save on API calls, or some other workaround along those lines…

Let me know what you end up doing.

Two calls it is :wink:

Contentful should ideally open up their GraphQL API to all product tiers, so it would be easier to grab all of these things at once in a single request

Maybe they’ll do this in the future as it matures more. Then again, it’s still a business so they can choose whatever they want to do with this.